onClick tigger timestop in Global Autoload with timer

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lokineo

trying to press “c” key then freeze the enemy and then after timeout enemy continuous to move again by using autoload. I am able get the enemy freeze but i cannot set timestopfreeze = false again, something wrong with my timer in Global autoload script and not sure what went wrong. If you can point how my timer setup wrong ,
hope i explained it well and any help will be most appropriate

cheers

Player script

onready var globleEffect = get_node("/root/GlobleEffect")
    
func _integrate_forces(_s):
    	if Input.is_action_just_pressed("Attack"):
    		globleEffect.timestopfreeze = true
    		globleEffect.timeStopFreezeTimer.start()

Enemy script

func _process(_delta):
	if globleEffect.timestopfreeze == false:
		set_deferred("mode", RigidBody2D.MODE_CHARACTER)
#		print("iam rigidmode")
	else:
		set_deferred("mode", RigidBody2D.MODE_STATIC)
#		print("iam static")

Autoload global script

extends Node
var timestopfreeze = false
var timeStopFreezeTimer:Timer

func _ready():
	timeStopFreezeTimer = Timer.new()
	timeStopFreezeTimer.set_wait_time(1)
	timeStopFreezeTimer.one_shot = true
	add_child(timeStopFreezeTimer)
	
func _process(_delta):
	if timeStopFreezeTimer.stop():
		timestopfreeze = false
:bust_in_silhouette: Reply From: Gluon

Your problem is that the autoload script will only run the _ready() function on start up not when you need it. You would be better off putting something like this in the enemy script

var timeStopFreezeTimer:Timer

func _process(_delta):
    if globalEffect.timestopfreeze == true:
        _timefreeze()

func _timefreeze():
    set_deferred("mode", RigidBody2D.MODE_STATIC)
    timeStopFreezeTimer = Timer.new()
    timeStopFreezeTimer.set_wait_time(1)
    timeStopFreezeTimer.one_shot = true
    timeStopFreezeTimer.connect("timeout", self, "_restart")
    add_child(timeStopFreezeTimer)

func _restart():
    set_deferred("mode", RigidBody2D.MODE_CHARACTER)
    globalEffect.timestopfreeze = false

and then just have the global script contain the variable timestopfreeze

sorry , the code above is not working, i am not sure why but I get it working on putting timer with yield in player script instead

Nevertheless, i want to thank you for time to help me

Cheers , have a nice day

lokineo | 2022-11-21 12:59

:bust_in_silhouette: Reply From: lokineo

Working Script , instead doing the timer in autoload, do it in player script with yield timer

Player script

onready var globleEffect = get_node("/root/GlobleEffect")

func _process(_delta):
	if Input.is_action_just_pressed("Attack"):
		globleEffect.timestopfreeze = true
		var t = Timer.new()
		t.set_wait_time(1)
		t.set_one_shot(true)
		self.add_child(t)
		t.start()
		yield(t, "timeout")
		globleEffect.timestopfreeze = false

Autoload global script

var timestopfreeze = false

Enemy script

func _process(_delta):
	if globleEffect.timestopfreeze == true:
		set_deferred("mode", RigidBody2D.MODE_STATIC)
	if globleEffect.timestopfreeze == false:
		set_deferred("mode", RigidBody2D.MODE_CHARACTER)