How do to call functions from another scene?

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

I want that every time a player hits an enemy with a bullet, the player will receive +1 to energy bar. I have attached screenshots of functions in bullet script and player script. This does not work in the game and gives an error from the third screenshot. How can i solve this problem?

enter image description here

enter image description here

enter image description here

:bust_in_silhouette: Reply From: Tato64

Screenshots are broken, but anyways…

What you need is a Singleton

You can read more about it but i’ll make a short guide for you here:

You will create a new script (Not tied to any nodes, just right click on the file explorer and choose “New Script…”, name it “Playersingleton” or something like that.

Then you will click on “Project > Project Settings” go to the “AutoLoad” tab, click on the file icon to add that script (Note: You will have to give it a name and click “add” after selecting it)

This script is now automatically loaded everywhere, in here you can keep variables (and functions too, but lets start simple first).

Now lets go to the practical part:
In this script you will add

var playerenergy = 0

to change this var from an outside script, for example on your player or bullet script, you can reference it like this:

if [bullet hits enemy]:
 Playervars.playerenergy += 1

(Playervars would be the name you chose on the autoload adding part)

Let me know if this works for you!

:bust_in_silhouette: Reply From: Suleymanov

Screenshots not visible.

Say you have “Script One” attached to the Node called “Object” with a variable name energy. You can call it from another script by typing $Object.energy

:bust_in_silhouette: Reply From: Mrpaolosarino
  1. create a script class that you extend within your desired place to use and use it’s func.
  2. preload a script and the access it’s function
  3. Create a export(script) var Script in which you can access it with much more versatility
  4. Autoload /Singleton