So I’m making a platformer where you’re freezing and you have to get to a campfire Quickly. to that I have a “Temperature” Scene wich looks like this:
And a “Campfire” Scene wich looks like this:
I always import both scenes into the level, and everything worked great so far (I’ll leave the code at the bottom)
But for some Reason when I made a new Level and did everything the same way, The Capmfire doesn’t increase the temperature value anymore (It still does in the old level)
Code in TempDisp:
extends Node2D
var temp = 100
@onready var label: Label = $TempLabel
@onready var alarm: ColorRect = $"../Alarm"
@onready var background: ColorRect = $Background
var Under15 = false
var campfireScene = preload("res://scenes/campfire.tscn").instantiate()
var campfire = campfireScene.get_node("CampfireControll")
signal temp_zero
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
campfireScene.visible = false
add_child(campfireScene)
campfire.connect("tempGoUp", Callable(self, "_On_Temp_Go_Up"))
print("Instanciated")
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
temp -= 0.05 # 0.005 is a good value
background.size.y = temp
if label:
label.text = str(int(temp))
if temp <= 0.005:
emit_signal("temp_zero")
if temp >= 99:
temp -= 0.05
if temp <= 10:
Under15 = true
else: if temp >= 10:
Under15 = false
if Under15 == true:
alarm.visible = true
else: if Under15 == false:
alarm.visible = false
func _On_Temp_Go_Up():
temp += 0.5
print("Signal Receivd")
Code in CampfireControll
extends Node2D
var tempUp = false
signal tempGoUp
@onready var anim: AnimatedSprite2D = $AnimatedSprite2D
func _ready() -> void:
anim.play("default")
func _process(_delta: float) -> void:
if tempUp == true:
emit_signal("tempGoUp")
print("Signal Emitted")
func _on_area_2d_body_entered(body: Node2D) -> void:
if body.name == "Player":
print("Player Entered Capmfie Zone")
tempUp = true
func _on_area_2d_body_exited(body: Node2D) -> void:
if body.name == "Player":
tempUp = false
what do you mean?
The Capmfire doesn’t increase the temperature value anymore
to the label or the var temp
and one more thing why the
func _on_Temp_Go_Up():
temp += 0.5
it will only add one time 0.5 or you call it in
func _process():
to continue add value
ok let me explan the signal form campfire will active it ones and will not add more of then 0.5 but you can make it bool to is player in fire then active in the process function the temp Up var and if player exit area2d then stop adding that value to temp Up
what do you mean?
The Capmfire doesn’t increase the temperature value anymore
to the label or the var temp
and one more thing why the
func _on_Temp_Go_Up():
temp += 0.5
it will only add one time 0.5 or you call it in
func _process():
to continue add value
ok let me explan the signal form campfire will active it ones and will not add more of then 0.5 but you can make it bool to is player in fire then active in the process function the temp Up var and if player exit area2d then stop adding that value to temp Up
in both code file in the process func just type
in file TempDisp and one in
print_debug(temp)
campfireControll file in the process function
print_debug(tempUp)
what you should see is
in file TempDisp
the value of temp 0.5 repeats itself in every frame
in file campfireControll
you should see the true or false value of the boolen also repeats it self every frame
just example for what it will shows
0.05
the file path TempDisp
false
the file path campefireControll
here if player enter camp fire while game run
0.1
the file path TempDisp
true
the file path campefireControll
and can you take image of it| it will more clear and ez to debug here
HI @Morris . It would be great if you provided a .zip minimal project that will allow us to easily reproduce your problem.
I guess the following is worth trying to fix your issue:
when instantiating scene, it is good to set the owner of the child node for some benefits in editor, etc; see node property: owner
if you always instantiate the same scene (campfireScene is not exported) on ready, then maybe it makes sense to add the scene in the visual editor?
In the meantime, there are some suggestions you could follow in order to improve readability and maintainability of your code. They are not solution to your problem but should improve your project and make it less prone to errors.
These are as follow:
I think it’s better to connect signals to functions using the properties instead of names, e.g.
campfire.connect("tempGoUp", Callable(self, "_On_Temp_Go_Up"))
# vs
campfire.tempGoUp.connect(_On_Temp_Go_Up)
and has some editor benefits, like error notification if the connected function name is misspelled,
calculations in _process(delta: float) function should use delta value as _process function is not guaranteed to be called with constant interval.
Instead of estimating temp change, you could define:
var temp_change_speed: float = 1.0
func _process(delta: float) -> void:
temp -= temp_change_speed * delta
... # now if your game freezes for some time, delta will reflect time since last update
signal emitting can be done using the signal instead of its name:
emit_signal("temp_zero")
# vs less error prone:
temp_zero.emit()
try to stick to one naming convention for easier code readability (I guess snake case for signal, variable and func names seems to be most consistent with Godot language):
campfireControll - camel case
temp_zero - snake case
_On_Temp_Go_Up - combination of both
use string formatting for fine grained control on what you display:
label.text = str(int(temp))
# vs
label.text = "%d" % temp
# or
label.text = "%2.0f" % temp