0 votes

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
Godot version 3.5.1
in Engine by (19 points)

2 Answers

0 votes
Best answer

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)
by (19 points)
0 votes

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

by (3,321 points)
edited by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.