What’s the best way to handle fights on a separate map?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By HerrKiosk

Hey there,

My question is what the best approach would be to solving the following:

I have a world map where the character can move around. When the character hits an enemy on the world map, a fight starts. The fight takes place on a separate map and time pauses on the world map for the duration of the time. After the end of the fight, the player character returns to the world map in its original position and time runs again.
Pretty much like Pokémon, for example.

The player character‘s stats, looks, etc. should be taken over to the fight map and back. If the player loses health in the fight, that must be reflected on the world map afterwards.

The entire game is 2D top-down (world and fight map)

First I considered having the fight map outside the world map in the same scene. I could move the player, enemy, and camera to the fight and afterwards back to the world. The issue here is that I don’t know how to stop time on the world map, but keep it running in the fight.

Another consideration was swapping world and fight maps as separate scenes under the player. How would I ensure that player and enemy stats as well as world level state are kept?

I am not looking for a complete recipe or the specific code how to do it. I am looking for an answer to how to even approach this to begin with.

Thank you so much for your time!

Maybe what would help me is knowing the name of this concept so I can search for it online. Current search results are useless :frowning:

HerrKiosk | 2021-08-07 06:58

:bust_in_silhouette: Reply From: TheJokingJack

Global variables are usually variables that can be accessed throughout the program. Although Godot does not support singletons or global variables, you can use autoloads for these tasks instead.

Personally, I would suggest swapping scenes between the fights and the world map. That would keep everything from being too cluttered in your main scene. To save stats so that you can access this information from any scene, you can use an autoload.

Here’s a similar question:

And some tutorials:

You can save variables, lists & dictionaries, and even functions. From what I understand, autoloads are always loaded first, so that any scene can access them. I suggest viewing the docs from above for a more detailed explanation.

So in the autoload script, you can define a variable…

var player_health = 8

And to access your variables from the autoload:


Replace AUTOLOAD_SCRIPT_NAME with the name of your autoload scene. You can name it PLAYER_VARIABLES, or GAME_GLOBALS, for example.

TheJokingJack | 2021-08-09 17:19

Thanks a lot for your answer. I have a follow-up question:

So I will store data in an autoload script. Let’s say the player character can have different clothing. For simplicity: jeans or shorts in blue or black. The autoload script will function as a data container and store, for example, „black“ and „jeans“.

Now when I want to render the character, I need to build the player nodes in the scene based on the singleton data container.

To me as a novice that sounds like tedious overhead. Wouldn’t it be easier to have the player scene up-to-date with the latest pants all the time? Now I need to duplicate that information: data and visuals. Using instead Godot‘s „everything as nodes“ philosophy, it would appear much simpler to handle.
Instead, with autoload, different weapons with different stats as another example can no longer be their contained nodes. Data must be duplicated into the singleton.

On the other hand, maybe it will be easier to save the game if I have everything in data containers already?

What are your thoughts?
Thank you for your time!

HerrKiosk | 2021-08-09 19:03

So basically, you’re saying that duplicating information for every single data on your character would be tedious?

To be honest, I’m also a novice, so I do not know the best way to do this.

On the other hand, you could save data into a file, and then extract the information to be used again. That’s definitely possible (but to be honest, I think that would be tedious too).

You could also just get the info using get_node() when the node is about to enter and exit the main scene.

Honestly, I have no idea what to say about this. I do agree with the 'everything as nodes" philosophy, in which everything is contained in their respective nodes. Saves headaches for a lot of us.

If it were me, as a complete novice, I would save information that does not need much manipulating. For example:

If I were to save player data such as health in the autoload, when the player is damaged, I would need to calculate the damage taken in either the autoload or the player scene. If I were to calculate it in the autoload, I would then have to update the player health variable in the player’s own scene. If I get rid of the player data as a whole, then I would have to get the information from the autoload, calculate the damage taken, update the variable, and then update the GUI and change the player or GUI behaviour, as needed. Tedious, just like you said.

On the other hand, autoload would be useful for information that would not be manipulated often, such as preferences, like hiding controls during a game, dark theme or light theme, viewport area, things like that.

With data like a player’s health constantly being manipulated, I would think it would be better to contain it to it’s own node.

With information that you exclude from the autoload, you could also just get the info using get_node() when the node is about to enter and exit the main scene.

Sorry for the long answer, got caught up in writing. Feel free to ask more questions if necessary. And don’t take my word for this. If needed, you should probably create a separate question based on this. “What information should I store in autoload?”

TheJokingJack | 2021-08-09 19:18

Maybe you should look up some game dev videos on Youtube, see how they structure their code. Or search up how to structure code in Godot?

Found some interesting articles:
Best practices — Godot Engine (3.1) documentation in English
Reddit - Dive into anything

TheJokingJack | 2021-08-09 19:26

Thanks! I will have a look. I’ll report back if I find something solid :slight_smile:

HerrKiosk | 2021-08-09 19:32