Can I pass a reference as an argument for a function call

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

Sorry if I’m not very clear, but I’m quite new.
As for the question, here’s an example:

I have a whole bunch of sprite nodes that I want to hide through script, so I write a function

func _hide_this_node(the_node):
    the_node.hide()

I was hoping to use this for a state machine technique I saw on Youtube where classes act as different states, but to use the “change state” function, that function must use a if else/switch statement to check the state you want to instantiate.

func set_state("the name of the new state"):
    Exit current state
    if the name of the new state = state1, then state1.new()
    elif the name of the new state = state2, then state2.new()

and so on

So to prevent the is else hassle, I would simply use

var state = Idle.new()

class Idle
class Run

func set_state(reference):
    Exit current state
    reference.new()

so an example would work out like

set_state(Idle)

And the function would instatiate whichever class I typed as a reference