Changing a sprites variables when spawning

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

Hello and thank you for reading my question.

I have a set of sprites which have functions which allow it to change behavior depending on what a variable is set too. Its an integer and when set to 1 it behaves one way and when set to 2 or 3 it behaves another. This works fine but when I spawn a new enemy I cannot figure out how to change this variable in the spawned enemy.

I have set up some code but when it makes a change to the variable it changes it for all the enemies of that type on screen so they all behave the same way. I want to only change the variable for the new sprite I am spawning, does anyone know how to change a variable in a sprite they are spawning on screen?

Okay so I have a truly terrible solution to this. I am really hoping someone can help me as this was a single line of code in unity and its been terrible trying to do this very simple thing in godot.

My solution is I create an autoload script with a single variable in. I then have a script in my level which spawns a sprite and it then changes the variable in the autoload script from 0 to 1,2 or 3 as I desire. In the new sprite I then have an onready function which changes the variable in the sprites script to match the integer in the autoload script and change that back to 0. If the number in the autoload script is 0 and not a viable number the new sprite sends out a signal to say it failed to set up, then clears itself. The level script listens for this signal and if it hears it then it tries to set the sprite up again.

This is horrific and honestly if this is the only way to do something so simple in godot then I think I am going to give up on this game engine.

YuleTide | 2022-03-07 18:29

:bust_in_silhouette: Reply From: Inces

You just need to introduce this variable inside a script of this sprite. This will make it private for all instances of this sprite. If You already did this and changing value still affects all instances of a sprite - then You must have messed up a code to change this variable on spawning. If so - just show this code here, we will fix it.


Firstly thank you for your answer I appreciate it. I may not have been very clear with my question. The variable is in the sprites script. I have a var

var sp_behave = 1

which is at the top of the sprites script. What I need to do is be able to change this variable when I spawn the sprite. It always shows as 1 when the sprite is initially set up so I created a function to change the variable sp_behave after I have spawned the new sprite but this function doesnt work as expected as it changes all sprites of that type on screen not just the one I have just set up.

YuleTide | 2022-03-07 17:51

That is what I referred to in second part of my answer :slight_smile:
Function to change sp_behave was coded incorrectly. Show the code of this function please and a code of spawning new sprite :). Or don’t , as I have just realized your problem was solved :stuck_out_tongue:

Inces | 2022-03-08 13:12

Thanks I think I have worked out what I was doing wrong now, I was referencing the actual scene path and changing the variable in that. I realize with the answer from the other person that my other function was not referencing the new sprite but the original scene.

YuleTide | 2022-03-08 14:10

:bust_in_silhouette: Reply From: Gluon

My guess is you are not referencing the right thing when you are changing the variable. If you want to change a variable in a scene when you are spawning it you would use code like the below;

const enemy = preload("res://En.tscn")

var ene = enemy.instance()
ene.position = $Position2D.position
ene.speed = 100 = true

The ene.speed and would be variables in the instance and it would change the variables in that one spawned instance only.

Worked perfectly thank you!

YuleTide | 2022-03-08 06:37