`Hi. I posted this originally on stackoverflow but it got like 7 views after 4 hours and 0 responses
Im new to Godot and I’m trying to build a simple farming game. My question is how can I have some codes that run across the scenes.
I’m trying to learn different ways to do it so I can decide which one is best for this game and later games I may build.
So lets say I have a scene that is the farm and player can plant vegetables in the farm and water them etc. but then the player may leave the scene and go like shopping etc.
But while he is on another scene I want to have a code running that make changes to the farm scene data, like vegetables etc. BUT also I want this code to be accessible and running across scenes. (I know I can use global variables by making a globals.gd autoload)
But what about having some code running across the scenes? I would like to know the solution for general situation/different situations.
I did some searching but I could only find about 1 solution and that is having 1 scene for the whole game and changing scenes would be like loading different scenes inside the main scene. Any other/better solution?
Autoloads should work, what do you mean by “code running across the scenes”? A specific problem would enlighten.
Autoloads can be either a Scene or a Script. In the case of a Scene the whole scene is loaded as a child of root, scripts are loaded as a child of a new Node on root, both can make use of _process and signals.
Thanks for your reply. Well by “code running across scenes” can be codes that are gonna be executed in all scenes.
codes for handling character animations, interactions etc
codes for handling game state
I know I can define global variables to hold game state but I want to be able to have some code always running on background to handle/update game state and act upon it.
Some examples:
a. message handling, like player inbox mail, system notifications of events happening like lets say player has put an item for sale and it has been sold so a new notification should appear so the notification icon on gui should be updated.
b. or code that should sync/update game state with server and so on
c. codes that are gonna be repeated like the code that would handle clicking on messages/notifications icon that would open window and fetch message/notification from server
i assume for c. i can simply create invisible nodes and attach scripts to them and copy paste those nodes in each scene. but what about a,b?
In other world I want some code that always run in background across scenes regardless of which scene we are in.
But I think having a global scene autoloaded on every scene could be a good solution. and that global scene can have some scripts attached to it running across scenes. right?
@rpc and/or the MultiplayerSynchronizer and MultiplayerSpawner nodes, usually not on an Autoload. unless you mean a HTTP request?
Godot’s multliplayer does a great job avoiding extra Autoloads.
This sounds like a Scene or a “component”, you can attach the same script to multiple nodes, and instantiate a scene multiple times within another scene. So you can have multiple notification buttons with the same behavior by attaching the same script to them, or a new Node with the same script operating on the button.
Notifications also benefit from signals, doesn’t have to be an Autoload, depends on your game.
It sounds like you do want an Autoload with at least one signal.
For my first godot game there’s a couple of tricks I’ve used for cross-scene code.
Firstly I have a Main autoload, which stores general state across all scenes, such as the menu system and the in-game scenes.
Secondly I have a GameManger autoload which gets setup when the player starts playing a game, and maintains game state across the different scenes.
Finally I have an EventBus autoload, which defines numerous signals, such as game_started, player_died, etc. The GameManager and the game’s scenes then emit or listen for those signals, providing loose coupling across the game, and making it easy to create new scenes that can plug-in to existing events.
What I don’t have is any systems that run across game scenes (e.g for notifications), but if I did I’d likely hang them off the GameManager, or have them listening for events on the EventBus they could then respond to.