Expediency of using static class and autoload in godot c#
-
The game has a time system. Now it is implemented as follows: there is a static TimeManager class, which contains a static DateTime variable and a method GetTime to get string representation of the DateTime var. There is also a TimeHandler node that I want to add to autoload so that all other handlers can connect to its signals. Or to implement Signals singleton pattern...
I have other similar systems. Two questions:
- Is this static class necessary, or should the data be stuffed into the node for autoload?
- If in a node, then it will load in all scenes. And I need time for each level to load my own. And this year's work is not needed in the menu.
What is the best thing to do?
-
So you have
TimeManager
classclass_name TimeManager ... static var DateTime static func GetTime()
Then you want
TimeHandler
node because you can't use signals on thatTimeManager
class you autoload it. So what's the difference between static and autoload in Godot? The main difference betweenstatic
andautoload
is where it's located. Static located in the class itself, autoload is located in the SceneTree giving you access to all the nodes in your scene.- Static class isn't necessary, nor the autoloads. Pick whatever make sense to you.
- Yes, if you set
TimeHandler
as an autoload, it will be loaded on all scenes anywhere you go. So this might not be ideal if you don't always needTimeHandler
Hard choice. Ditching TimeManager statics, and just shove everything in an Autoload node might be the easiest.
-
@altria So If I refuse from statics and create one node with funcs and vars and then I create an additional in Autoload node with signals for interactive with those nodes? Will it be correct?
-
@altria Thanks a lot, I've tried to implement your advice and everything works correctly now
-