How can I run a function called by a string?

Ardima96Ardima96 Posts: 1Member

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:

This is why i think it should be like:


But it doesn't work, any suggest?

Ty anyway!


  • 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 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/ is not working is because $ is trying to get a child node in Cylinder called, 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.