x

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"

How to use game instance to transfer a spawned player between levels

I followed this tutorial by Unreal Engine: https://www.youtube.com/watch?v=ZOc9fV6Sap4&t=196s

So I have a 4 player couch co op game where you can drop in and drop out, on loading the next level, everyone has to respawn (drop in) again. How do i carry over if they are already playing or not. I know its has to do with storing a game instance, but not sure how to get the variable of players spawned.

Many thanks in advance.

Product Version: UE 4.19
Tags:
more ▼

asked Apr 19 '18 at 05:08 PM in Blueprint Scripting

avatar image

Darrel Leigh
3 1 7

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

Gameinstance is a class that survives for the entire instance of the game.

Now, lets say you have a variable "IsAlive" stored in Game instance. Create a PlayerInfo Map (Dictionary) of PlayerBP and PlayerData struct.

The playerdata blueprint would contain data like IsAlive, Score, Coins etc.

When you go to new level, iterate the through the PlayerInfo Map and spawn the player based on the data (as the data would contain if the player is alive or not)

more ▼

answered Apr 19 '18 at 05:16 PM

avatar image

shrikky
218 6 9 12

avatar image Darrel Leigh Apr 19 '18 at 08:35 PM

I'm finding it hard to understand. Could you perhaps elaborate

avatar image shrikky Apr 19 '18 at 09:43 PM

So game instance is eternal for the application. As for as the exe stays alive , the game instance will be running. The gameinstance is not destroyed when maps are changed. So any kind of persistent data is stored in Game instance.

For example,
Step 1 : Create a Structure called player data. Put all the player related variables there. For example you want to track if a player was alive or not when the scene changes. So create a bool in that struct called IsAlive. Also create an integer value in the struct called score (just for understanding).

Step 2: In the gameinstance class. Create a Map called PlayerInfo ( dictionary) that has the Player Actor BP as the Key and the PlayerData structure as the value

Step3: When a player dies, GetGameInstance -> cast it into yoru gameinstance. Now grab the dictionary and update the struct of the Player. Lets say Player 2 died. Grab the Playerinfo and modify the Value for the Player 2 key.

Step4: When a new map is loaded. Grab gameinstance -> cast it into your gameinstance -> get the map. Now iterate through all the player (Keys) and get their Value which is the player data. Now if the data has the ISALIVE set to true, it means the playr didn't die in the previous scene, so you can spawn him. If the IsAlive is false, don't spawn him.

avatar image Darrel Leigh Apr 23 '18 at 04:53 PM

Thank you for your help, after reading through what you said a few times and a few experiments, I'm getting closer and closer to what I want. Much appreciated.

avatar image shrikky Apr 23 '18 at 05:14 PM

Glad to help, if you are still confused, think of it like gameinstance is like a singloeton for the entire application. It is always alive. So the state of all the variables that get destroyed during scene change can be saved in gameinstance and loaded again when the new level is loaded.

(comments are locked)
10|2000 characters needed characters left
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