How can I run a function called by a string?

Ardima96Ardima96 Posts: 1Member

Hello,
Here you are the code:

var cylinder=str("$Cylinder/Lead_",(turn))

In this first part i assigned to a var called cylinder the name of a node i want to call. It is an AudioStreamPlayer3D.
turn is another variable which changes through the code and it can be 1, 2, 3 and so on.
I would like to do something like this:

cylinder.play()

This is why i think it should be like:

$Cylinder/Lead_1.play()

But it doesn't work, any suggest?

Ty anyway!

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 892Admin

    Have you tried var cylinder = get_node("Cylinder/Lead_" + str(turn))?

    One reason why the code you posed above is not working, is because you are not getting the node, but instead creating a string. If you want to use cylinder.play() then you need to get the AudioStreamPlayer3D node into the cylinder variable so you can call functions on it (in this case, the play function).


    I could be wrong (I do not use the $ shorthand), but I think the reason $Cylinder/Lead_1.play() is not working is because $ is trying to get a child node in Cylinder called Lead_1.play(), as opposed to getting a child node in Cylinder called Lead_1 and then calling the play function.

    In theory, the following should work (though as I said, I do not use $): $"Cylinder/Lead_1".play() or get_node("Cylinder/Lead_1").play()


    If neither of these work, could you post the error message Godot prints to the debugger? It would make it easier to help find a solution to the problem.

Sign In or Register to comment.