Why doesn't it stop printing the frameTime?

Hello, I’m new to Godot and I’m having trouble with a class I’ve created so basically I have this pause method that set paused to true, and in the proccess method if it’s paused I make it return so nothing prints but this just is not working for some odd reason?

Main

extends Node

var ObjectAnimation = preload("res://ObjectAnimation.gd")

# Called when the node enters the scene tree for the first time.
var file = FileAccess.open("res://TurnAround.txt", FileAccess.READ)
var string = file.get_as_text()
var anim = ObjectAnimation.new()

func _ready():
	get_tree().create_timer(2).timeout

	anim._pause()

func _process(delta):
	pass

Class

extends Node
class_name ObjectAnimation

var animationData = {}
var FPS = 30
var frameTime = 0
var paused = false

func _ready():
   pass

func _pause():
   self.paused = true

func _setString(s : String):
   self.animationData = JSON.parse_string(s)

func _process(delta: float):
   if self.paused:
      print("PAUSED")
      return

   frameTime += (delta * FPS)
   print(ceil(self.frameTime))

Your sample doesn’t add anim as a child to anything. Does it only print ceil(self.frameTime) or does it never print anything?

Yeah it only prints the ceiled frameTime, im declaring the object in another script I dont get why its not working???

image

I notice your editing script doesn’t add anim as a child, I would try that first. Nodes without a parent act strange.

func _ready() -> void:
	add_child(anim)

	get_tree().create_timer(2).timeout

	anim._pause()

Yeah that’s like so weird it still just keeps printing the frame time, I have no clue why this is being so difficult

extends Node

var ObjectAnimation = preload("res://ObjectAnimation.gd")

var file = FileAccess.open("res://TurnAround.txt", FileAccess.READ)
var string = file.get_as_text()
var anim = ObjectAnimation.new()

func _ready() -> void:
	add_child(anim)

	await get_tree().create_timer(5).timeout

	anim._pause()

	pass

func _process(delta):
	pass

Well another trick you could do is set_process(false) instead of pausing. It should have the same effect you are going for, stopping the _process function. Hopefully works though.

func _pause() -> void:
    set_process(false)
    self.paused = true

Yeah I just tried that and it still isn’t working, but there is something I just found out…

For some reason, doing it in the ready method inside of the Class itself works, but of course I don’t want it to be in the ready method I wanna use it outside of it in another script

extends Node
class_name ObjectAnimation

var animationData = {}
var FPS = 30
var frameTime = 0
var paused = false

func _ready():
   await get_tree().create_timer(2).timeout

   self._pause()

func _pause():
   set_process(false)
   self.paused = true

func _setString(s : String):
   self.animationData = JSON.parse_string(s)

func _process(delta: float):
   if self.paused:
      return

   self.frameTime += (delta * self.FPS)
   print(frameTime)

Very strange! Let’s try printing after the create_timer timesout, maybe that is being halted for some reason. Also could be that _pause is a virtual method that should not be overriden; it is a pretty common engine term. Hopefully not so bad to use set_process(false) directly too

func _ready() -> void:
	add_child(anim)

	await get_tree().create_timer(5).timeout

	anim.set_process(false)

It still keeps printing frameTime, I don’t know if maybe I’m not setting up OOP correctly or what

extends Node

var ObjectAnimation = preload("res://ObjectAnimation.gd")

func _ready() -> void:
	var file = FileAccess.open("res://TurnAround.txt", FileAccess.READ)
	var string = file.get_as_text()
	var anim = ObjectAnimation.new()
	add_child(anim)

	await get_tree().create_timer(2).timeout

	anim.set_process(false)

func _process(delta):
	pass

Scene Hierachy

image

1 Like

Now that is strange too. It should be a child of Main. Is there any other part of the script interfering with the ObjectAnimation or Main. Is the game paused but the nodes set to process_mode alway or whenpaused?

Nope not at all, also I just made ObjectAnimation a child of Main and that still didn’t change the behavior of it at all, i havent even touched the proccess_mode or anything like that I just created those two scripts and thats really about it

I can’t find any videos or anything of anyone doing something like this

Oh my. So a new ObjectAnimation is being created, that’s what ObjectAnimation.new() is giving you. If you wanted to reference the existing ObjectAnimation you should try a @export variable like so

extends Node

@export var anim: ObjectAnimation
func _ready() -> void:
	var file = FileAccess.open("res://TurnAround.txt", FileAccess.READ)
	var text = file.get_as_text()
	anim._set_string(text)

	await get_tree().create_timer(2).timeout

	anim.set_process(false)

Otherwise just delete the existing ObjectAnimation.

You can view the running program in the same scene tree view, but click on “remote” while playing. You might have noticed twice as many prints up until one of them was paused.

1 Like

Thank you, this worked but I do have a few questions though

  1. Will this allow me to have multiple scripts that can create an object animation object?

  2. Why is this happening when it reaches the end of the 2 seconds?

1 Like

Your sample is missing the anim._set_string(string) before the timeout, it should fail instantly!

The exported variable is now a property that needs to be set in the editor’s inspector.


Answering back to 1 depends on how you want to set it up, you mention “scripts that can create an object animation object” and the export does not handle creation, just uses an existing node. The ObjectAnimation.new() method allows you to actually create new ones, since they are basic Nodes I see no reason they can’t be created and added as children among other scripts. I like exports though, potentially easier in-editor set up.

Also I now noticed you set a class_name for the ObjectAnimation, this means you do not need to preload the script to use the class for ObjectAnimation is now globally defined.

1 Like

Yeah I tried it without export and it also worked but what’s just really confusing is why it’s saying the _setString doesnt exist?

It’s quite literally right here

Alright I just had to linked the ObjectAnimation module and it worked but if I didnt want use @export

it would just bring me back to the problem of not being able to even pause the animation outside the class, Godot is so weird man

YES LETS GO I FIGURED IT OUT on how to do it without export okay so basically you just gotta do this

extends Node

var anim = load("res://ObjectAnimation.gd").new()

func _ready() -> void:
	var file = FileAccess.open("res://TurnAround.txt", FileAccess.READ)
	var string = file.get_as_text()

	await get_tree().create_timer(2).timeout

	anim._pause()

func _process(delta):
	pass

No errors once the animation is paused and also no need to link every single animation object your gonna need inside of Godot!

Edit: Yeah that doesnt work :neutral_face:

The problem without @export is that ObjectAnimation node in the scene tree is a totally different node from the one Main creates and adds as a child. You would be pausing only one of them. If you want to reference a node that already exists in the scene tree you must use @export or the dollar sign notation, the latter usually reserved for children.

extends Node

var my_child = $ChildNode/GrandChild

Your initial problem was creating a new ObjectAnimation, while another existed on it’s own, you believed there was one, or they were linked. It just took a while to see your scene tree so I couldn’t decern the actual issue.


You should delete the ObjectAnimation in your scene tree and try this script

extends Node

var anim := ObjectAnimation.new()

func _ready() -> void:
	var file = FileAccess.open("res://TurnAround.txt", FileAccess.READ)
	var string: String = file.get_as_text()

	anim._setString(string)
	add_child(anim)

	await get_tree().create_timer(2).timeout

	anim._pause()
1 Like

That’s awesome, this worked but could you explain what the := means when you declared anim and also I’m guessing when you add_child it just basically instantiates it?

Type can be declared after the variable name using colon. Without the type inbetween a explicit type is required, use to help with auto complete but Godot has gotten better with that.

var variable_name: VariableType = "Varibale value!"

Add child will run the _ready() function, before that is also _enter_tree().

1 Like