How to attach a script to a node in gdscript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By frankiezafe

Hello,

I’d like to attach gdscript to a node at runtime with something like this:

var cscript = load( ReVA_PATH_CONSTRAINT )
for i in range( node.get_bone_count() ):
    var bname = node.get_bone_name(i)
    var ba = BoneAttachment.new()
    node.add_child( ba )
    ba.bone_name = bname
    ba.script = cscript.new()

There is no error, but when i print ba.script, the attribute is still to null.
Am I missing something? Or it is simply not the way to do this?

Thanks in advance,

françois z.

:bust_in_silhouette: Reply From: frankiezafe

Just found the solution here (with an error):

var cscript = load( ReVA_PATH_CONSTRAINT )
for i in range( node.get_bone_count() ):
    var bname = node.get_bone_name(i)
    var ba = BoneAttachment.new()
    ba.set_script( cscript )
    node.add_child( ba )
    ba.bone_name = bname

I have added the script before moving the object in the tree, to avoid missing events, not sure if it is usefull…