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))
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.
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
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.
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.
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()
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().