Why my variable isn't updated?

Godot Version

4.2.2

Question

Hey, I want to upload a variable multiplier when a scene is ready. I made a signal that upload multiplier value, but when other signal use this variable, the upload doesn’t have effect (returns to its original value):

#Script habitat
class_name habitat
extends Control

@onready var lvl: float = 1.2
@onready var manaAdded = 8 #coin 1

signal level(lvl)
signal addMana(manaAdded)

@onready var time = 3

# Called when the node enters the scene tree for the first time.
func _ready():
	level.emit(lvl) #upload multiplier variable
	%timer_habitat.start(time)


func _on_timer_habitat_timeout():
	addMana.emit(manaAdded) #add a value
--------------------------------------------------------

#Script coin 1
class_name coin_1
extends Node

@onready var multiplier: float = 1.0

(irrelevant code)

func _on_container_habitat_level(lvl): #signal that upload multiplier
	multiplier = lvl
	print(multiplier) #print 1.2


func _on_container_habitat_add_mana(manaAdded): #signal that add a value
	print(multiplier) #print 1
	var lvl = roundi(manaAdded * multiplier)
	add(array_num(str(lvl)))

Signal level(lvl) is emitted first, so it’s supposed to change multiplier before signal addMana(manaAdded) use it, but it doesn’t. I try to make multiplier a setter, but nothing change.

If anyone know why, please let me know. Good day

whats the hierachy of these nodes? the multiplier default-value from coin_1 gets only set when its ready → when the signal gets recieved before ready is called, it will get overwritten by onready-default-value

Sorry for the late response. This is the hierarchy:

It’s like you said, the value is being overwritten. If I swap its place, the problem is fixed. Thanks!

1 Like

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