0 votes

Hey, so Im working on a dungeon crawler thing and this has me completly stumped. This is the code:

    #Spawn Player
    var player = PLAYER.instance()
    get_parent().call_deferred("add_child", player)
    player.position = emptyTiles[0] * GRID_SIZE
    player.z_index = PLAYER_LAYER
    itemlist.append(player)
    print(itemlist)

    #Make Stairs
    var stairs_to_make = 2
    for stairs_exist in stairs_to_make:
        var stairs = STAIRS.instance()
        get_tree().get_root().call_deferred("add_child", stairs)


        if stairs_exist == 0:
            stairs.direction = "Up"
        else:
            stairs.direction = "Down"
            stairs.connect("next_room", self, "generate_new_dungeon")
        stairs.position = emptyTiles[0] * GRID_SIZE
        stairs.z_index = STAIR_LAYER
        emptyTiles.remove(0)
        itemlist.append(stairs)

    #Spawn Items
    var max_items = emptyTiles.size() / 80
    round(max_items)
    var min_items = emptyTiles.size() / 140
    round(min_items)
    print (max_items,",", min_items)
    var items_to_spawn = randi() % (max_items - min_items + 1) + min_items

    for items in items_to_spawn:
        var testitem = TESTITEM.instance()
        get_tree().get_root().call_deferred("add_child", testitem)
        testitem.position = emptyTiles[0] * GRID_SIZE
        emptyTiles.remove(0)
        itemlist.append(testitem)

#Debug functions---------------------------------------------------------------
func _input(event):
    if event.is_action_pressed("ui_accept"):
        generate_new_dungeon()

func generate_new_dungeon():
    print("Before: ", itemlist)
    for i in itemlist.size():
        itemlist[i].queue_free()
    itemlist.clear()
    print("After: ", itemlist)
    if itemlist.size() == 0:
        get_tree().reload_current_scene()

To clarify, all items in the dungeon is spawned at random locations, and all items are Area2D's. itemlist is an array to keep track of all of them. emptyTiles is an array with all tiles not occupied by walls.
When the player stands on top of an item some information about it is printed. When the dungeon is reset, all items stop interacting with the player, the only thing working is the walls. Once the scene is reloaded once more, everything returns to normal. I have no idea what is happening. Any help finding out what is going on would be gretly appreciated. If anyone wants to take a look at the code as a whole I would gladly upload the project somewhere for people to look at.

in Engine by (14 points)

Please log in or register to answer this question.

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 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 webmaster@godotengine.org with your username.