Functions that work on _ready dont work in code

Godot 4.5

I’ll just post all my code here cause I dont think I can explain any other way. It is very sphagetti code, dont laugh at me, this is mostly temporary for testing purposes anyway, just help pls

func _ready() -> void:
	initialize_item(1)
	initialize_item(2)
	initialize_item(3)

func initialize_item(index: int) -> void:
	var new = item.instantiate()
	new.data.length = index
	new.data.dimentions = Vector2i(index, 1)
	new.size = Vector2i(index * SLOT_SIZE, SLOT_SIZE)
	if index == 1:
		new.update_texture(text_1)
	elif index == 2:
		new.update_texture(text_2)
	elif index == 4:
		new.update_texture(text_4)
	elif index == 5:
		new.update_texture(text_5)
	else:
		new.update_texture(text_3)
	add_child(new)
	init_item.emit(new)

that stuff makes my first 3 items, which are sprite2D’s. thats all in it’s own script, cause it’s just for testing. “new” is added as a child just so the reparent function works later, init_item.emit(new) signals this stuff in a vboxcontainer:

func _on_bag_init_item(node: Node) → void:
add_item(node)

func add_item(new_item: Node) -> void:
	var new_slot = bag_slot.instantiate() #creates a button as an inventory slot
	add_child(new_slot)
	new_slot.connect("clicked", item_select) #dont mind this
	new_item.get_placed() #or this
	new_item.reparent(new_slot) #sprite is added as a child of the button
	move_child(new_slot, 1)
	new_slot.do() #THIS is the part that doesnt work
	var i: int = get_child_count()
	resize_bag.emit(i) #resizes the master container that contains the scroll container that contains the vboxcontainer that contains the buttons that contain the items

this is the part in the button, that works on the initial three items:

func do() -> void:
	var item = get_child(0)
	item.centered = false
	item.offset = Vector2(2, -16)

that’s necessary to position the sprite in the button correctly, and when the sprites are used elsewhere these things need to be set to other stuff.

this is what we look like after ready, the top button is selected to show how the sprite is positioned.

however, then i press that button there to make more items while running. thats inside the same script as the initialize_item function

func _on_test_button_pressed() -> void:
	var index = randi_range(1, 5) #this is to get items of random sizes, this is consistent for all numbers
	initialize_item(index)

thats what it looks like after i press the button. The thing is, in remote view i can see that the variables are set to the correct numbers, yet its all like that. I can keep pressing it and initiating more, and the three originals will stay cool and centered and all of the new ones are like the one above. what gives

Nvm i figured it out
It had to do with the sprites not having their global position set, i assumed it would automatically be set to the position of the parent node but i guess thats just with controls. so all I had to do is make the item slot button also set the sprite’s position to its own

1 Like

What was it?

1 Like

i’ll edit my solution reply

While it isn’t a problem in GDscript, I’d advise against variable names like “new” because it is a reserved words in other programming languages and you could use a better name like “new_item” which you did use in the second script and having consistency across various scripts will help you.
Just to avoid future confusion and to remind you that naming things isn’t that hard and always worth an extra thought.

3 Likes

thats good advice, thank you!