0 votes

I have a level consisting of different parts, the level moves to the left. If the first part reaches a certain position, it gets teleported back, would be simple enough but nothing works and i get an invalid get index... why? my arrays are all assigned, even worse, it deletes my arrays from the inspector when hit play.

extends Node2D

var position_cache : Vector2
export onready var levels = []

func _ready():
    position_cache = levels.size().position

func _process(delta):

    for i in levels:
        print("something happening here?");
        levels[i].position.x -= 200 * delta

    if position.x < -600:
        if levels.size() > 0:
            levels[0].position = position_cache
in Engine by (163 points)

1 Answer

+1 vote
Best answer

You're using a for-each type loop, so "i" is actually the object, not the index. This might work:

for level in levels:
    level.position.x -= 200 * delta

Also, this looks off to me:

func _ready():
    position_cache = levels.size().position

Array.size() returns an integer. You can use back() to get the last array element, if that is what you wanted to do instead.

by (129 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.