How to Switch between two Blueprint classes using widget buttons

I wish to create a widget where once u click a button u can shift between one blueprint class(containing a camera with a spring arm attached) to another blueprint class. I have seen some post but they have assigned a keyboard shortcuts ( 1 and 2) for doing the same.

Hello Suraj Nair,

When you say “switch” do you mean that you’re switching what you are possessing? Do both of these blueprint classes have instances already spawned in the level at the time of this switch? What types of blueprints are these two classes, as in Character, Pawn, or Actor?

Hi ,

Thank you for replying, what I meant by ‘switch’ was to posses from one class to another class which are both pawns.By instances do you mean both the pawns are in the view port…? if so then yes.

Below is what my view port looks like

both the cameras are pawns with just spring-arms attached to them

Thank you for clarifying that. Here’s a simple way to do this with your current set up.

This method is assuming that both pawns are of the same blueprint. If they’re not, it should be even easier as you can set the references before runtime.

In the pawn’s blueprint, make a variable that stores a reference to this type of blueprint, so if you’re working in the ThirdPersonCharacter from the template, you’d be setting up a variable that is a ThirdPersonCharacter actor reference. After that, set up the following nodes (OtherReference is my variable).

This allows us to look through all of the instances of this type and get a reference to the other one in the scene.

After that, create the widget blueprint for your button. Add a button and then in the details for the button, click the + beside “OnClicked”.

In the event graph that appears, you can add the following nodes.

This gets the currently controlled pawn, makes sure it’s of that blueprint class, and then gets its reference variable to tell the player controller what to possess.

Let me know if you run into any issues. Hope this helps!

Hi ,

I did what u said and it works but am having issues when the game starts.

1.When I click play the camera shifts to a position which I have not placed

  1. I have two cameras named Cam_01 and Cam_02 (pawns) placed in the view port but the switch happens only with 1 camera and the other cam mentioned above. I may have messed up something

I created a switch based on what you have said and it works but not between the cams I want

Could you show me the logic you added to the blueprint for these two pawns as well? When using two different blueprint classes, you shouldn’t need anything as complicated as that for the button’s logic. You can do this:

Hi Mathew,

thanks for that. I have fixed it but there is one initial problem about the camera switching
as above the positions of my cam in the view port are different than what they show during Game Play

this is the Cam_01 blueprint.

I don’t know if adding just a capsule collision is a mistake but i was trying to achieve it like the third person camera

Cam_02

and this is the result I am getting currently

unknown camera

cam_01

thank you for helping me out on this

Are the cameras placed into the level before runtime (before pressing play)? If so, you may want to check the World Outliner to see if you’re ending up with 3 cameras. It may be spawning a 3rd one to possess rather than attempting to possess one of the ones already in the scene. If that is the case, you should be able to fix this by selecting one of the instances of the pawns in your level and changing the “Auto Possess Player” in the Details panel to Player 0.

there is no third camera in the scene. This is how the outliner looks like

73808-unreal+prob+1.jpg

what is the type of variable You had used for referencing the “Third Character blueprint” here

cause if its a pawn type of variable then it does not connect to Array element(For Each Loop), but in the event graph of my widget blueprint it gets connected to the posses function only if its a pawn type variable

Cam_01

Mouse_interface

the last time I connected it was by first changing it to an Actor type variable so it connects with the Array element and later change the variable type again to Pawn so that it gets connected to the Posses Function.

My particular one was “ThirdPersonCharacter” but the reference variable should be set to a reference to your particular class, so it should be a “Cam_01” or “Cam_02” reference, whichever one it is trying to point to.

Hi ,
what I was asking was when you create a new variable it is originally a boolean type by default, so what type of variable did you place as a ‘set variable’ for referencing the pawn.Is it a pawn type of variable or an actor type

Yes, that is what I mean. In the field at the top of that, type in Cam_01 or Cam_02, depending on which one you need. It’ll give you a Cam_01 or Cam_02 reference and give you an option of 4 different types. You want the object (blue) reference.

Thanks they are finally connecting I didn’t know they would appear there :slight_smile:

the switch is working finally but still stuck with a third cam. The switch is happening between cam_01 and the ‘Player start’.
I tried first selecting one of the pawns and changing the ‘Auto Posses Player’ to ‘Player 0’
But then the switch does not happen

This may be related to how your Gamemode is set up. It should be able to be set to None for the Default Pawn Class seeing as you’re going to be possessing something that already exists in the scene when the game starts.

Hi ,
I restarted everything and did what you said but now the cam goes somewhere else and does not switch
is it possible if I can send you the file so that you can have a look at it…?

Of course, you can send a link to a download for the project either here or to me directly in a private message on our forums.

[1]:

Hello Suraj Nair,

There was a problem with the project that you sent me. Could you please check your private messages on our forums for the details?