Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Global variable or something else?

Good afternoon. I'm developing a simple Tower Defense. Got to create health points (taken away when creeps reach the base) and coins (replenished for killing creeps, are taken away when building towers). Here, what interests: where usually such variables declare? After all, in fact, in the tower defence there is no player .. we just move the camera (for her I have a separate GameModeBase and controller).

I think that this can be done as follows:

  1. Use global variables in a separate .h file

  2. Create a class for them (But what kind of class is this, this is no actor, right?)

  3. I heard about GameModeBase, maybe in this class?

  4. Something else?

Thanks for help!

Product Version: UE 4.18
more ▼

asked Feb 18 '18 at 02:36 PM in C++ Programming

avatar image

3 1 4

avatar image erik321123 Feb 19 '18 at 08:11 PM

I would add it to your gamemode, accesable from every UObject, yet still easy to follow for others

avatar image Dune Feb 20 '18 at 03:20 PM

Erik is right, if that is your setup then your game mode should be used to keep the 'game score' so to speak.

You can access the GameMode from any other class (GetGameMode in blueprints) or:

 AMyGameMode* MyGameMode = Cast<AMyGameMode>(UGameplayStatics::GetGameMode(this));
(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

It's not recommended ot use global varbales in UE4 and not to mention UE4 reflection system does not support global variables. In case of UObject pointers in global variable you facing risk of having invalid pointer if object gets destroyed and you not null that varable after that, UE4 manages that for you if you use UPROPERTY(). Your object also can be garbage cleaned as UE4 won't see you referencing it in global varable

What you trying to do should be in your GameMode class (whatever you use Base version or not). Heres quick list on where you should place data for what thing:

GameInstance - Data for entire game, most persistent and most global class in your arsenal. There might be different classes as persistent, but by assumptions of engine design for anything game related you should use this class. best place for persistent stats for your tower defense that effect multiple levels

GameMode - Data for single match, it resets on level changes or level restarts. This is best palce for your health information, as health in tower defense is for single match and should reset on level change or restart, alternativly you could use PlayerController, but if your game is single player then it makes no diffrence, or else players should share health then you should use GameMode.

PlayerController - Data for each single player, data here will be persistent even if pawn of player dies. Same as GameMode it resets on level change and level restart as well as player rejoining the game as it resets it PlayerController.

Pawn (any actor on level really) - Data for pawn, it get destroyed together with pawn this includes level restart and level switch ofcorse as pawn gets destroyed with the world.

more ▼

answered Feb 21 '18 at 05:14 AM

avatar image

37.2k 936 172 1116

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question