Set GameMode variables independently for each level

Hey all, I have the following:

Main Level with a custom GameMode. Inside the GM I have a few variables that define the level objectives. # of targets, ammo, playerpoints.
I have set these variables as “public and editable on each instance of this bp”.

When I load a new level, I would like to be able to use the same GM blueprint, but set these variables independently from the previous level.
So, for the new map, I set a different value for each of these variables. But this seems to change the original ones. As in there’s no instancing. It’s the same GM bp.

How do I actually instance these variables per level? Also, these variables don’t show up in the World Settings of the GM Section for each Level. Should they?

I have never done anything like this before, so any tips are greatly appreciated.

Level DB…
Create an actor… add the specific variables/values, drop in your level. Have the GM read those values.

Data Table…
Create a struct the defines your variables. Create a data table that uses that struct.
Row name can be level name. GM → get data table row name.

1 Like

WorldSettings is a different class. It is not exposed to blueprints, so you would have to expose some variables by creating a C++ class based on it and setting that as the default WorldSettingsClass in C++. In terms of things to do in C++, that’s on the easier end, so if you’re interested in doing things on that side it could be a good starting point. It’s also a sensible place to put such data into.

If you don’t want to do things in C++, you could use the Level Blueprint to set these variables inside the GameMode.

You could also use a DataTable asset or something which is basically a .csv file where you have put things like “levelname | number of targets | ammo | playerpoints” and then the gamemode reads the appropriate values based on the name.

Sigh, I swear… every single time I have the feeling I understand what’s going on in UE, there’s something knocking me down a notch. :slight_smile:
I read, watched, asked around… GameMode is where you keep your variables that define your game they said, never put anything in your Level BP they said…
Couple of questions before I change my code again for the nth time:
Remember, I’m still a complete noob, so be gentle…

So in my LevelBP, I define the variables, cast to GM and set them in there as well. And the rest of the code that increments/decrements/updates them can stay in the GM?
Is this the most common way? Is there a better way? Not proficient enough in C++ to handle things that way, (though, I am taking some classes at the moment).
Regarding the PlayerPoints variable… this would be carried across all levels. Would this be better Placed in the GameInstance then?
I am literally learning something new everyday. It’s an exciting path, but I do have to say… Epic could do a bit better on docs and tutorials. These forums though, definitely pick up the slack :+1:
Thank you guys for being here and be willing to share your knowledge. I do appreciate it.

I like that idea. I’ll see what I can do

When I start working with custom settings per map in the game mode I use the Lvl DB method. At least for testing and prototyping the functionality. Once I nail down all the settings/configs I need I move over to data table.

I never use Level BP.

you need to use the GameInstance

this object is always active and can manage other variables like this gamemode if needed. so if you want to switch a level and add a different gamemode you can do it in this GameInstance

you set a custom gameinstance in the project options section (where all the options about graphics and inputs and the rest are located)

GameInstance is very important but is not covered by basic to mid level intro tutorials as many of those that create tutorials are not real unreal devs.

I’m no dev either so I’ve struggled to understand what is all this gamemnode and when to use them correctly.

this guy shows an overview of how to create where to add the custom gameinstance

also don’t get crazy with saving all your variables and meshes and everything in that class because you will have a very bad time.

Now that I’ve mentioned this I think unreal didn’t put focus on this class because is too powerful and beginners would had use that instead of figuring out how to communicate between blueprints

Greatly appreciate all your input guys. More work to do I guess.

For the Data Table which Miscellaneous > Data Table, which row structure would you recommend for these 2 maybe 3 variables that need to be reset for each level??? @Rev0verDrive

You create your own row structure based on the variable types you need.

Right click in the content browser. Blueprints → Structure

Go the level db route until you get all your variables sorted.

If you want some data set depending on the level:

For data which only consists of numbers and not references, you can create a data table which you then read through.

If you need references, the most sensible place to put the variables imo is the World Settings class. But it is not exposed to blueprints. That said, creating another class based on WorldSettings and exposing some UPROPERTIES for blueprints is not all that difficult. If you want to avoid C++ at all costs, level blueprint works but is not as convenient.

Your GameMode will still have the data but they need to be set for each level.

If you want some data set depending on what happened in the last level:

GameInstance is a sensible class to work with especially if you only want to work with blueprints. It is also possible to create custom parameters for opening levels which you could then set the GameMode variables with as you open the level. I have personally had no need to do this and I’m not sure if it is exposed to blueprints.

@Rev0verDrive

OMG, I think i did it :smiley:

Ok, so I started with an actor bp… cast to gm and set the variables like so:

This worked really nice and I could see why it’s good for testing, but then I really wanted to go the Data Table route since it sounded more elegant…

I created a Struct:

Then used the struct to create a data table:

Then inside my GM bp, from the “Event BeginPlay” I set the variables:

This works beautifully. I love it!

Couple of questions…

  1. Is this how it should be done?
  2. My only concerns is "Get Current LevelName == Row Name (string to name conversion) is this always gonna work ok??? I couldn’t think of another way to get that info of the current level to match the row from the data table.

As I said, it works really nice, but wanted to run it by you guys. If you see any issues please let me know.
Otherwise, fantastic help. Thank you so much, again!

This should always work.

Best part here is Data tables are simply CSV files at heart. You can make changes to them on the fly. You can even have the server download them at runtime.

A lot of my games structure uses data tables. Complete weapon/vehicle configs etc. The DT allows us to change recoil, sway, fall off damage etc on the fly. Never having to touch code.