0 votes

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?

Godot version 3.4
in Engine by (51 points)

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.

2 Answers

0 votes
Best answer

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
ene.foo = true
add_child(ene)

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

by (3,041 points)
selected by

Worked perfectly thank you!

0 votes

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.

by (7,753 points)

Hello

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.

That is what I referred to in second part of my answer :)
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 :P

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.

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.