Download

Having issues with character spawning....

Currently having an issue thats been a headache in UE4 for a long, long time.

I’m currently making a 2D side scroller co op game. Similar to all of the old arcade game. What id like to do is spawn a character for each player. However, I want this character to be different based on the player number.

I spent weeks trying to get it to work in C++. I tried spawning pawns manually and possessing, and it wouldnt work correctly at all, with little help from the community. So went down the route of overwriting what class gets returned as the default when spawning. Could only get it to change colors with a random number generator. Any attempts at accessing player id would crash the editor. So got left with random. And it kinda worked. If you had 2 players, it was great, but any more and at least one would never get possessed. No matter how many times I rewrote the code. Lets not forget that sometimes it would random spawn extra pawns on its own, even when I wouldnt touch any spawn code other than the class variable.

So I gave up on C++. Figured, hell, fine, Ill try blueprints. So tried that route. Here are my attempts and where they failed.

  1. Getting an array of all player controllers, having a for each loop to iterate through, and spawn the color based on the current index in the loop. Apparently the switch broke and the array was never filled, as they all defaulted. However that same reference at that same index in the PlayerController was used in possession and it worked. Kinda. Each player possessed a different pawn, but they were all blue, (the pawn the switch defaulted to) and they couldnt move. Could duck and change direction, but no movement whatsoever. (Pawns are definitely set to replicate This happens regardless of if running multiple players or not.

  2. Placing one of each character manually, and on begin play getting a reference to a player controller based on index, and using possess to possess the pawn. Didnt work at all.

So, I’m about ready to trash this project. The whole premise was based around the co-op and each player is needed, as each player presents a unique ability to help succeed.

I guess before I move forward with something new, I’d like to ask for ANYONES feedback in what I can do. The C++ side was pretty terrible with giving support, so I guess Id try these waters.

Again, Heres all I would like to do.

4 players. Each one gets its own color. Player 1 gets blue, player 2 gets beige, player 3 gets green, player 4 gets pink.

At level start they all spawn, or get possessed and play starts. That is all. Any help is greatly appreciated.

I apologize if I sound bitter, but I’ve been banging my head against I problem I figured should be really simple in this engine. I guess I was wrong…

Hi there, I might have replied to one of your previous threads. We are working on Super Smash Ponies, and encountering a similar hurdle. While I can not give you our code, I think I can point you in the right direction. Be aware however, that PIE will render this wrong. It only works in the cooked version of the game.

This is what we did in blueprint (condensed).

  • What you want to do is create a game instance, and have a variable for each slot that is set to just a plain old actor class.
    *player1slot, player2slot, player3slot, ect…
  • Then you want to create a blank character object with no physics and all collision set to ignore.
  • Set your base spawn class to that blank character. This is your noncharacter.

You must do steps 2 and 3, otherwise, you will never get player 1 to spawn correctly.

  • Now, set the slots you want to spawn with a character, and the slots you don’t want to spawn with the noncharacter character actor.
  • In your Game Mode, on the Event Begin Play, you will want to set a for loop, 0 to (playercount-1).
  • Create a local variable called PlayerID on your Game Mode.
  • Attach the output to an int switch and set the PlayerID to that, you will need it later.
  • Now, on int 0, you will want to Spawn Actor player1slot from your Game Instance.
  • Still on int 0, you will want to Possess and set that to Get Player Controller 0.
  • For all other players (all other numbers on the switch) you will want to set system Default Pawn Class, to the corresponding Game Instance player slot. IE: Switch Int 1 would be player2slot from the Game Instance.

This is what one of the moderators did, and we are in the process of trying, as the described process above ONLY works in the cooked version of the game, and creates a monster of problems with online multiplayer. It DOES work however.

Have fun!

Having learned that it only works when built, I think I have to finally shelve this project. Unfortunately, the game is heavily designed around the Co-Op gameplay, and having to rebuild the game every time we make a change is something that sounds like a detriment to the project.

I appreciate the help.

It certainly is, and if we find a solution, I will be sure to post it. With one of the earlier builds of 4.7 - I think it was 4.7.0 - it worked in PIE. So I have high hopes for 4.8.