0 votes

I have a node structure as

-Control
    Buttons (A Control node)
    Sprite

In the root node, I have a script which is meant to add 50 buttons to the 2nd Control node.Well its not working at all ie it does not display any button at all but when I outputprint(get_node("buttons.get_children())) it gives me this:

[[Button:1142]]
[[Button:1142], [Button:1147]]
[[Button:1142], [Button:1147], [Button:1152]]
[[Button:1142], [Button:1147], [Button:1152], [Button:1157]]
[[Button:1142], [Button:1147], [Button:1152], [Button:1157], [Button:1162]]
[[Button:1142], [Button:1147], [Button:1152], [Button:1157], [Button:1162], [Button:1167]]
[[Button:1142], [Button:1147], [Button:1152], [Button:1157], [Button:1162], [Button:1167], [Button:1172]]
[[Button:1142], [Button:1147], [Button:1152], [Button:1157], [Button:1162], [Button:1167], [Button:1172], [Button:1177]]
[[Button:1142], [Button:1147], [Button:1152], [Button:1157], [Button:1162], [Button:1167], [Button:1172], [Button:1177], [Button:1182]]
[[Button:1142], [Button:1147], [Button:1152], [Button:1157], [Button:1162], [Button:1167], [Button:1172], [Button:1177], [Button:1182], [Button:1187]]
[[Button:1142], [Button:1147], [Button:1152], [Button:1157], [Button:1162], [Button:1167], [Button:1172], [Button:1177], [Button:1182], [Button:1187], [Button:1192]]
[[Button:1142], [Button:1147], [Button:1152], [Button:1157], [Button:1162], [Button:1167], [Button:1172], [Button:1177], [Button:1182], [Button:1187], 

Etc

Yet it shows no button on the screen.I am sure that globals.CURRENT_LEVEL is et to 50

Here is my code:

extends Control

var CURRENT_HIGHEST_LEVEL = globals.CURRENT_LEVEL
#onready var level_buttons = buttons.get_children()
var button_init_pos  = Vector2(100,100)
# Called when the node enters the scene tree for the first time.
onready var buttons = get_node("Buttons")
func _ready():
    print(buttons)
    print(CURRENT_HIGHEST_LEVEL)
    var i =1
    for i in range(CURRENT_HIGHEST_LEVEL+1):
        if i >0:
            var button = Button.new()
            var button_sprite = Sprite.new()
            button_sprite.texture = load("res://assets/button ("+str(i)+").png")
            button.add_child(button_sprite)
            buttons.add_child(button)
            print(buttons.get_children())
            if i == 1:
                button.set_position(button_init_pos)
            else:
                var prev_button =  buttons.get_child(i -1)
                button.set_position(Vector2(prev_button.get_position().x + 100, button_init_pos.y))



#           print("The level chooser button at : "+str(i)+" has position :"+str(buttons.get_child(i).get_position()))
in Engine by (71 points)

1 Answer

+1 vote
Best answer

I don't have Godot at hand so I'll have a read and deduce from your post.

First checking the obvious: is your Buttons node visible? Is any of its parents hidden?
Also, are you sure buttons or their parents are not positioned too far away to be seen? I suggest you check the node hierarchy to make sure of that.

Now, your loop code can be simplified.
From this:

var i =1
for i in range(CURRENT_HIGHEST_LEVEL+1):
    if i >0:

To this:

for i in range(1, CURRENT_HIGHEST_LEVEL+1):

In fact, you could even change it to:

for i in CURRENT_HIGHEST_LEVEL:

And replace your if i == 1 with if i == 0 and str(i) with str(i + 1), but that's minor.

Now here is a mistaken part:

var prev_button =  buttons.get_child(i -1)

get_child() indexes start from 0. But as we just saw, your loop ignores 0, so prev_button will actually give you the button you just added (i representing the count of buttons and not the index). Then you are moving that button to the right, but it's not incremental since prev_button == button.
So I assume the result you obtain is all your buttons stacked in the same spot...

You are saying you see no button at all but from what I read, something would show up. I don't know what could cause none of them to show.

by (28,789 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 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.