Topic was automatically imported from the old Question2Answer platform.
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?
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)
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