Switch Characters - Not Mesh

Hi, I would like to have a system where, if you press right bumper, the character on screen switches to another character. Ideally, there would be two icons, one on the right and one on the left that show which character you would switch to when pressing left or right bumper, and if you press a bumper it switches to said character.

I’ve been banging my head against the wall for a while on this. Any assistance would be greatly appreciated. I have some blueprints here: No Title - Album on Imgur

They aren’t working. All it does is create another character on button press, but you can’t possess that character.

If you want to use standard nodes, the process might look something like this. Do Once nodes help prevent input spamming - maybe not necessary here but anyways a good safety check. I would then first check that this controller actually has a pawn (Is Valid?) before doing anything else - followed by saving the original actor’s transform into a variable to remember his position and rotation etc before you unpossess and destroy him. (If you dont do this step then the guy might spawn in at 0,0,0 on your map because the old guy got destroyed). Now you can Spawn a new actor using whatever logic to pick a new class, and use the transform variable to pop the new guy back in the same place and rotation as the old guy. Then you can tell the player controller to go ahead and possess the new one. It looks like the two nodes you’re using are custom events on that class so it’s hard to say what’s going on in there currently.

For all who come to this, I haven’t found a good way to do this, but I came across a different way to start. I just have an issue where the newly spawned character doesn’t spawn in the last character’s location.

Hey 1I2Hawk,

Thanks for the help! I feel like I almost have it. I think I have to move this ability to the PlayerController instead of storing the code within each character blueprint since this is a functionality between the 3 characters. How you have it set up isn’t working in the playercontroller. Also, I’ve never used the select node, and I’m getting an error on the select node when compiling.

This feels close though!

Oh - well you almost got it while I was typing. Try saving the transform to a variable before destroying the first guy and you’ll have it.

Yes correct - this should definitely not go in the character BPs because they’re getting destroyed and spawned while this is logic is trying to run and having multiple copies of code is generally a pain to debug later. The player controller is a good spot because that persists on behalf of the player even when a pawn is not present. You could also pass some things back to your Game Instance class too, etc etc. Don’t worry about the Select node (although those can be super handy to learn about). Might be easier to store an array of classes and have your bumpers increment or decrement which class you “Get a copy” off of that from. If this doesn’t run properly, you may have to be more explicit with the target pins on the Possess and Unposses. I have default “self” in there which assumes this is written in the actual player controller class already.

I’ll try to set it up without an array right now, but an array sounds best. I’m just so new to this I don’t know how to set up an array of the character blueprints. Do I make it a variable? Also, check the screenshot below. It’s still not working correctly, even without the select node.

When it’s in the Character Blueprint, it works, but it spawns a new character while keeping the old character on screen, doesn’t destroy them, and in the PlayerController it doesn’t work at all.

So, I can get the pawn to spawn, but it still spawns not in the same spot as the previous pawn, and also, the previous pawn doesn’t get destroyed.

Again, this logic is still inside the character you might be destroying halfway thru so start moving this code into your player controller first or it won’t work. Over there, create a new Variable and under its Type dropdown, type in the word “Actor” - hover over that in the list (it’s the top one) and pick the purple ball next to Class Reference … You should see Actor in a purple color now instead of its normal blue (the blue version would be used for actors that already exist in the world, think of the purple as pulling things out of your content browser instead). To make this an array, pick the little bar to the right of the Type dropdown and set it to the little 9x9 grid icon as shown here. Now compile. This should let you create a list of all the actors you want to spawn under the default value section underneath.

329241-aarray.jpg

Oh - just to double check - make sure in your map’s Windows → World Settings that you are actually calling your custom player controller there if the code doesn’t seem to work. You might be writing code in a player controller but not actually using that when you’re going to test. Easy to forget this step.

We’re so close. I can now spawn the actor in place of the previous actor, but the previous one isn’t getting destroyed. And if you keep pressing 0 it spawns the first character again, but you retain control of the 2nd character, so it doesn’t cycle through the array I don’t think

All this script is in the Player Controller class, so not the individual characters.

As far as working with your new array, here are some tips since you said you’re new to this stuff. Note, if you use a For Each node like that then you’ll spawn ALL of the guys at once. Do this instead:

  • Arrays count from 0, so if your array has (5) guys in it, then these are referenced as #0 thru #4 and not 1 thru 5.

  • It would be handy to store which of the (5) guys you currently have as a new variable of type Integer. I made one here called CharacterIndex for example.

  • Maybe your right bumper increases thru the list. On each press, you can increment the integer. Just be careful you don’t go higher than the length of the array or you’ll get an Out of Bounds error. In this top section, I made it where if you get to #5, then it wraps back around to #0.

  • On the left bumper, you can decrement the integer. Just be careful not to go lower than zero for similar reasons. In the middle section, I made it where if you get to -1, then it goes back around to 4. This would be the length of the array minus 1.

  • If you don’t like wrapping the values around, then another option would be to add or subtract like normal and then use a Clamp node with a min of 0 and a max of array length - 1. Then you’ll stop at either end instead of wrapping around the list.

  • To draw the correct guy’s class out of the array, use the “Get a Copy” node and simply feed the index into the left side. It will pull just that one part out of the array which can now go into your SpawnActor node.

The reason you’re guy isn’t going away is because your graph is trying to destroy the player controller and not it’s controlled pawn instead. Stick that extra node on the wire going into the destroy section yeah. For the same reason, your new guy is spawning out in the middle of nowhere because you’re grabbing the transform of the player controller and not the old pawn - and the controller usually sits like a 0,0,0.

Dude, you rock with helping me, and I understand if you wanna bail, since this is getting pretty long. Thank you so much.

Still have an issue though. I set up the Right and left bumpers with ints like you have, and they work fine, but there’s a weird issue where I’ll press right or left bumper, then it’s like the new character spawns in the middle of nowhere, however if I keep pressing RB or LB, it spawns somewhere else, then I can keep switching between them, but now in the new spawn. The switching now works, but there’s something off with the set and get transform I think. Also keep getting weird errors Set transform and destroy actor

Sorry, I am using the get controlled pawn then get actor transform then Set transform variable, which is then plugged into destroy actor. My last pic is the latest setup.

Accessed None tells me that this player controller isnt actually the one controlling the Pawn (it has None for a controlled pawn). Check two things:

  • In the map, find Window → World Settings and make sure this PlayerController_MAIN is set correctly in the GameMode section
  • Back in your character, search in the Pawn category for “Auto Possess Player” and set that to Player0 and not Disabled

It’s probably some stupid checkbox somewhere that’s causing grief … this is the life of a developer hah.

329263-worldsettings.jpg

:confused: Still isn’t working. Now it’s doing the same as before, where it seems like the camera resets to 0,0,0 and the errors are still happening. If you’d like to stop helping that’s completely fine. If not, I can share the project through google drive with you or you can try giving some other suggestions on what it might be

Two little fixes and I got it to work. In the Switching function, delete the Unpossess node. Turns out the destroy does the unpossess for you. Just go from SET Transform straight to Destroy Actor and into SpawnActor… Also, set your Character_Index to a default value of 1 for the Viking guy, not 2. When you only have 2 items in an array, these are items 0 and 1 … so a default of 2 is already out of bounds. One thing I see is a noticeable pop when the character changes over because the new guy doesn’t come in at the same place in the animation as the old one that was there, but that’s cosmetic stuff you can figure out later. The good news is you can add more than 2 characters later into that array and you won’t have to recode anything.