How to call a function in another script, Godot's Visual Script Tutorial

archerallstars

Archer Allstars

Posted on April 23, 2022

How to call a function in another script, Godot's Visual Script Tutorial

Update: Godot's Visual Script will discontinue in Godot 4.0. Therefore, it's useless to continue using Visual Script in Godot now.

A lot of people who use Godot's Visual Script don't know how to call a function in another script.

Well, that's easy! You can just drag and drop a scene node, which is attached to the script, into the canvas as shown in the screen recording below:

Drag and Drop Operation

A proper way to do it

Imagine if you have to call a function from another script a hundred times, but you need to change the structure of the scene node to which the script is attached later, then you'll definitely have a serious problem of having to edit those hundred nodes.

Therefore this is how you do to prevent it:

  • Set a scene node to an object variable in the _ready function which will be called only once.

Set a scene node

  • Change call mode of the node to Instance.

Change call mode

  • Put the object variable to the instance input port.

Put the object to the input port

Now, if you need to change anything to this scene node that you want to call a function from, all you have to do is change just one node in the _ready function, instead of a hundred nodes in the canvas!

I hope this helps 👍


Cover photo by NoWah Bartscher on Unsplash

💖 💪 🙅 🚩
archerallstars
Archer Allstars

Posted on April 23, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related