Hello, so I’m working on a platformer and i was giving a small immunity time to my character after taking damage, here is the enemy’s script:
extends Area2D
@onready var immunity_timer: Timer = %immunity_timer
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func _on_body_entered(body: Node2D) -> void:
if body.is_in_group("player") and GlobalLevelData.immunity == false and immunity_timer.is_stopped():
GlobalLevelData.resistance -= 1
GlobalLevelData.immunity = true
immunity_timer.wait_time = 2
immunity_timer.start()
print("not immune")
else:
print("immune")
print("Timer started: ", immunity_timer.time_left)
func _on_immunity_timer_timeout() -> void:
GlobalLevelData.immunity = false
I tried to make sure that the timer started and had the right duration but it does NOT seem to start as it prints
Now this game has the special factor of reloading the scene / level when the player takes a specific amount of damage (which at the start of the level is 1, this is why I’m handling the data on a Global variable called GlobalLevelData)
in fact this is the level’s script
extends Node2D
@onready var loop_man: CharacterBody2D = %LoopMan
@onready var timer_label: Label = $CanvasLayer/timer_label
@onready var level_timer: Timer = $Level_Timer
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
if GlobalLevelData.first_time == true:
GlobalLevelData.resistance = 1
GlobalLevelData.total_resistance = 1
GlobalLevelData.gems = 0
GlobalLevelData.acquired_speed = 0
GlobalLevelData.first_time = false
GlobalLevelData.acquired_lives = 0
GlobalLevelData.acquired_resistance = 0
GlobalLevelData.jump_boost = 0
else:
GlobalLevelData.total_resistance = 1
GlobalLevelData.resistance = 1
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
timer_label.text = str(int(level_timer.time_left))
func _on_level_timer_timeout() -> void:
get_tree().reload_current_scene()
I don’t know if it is a problem with the reloading of the scene but it pretty much should not be because I handle the immunity globally too.
Update: I tried moving the Immunity logic to a scene on autoload with
extends Node2D
@onready var timer: Timer = %Timer
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if GlobalLevelData.immunity == true and timer.is_stopped():
timer.start()
func _on_timer_timeout() -> void:
GlobalLevelData.immunity = false