+2 votes

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!

in Engine by (20 points)

1 Answer

+11 votes
Best answer

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:


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:


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.

by (77 points)
selected by

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!

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

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 :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.