So I have this system I made to be able to switch between playable character A and playable character B, by teleporting the new one in the same place of the older one… The thing is that all of the playable characters can only have one instance (so I can’t have 2 characters A), which I saw that to do this you need a game instance with the A and B characters as variables and initialize them in their own BeginPlay Event.
After saying all that the question is if it’s better to hide “important” (by important I mean that only 1 can exist in the world) 3D Models or make them spawn in only WHEN I switch character and of course make the other character despawn? Of course this last is better (even tho my game is super low poly, so it probably isn’t even that bad), but I don’t know how it would interact with the game instance, like would despawning the character A, also destroy the variable in the game instance? If so would respawning it just refill it in like nothing ever happened?
Bonus question: Do Different Levels mess up with the game instance? Like if I open a new level, do all the BeginPlays of the new level start when I load that new level, or do every levels’ BeginPlay start the moment I begin play in level 1? And even if every levels’ BeginPlay only start when you open that level, will the character A that exist in level 1, be the same that exists in the new level, to not mess up with the game instance again?
Sorry if I can’t explain myself correctly, but I’m very confused about this matter in particular, so yeah, thanks in advance for the help!
Both options have a right to exist, but in certain situations.
If a player can change a character very often, then deleting and creating a new one is worse than simply hiding.
It is important to remember that after deletion, objects will remain in memory for some time until the garbage collector clears the memory.
However, if your character needs to be “reset” after a change, then when hiding, you will also have to reset the necessary values manually.
Game instance is created when the game is launched and exists as long as the game is running.
When loading/unloading a level, all actors in the level, as well as the player controller, player pawn, camera manager are also loaded (recreated) and unloaded.
Except when Seamless Travel is enabled in game mode. But from personal experience I can say that it doesn’t work very well.
They all then call begin play, including begin play in the current level blueprint.
When the level is changed, the references to pawns in the game instance will become invalid. If the character is deleted, it will also break the reference.
If your pawns set these variables on the begin play, you will have correct references in the game instance again after a level change or after re-creating a pawn.
Okok, saw that I don’t have to reset anything, and also the character switch can be kinda spammed I should hide them, just one question about this thing tho: When I hide them (For example under the world) should I set their physics as disabled and only enable it when switched to it (so it doesn’t fall, which not sure if anything bad can happen whit that), and only after switching I enable physics and wake the mesh, or can I simply place them on a flate plane so they don’t fall off?
Anyway thanks a bunch for all the info, thankfully I already put all of them in BeginPlay of each class, so there shouldn’t be any problem!
P.S. I didn’t try yet, but do you know what would happen if I spawned two instances of the same class where the game instance can only store one? Would it just set the last one as the main one, so everytime I call the game instance for the character instance it would refer only to the last one?