Using function from another Script (Godot 3.0)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By adamGodot
:warning: Old Version Published before Godot 3 was released.

Hello! What is the correct way to use a function from one script in another script? (Godot 3.0) I have a Camera with a camera.gd script and a shake function in it. I am trying to use the function in enemy script. I get the node in enemy script (onready var camera = get_node("res://scripts/Camera2D.gd")) and then call camera.shake() but I get this error: Attemp to call function ‘shake’ in base ‘null instance’ on null instance.

Thank you!

:bust_in_silhouette: Reply From: atorresm

You are calling get_node on something that is not a node!

First, instantiate the object:

var camera = load("res://scripts/Camera2D.gd").new()

Then, you can just call the function:

camera.shake()

However, in your case, it seems like you want to do something using a node that is already in the scene that you’re using. In that case, you can just do:

$camera_node.shake()

Assuming camera_node is your camera with the camera script attached and it is a direct child of the node that owns the script from which you want to do the call.

Hello! I have the same problem, but i need to call a script function attached on spawned enemy.
When i tried to use a load function i got an error message,
I can’t call a dinamic function by that (or something similar)
How i can solve this??
Thank you in advice!

SalvoGalvagno | 2019-04-03 20:59

Hey, thank you for your support so far. This case seems much more complicated than I first thought :slight_smile:

So basically I have this structure:

GameManager (scene): contains a player node (Area 2D) + camera + UI and some timers. This scene also has a script that is taking care of player movement + ui + cam movement etc.

Level_1 (scene): contains GameManager + a random level generator which produces a gamefield out of 4 different types of fields (area2D).

And this is the problem I have:
Every time I push a button the player will take 5 steps in a random direction. (this is already managed inside the GameManager.gd). And for each step I want to check (inside the GameManager.gd) on what type of field the character just stepped. Depending on the type of field different events should be triggered.

I hope this helping somehow. Please let me know if you need any more information.
Thx :slight_smile:

BigBackPack | 2020-07-29 14:57