0 votes

Hello to everyone. I spent only a couple of weeks using Godot.
I´m creating a little puzzle game. The game consists in a board scene (PuzzleManager), a block scene (Block), and a Main scene that for now,only contains the board.

Mi Scene Tree and filesystem

Blocks scenes are instantiated by a function in PuzzleManager and added as his child. But the code in the ready function never executes

I´ll show you first the script attached to PuzzleManager (function create_board() is called in PuzzleManager _ready(). It works fine, create all the pieces correctly

#Blocks are created from a Dictionary, if the function reads "r" creates a block and #makes it red (block has a color property) and so on

var BoardDefault:Dictionary ={
"row0": ["r","e","e","b","p","e"],
"row1": ["e","e","e","e","o","e"],
"row2": ["e","e","e","g","e","e"],
"row3": ["e","e","e","g","e","e"],
"row4": ["e","e","o","p","e","e"],
"row5": ["r","e","e","e","e","b"],

}

var block_path = preload("res://Scenes/Block.tscn")


func create_board():
var dictionary_keys:Array = BoardDefault.keys()
# i = x j= y
for i in BoardDefault.size():
    for j in range (GRID_CELLS_VERT):
        var values_array:Array = BoardDefault.get(dictionary_keys[i])
        var current_read_value:String = values_array[j]
        var id = block_path.instance()
        add_child(id)
        id.position.x =  (CELL_SIZE * j)
        id.position.y = (CELL_SIZE*i)
        # id.sprite.texture funciona porque el block tiene una referencia a su nodo hijo Sprite
        match current_read_value:
            "b":
                var target_texture:Texture = load("res://Assets/Block64x64Blue.png")
                id.color = "blue"
                id.sprite.texture = target_texture
            "e":
                var target_texture:Texture = load("res://Assets/Block64x64GreySolid.png")
                id.color = "empty"
                id.sprite.texture = target_texture
            "g":
                var target_texture:Texture = load("res://Assets/Block64x64Green.png")
                id.color = "green"
                id.sprite.texture = target_texture
            "o":
                var target_texture:Texture = load("res://Assets/Block64x64Orange.png")
                id.color = "orange"
                id.sprite.texture = target_texture
            "p":
                var target_texture:Texture = load("res://Assets/Block64x64Purrple.png")
                id.color = "purple"
                id.sprite.texture = target_texture
            "r":
                var target_texture:Texture = load("res://Assets/Block64x64Red.png")
                id.color = "red"
                id.sprite.texture = target_texture

The problem is that the _ready() function of the blocks doesnt work, input functions does work and variables seem ok in Remote inspector
I´ll paste here the script attached to Block.tscn

extends Area2D
var color:String
onready var sprite:Sprite = $Sprite
func assign_group_by_color():
match color:
    "blue","green","orange","purple","red":
        add_to_group("ColorBlocks",false)
        print("Es de color")
    "empty":
        add_to_group("EmptyTiles",false)
func _ready():
assign_group_by_color()
func _on_Block_input_event(viewport, event, shape_idx):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed == true:
    print(color + str(position))
    var my_group = get_groups()
    print(my_group)

I can see that the _ready function is not working because blocks are not added to groups: When I try to print the group I only see an empty array

Thank you very much for reading this long newby question !!!

Godot version 3.3.2
in Engine by (54 points)

Probably _ready function is being executed, just try to put a print and see if it works. I guess that the problem is in assign_group_by_color, when is it called, the variable color has a value assigned?

Thank you very much for taking the time to answer my question. I think you gave me a hint, I added a lot of prints to check whats happening. I think the problem is that I first add the blocks as childs, and then I try to assign variables to them, but, correct me if I wrong, _ready() function executes when instances are added as children, right?

I'm starting using Godot also, so don't take all my sentences to be true 100%. The use of print is used generally when you are coding to see what is happening. Here you have some documentation. According to my experience, first use _init to get your nodes ready, I mean: assign values, add children... and then you use _ready for example to connect them via signals.

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 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.