Calling a function on a newly added script

Godot Version

4.4

Question

Hello there,

I’m trying to dynamically setup some play pieces, setting a script on them:

for child_node in puzzle_scene.get_children():
	if not child_node.name.begins_with("piece_"):
		continue
		
	child_node.set_script("res:puzzlepiece.gd")
    var piece = child_node as PuzzlePiece
	piece.setup(Vector3(3,3,3), Vector3(2,2,2))

I can see the _init() function being called but when I cast to the type of the script I get NIL. I can also not call my function without casting, I get an error that the function doesn’t exist.

What am I missing?

Thanks for any hints!

seb

Well, shouldn’t it be res://? I noticed you had just res:, don’t know if that’d impact anything.

Also, after setting sdript have you tried node.set_process(true)?

1 Like

That AND I realized I need to load the resource too…

so this works:

var piece_script = load("res://PuzzlePiece.gd")
child_node.set_script(piece_script)

As you can see, I’m a bloody beginner :slight_smile: Thanks for helping out!

1 Like