0 votes

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

Godot version v3.2.3
in Engine by (17 points)

3 Answers

+2 votes

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!

by (309 points)
edited by
0 votes

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

by (335 points)
0 votes
  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
by (385 points)
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.