Global variables not updating when calling from group

Godot Version


Hello, I am new to this game engine and I am encountering a problem.

I have a global script for mobs that has the speed and health variables and
they change every minute - as you can see in the last function.

var HEALTH = 3
var SPEED = 200

func return_variables_health():
return HEALTH

func return_variables_speed():
return SPEED

func _on_scene_data_update_model_slimes_minute_passed():
SPEED += 60

This is a model for every slime and I am intending to use it like so:
Here, the “health” and “SPEED” variables represent the health and speed of the slime.

I cannot directly call the slimes as they are in a group (exept call_group()), and they keep spawning after the changes to the global variables.

If you think that there is a better solution, the reason why I am doing this is so that I can change the 2 variables (of the future slimes that will spawn) to be a bit bigger.
What am I doing wrong? (after the minute function, the variables in the global script icrease, but when I use them, the health and speed of the slimes remains the same as in the beginning)

Once you set the variables in the _onready() function there is no connection between the global variables and the instances.
When the global variables change you need to then communicate with the instances and update their values.

I have no idea what the above sentence is trying to say.
So break it down:

I cannot directly call the slimes as they are in a group

If the slimes are in a group you can use something like:
var slimes_in_group:Array[Node] = get_tree().get_nodes_in_group("slimes")
That will add each slime as an element of the array. So you could access an individual slime by index:
var slimeA = slimes_in_group[n]

(exept call_group())

You don’t want all slimes impacted by changes to the variables?
If each slime is dependant on the global variables they will all be impacted by changes to the global variables.
If this is the case then you have a design flaw going on. We are going to need some clarification on this.

they keep spawning after the changes to the global variables.

In what way does this impact what you are trying to do?

1 Like

I wanted to mention that the slimes are in a group and not in the starting root, therefore I thought that I cannot access each one, individually, with code. I want every slime to have the new speed and health and I have been able to set those paramaters to the slimes that were already on the screen. To furether clarify, in my game the player has to survive waves and waves of slimes and after a minute passes I want every slime, that is on the screen or that spawns in the next waves to have the “new” speed and health. The last part is the one that I am having trouble with. Even though the variables in the global script are being updated, the slimes from the next waves still have the initial speed and health values.

If you can confirm that the autoload variables are increased then each new slime should get the new values.
The existing slimes will need to be specifically updated with the new values.
For that call_group() should work. If they are in the current scene then that group should be accessible via get_tree().call_group("slimes", "change_values_method")
Note that the slime script will need that method

func change_values_method()->void: 
    health = ModelForSlimes.return_variables_health()    
    SPEED = ModelForSlimes.return_variables_speed()

Also, you should be consistent with variable name style.
Generally all uppercase variable represents a constant.
Style Guide

Thank you for the advice! In the end I was able to solve the problem by seeing that the object ID for the global script was different to the one to the “self” script, so I instead increased the numbers directly:

I don’t know why this happens, but if it works, it works.

It can happen if you give the autoload a class name:
class_name ModelForSlimes

and then instantiate an instance of that class.
This would end up with two instances of ModelForSlimes because the autoload is already the first instance.
It could be that each slime is creating its own instance of ModelForSlimes and I think that would be a problem.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.