Switch actors with keystroke

So I have an rts project I’m working on and I can click a gui button and spawn a static mesh that follows the mouse cursor where I then click to spawn it and it builds over time and I’m trying to figure out how to make it so I can hit a button on the keyboard and the static mesh will switch to a different one.

Like say I have house variant 1 selected and then I hit r and it switches to house variant 2 which will then play it’s blueprint command to build when I place it. And then if desired hit r to cycle through or f to cycle previous.

Any help appreciated.

I’m pretty new. All my buildings have their own bps and special commands and such inside each building as well as their own static meshes and such. But they all have parent child relationships

house master>woodHouse>icewoodhouse>icewoodhousevariant1 or iwhv2 > iwhv3

and so if I’d like to have it eventually be one singular command that I put in the global buildings master is there a way to reference ALL buildings with a single code or will this be something I’d have to tweak later for each “category” Like say I click on the house category in the hud itd work for any “template” I select ice, grass, forest etc. and then from there I’d use the keystroke to switch between specific variants

So to reiterate; I already have a way to select buildings, have them follow mouse with a ghost mesh, spawn them and build them. I’m merely looking for a way to be able to switch from one actor to the other without having hundreds of gui buttons on screen.

Thanks again.

I have reposted this a bunch now and haven’t received any answers that actually work, so pls no salt or toxic in this thread. I’m not trying to spam unreal just need help and can’t find a good tutorial anywhere so I’m using the answer hub for it’s intended purpose.

If the toxic guy shows up just ignore him pls.

Here’s what I have so far, doesn’t work and I’m not really sure what I’m doing

I’m assuming that you want to cycle through the classes based on the current index. One problem is that you use that same index to select which array you want to get classes from.

261689-image0.png

Another problem is that the action events that you want to run will not run in this blueprint, since it has no relation to the player controller at all.

Won’t ever get called, which means that the nodes won’t be reached.

Instead, you could be running the InputAction events in either your player controller or your player character (if you don’t want to create this logic in the player controller, then you could just enable input for this blueprint: ([see here][2])).
You can create two event dispatchers in the player controller which will handle the next and previous house swapping.

261702-image3.png

Then, just call these events on the appropriate Input Action.

261703-image4.png

In your master house blueprint, create two custom events for Next house and Previous house. These can then be bound to the player controller event dispatchers by getting a reference to the player controller.

Now, any child House blueprint you create from this will have this logic built into it, so you only need to write it once. Now, whenever you hit one of the input keys, it will swap between whichever house class is next in the array.

261705-image6.png

For each child blueprint, simply set the default value of CurrentIndex to the corresponding index it has in the master house’s array. For any new child blueprints you add, you’d have to go into the Master House blueprint and add them to the array.

Wow! Thanks! This one actually worked without spitting an error. I am having a couple problems with it though. It’s only incrementing or decrimenting once, it’s only changing after the building is already placed and I can flip flop through the first one and the default while it is placed.

Thanks for your help thus far and here’s how I have it all set up. If it’s any help I have a ghost mesh for buildings before they get placed and a way for everything to follow the cursor and spawn.

Could be any number of things, but let’s see if were can narrow it down.

Firstly, is your MaxIndex set to the number of child house classes (I.e.the length of the house array (which might actually be better to use come to think of it))?
Secondly, is Forest Wood House BP the parent blueprint of the houses you want to cycle through? If it isn’t, then any house toy spawn that isn’t a child blueprint won’t have this logic, and therefore won’t cycle.

Thirdly, is it the house actor that follows your cursor, or is it just some mesh that is a placeholder for where the house actor will spawn? If it’s the former, and the current mesh of the actor is set to be the ghost mesh, then it shoukd theoretically be cycling before placement (assuming this is what you want to do). If however it is the latter, then it gets a bit tricker, as the ghost mesh would have no relation to the house blueprint, and would therefore not cycle itself to match the next or previous house.

These are the only causes I can think of with the information provided. Hopefully one these points is what’s causing the problem? If not, would you be willing to show a bit more code so that I can help you resolve this?

Max Index is set correct, is an array with 3 elements and is a parent. Placeholder mesh (transparent ghost mesh is what cycles I can manually set the display mesh for it in each bp. And sure!

Okay, so the reason your building is cycling through only after placement is that that is the nature of the blueprint. In your Building_Ghost you create a house actor and then destroy it, so the logic is essentially unreachable because the house has been destroyed.

Re-reading your initial question, I find that I’m still unclear as to whether or not you want the buildings to cycle while they are in “ghost” mode, or if you do want them to cycle only after they are placed.

If you’re wanting the ghost mesh to cycle as well, then you may have to do one of 2 things:

  • Send the Create Building (Message) every time you cycle the variants, or
  • Instead of destroying the house actor that is spawned in Building_Ghost, simply hide the actor mesh, and add some logic somewhere in the ghost to refresh the mesh of the ghost if the spawned actor reference changes.

If you’re not wanting the mesh to cycle after it is placed, then you’d simply need to move the variant logic from the house Blueprint to the ghost blueprint.

You mentioned that you were only flip-flopping between the default and first array element houses? The reason for this could be that your default house blueprint has a CurrentIndex of 0, and so does the first house actor in the array, so my suggestion would be to perhaps increase the CurrentIndex of each house in the array to avoid any of them having the same CurrentIndex value.

Ah, I see. The same logic should still be applicable to the ghost, you just need to pass the values for current index and Houses to the ghost so that it can cycle through them:

Sorry I’d like them to only cycle in ghost mode. No I couldn’t put the code into the ghostbp cause I need to change the variants for each separate building.

I tried create building with the ghost bp as the reference to no luck.

Accessed none error when it tries to destroy actor so the create building node just spawns a new building every time i hit the key to cycle through.

What is the “house mesh” and the “ghost mesh” you are using for this cause no matter what I can’t seem to get the spawn actor to link up with the set static mesh.

Oh sorry! I forgot to use your same custom event and names. House mesh is equivalent to your Display Mesh from the spawned actor (forgot to name it the same as yours), the ghost mesh was just a placeholder for testing if the static meshes do change on the ghost blueprint. Essentially, for the Create Building event, nothing really changes for what you already have, all you need to add is the CurrentIndex and HousesArray variables so that you can use them for the NextHouse and PrevHouse events.

Also, this was for your ghost blueprint. The variant swapping logic should be transferred from the House to the ghost, and the house simply passes the index and array information to the ghost.
link text

Well here’s how both my ghost and my forestwoodhousebp parent look.

My only real issue with this setup is I was trying to avoid using the ghost bp for this code cause wouldn’t I have to add something like this for every parent array for every building? Like stores, farms, mines etc…?

Thanks so far, been tremendous.

Okay, so your house blueprint no longer needs the variant logic, since you don’t want it to cycle through houses once it is placed.

So instead, in the house blueprint, all we have are the variables CurrentIndex and Houses (as well as whatever you had prior to this answer). This is where you set the values for those variables.

In the Building_Ghost is where the variant logic will all go (this includes binding the events). Instead of replacing everything you already had, this is simply added on top of what you already have in the Ghost blueprint.

The above “Create Building” event is the interface event you had previously implemented. There’s simply some added logic on top of this. So now we can get the CurrentIndex and Houses variables from the spawned house.

What the NextHouse and PrevHouse events do is almost exactly the same as what they did earlier, except this time they run an ‘Interface Call’ (not a message, though that would technically still work) to Create Building, which will again create the next or previous house variant, set the appropriate variables as you had done before, and destroy the actor again.

Your ‘On Spawn’ function would just need a simple addition of the variables to be set (except the array, which cannot be passed by reference through the custom node for some reason).

*This image is missing a few of the boolean variables you had set in your previous image. I will stress that you should keep the work you’ve already done, and add the appropriate nodes on top of the work you’ve already done.

In answer to your last issue, you can theoretically use this logic for any building type. I’ve used the name “House” since that was the original query. This could be renamed to ‘Buildings’ to be more generic. By putting this in the ghost, you remove the need to copy-paste the variant swapping into every different building parent blueprint. All you would need in the building parent is the variables (and I’m assuming you have a master building type which is generic, so you’d would theoretically only have to add these variables to the master building blueprint, and then simply set the variables correctly in each blueprint).

Another way you could handle the various buildings, although this would require another question thread, could be through using data tables with rows that contain the array of meshes you wish to use per building type (the rows would be named after whatever building you are constructing). So instead of creating 50 blueprints, you could have a generic blueprint that handles the logic for constructing the buildings in stages, and simply get values from a data table.

Hmm. I’m super new and didn’t even know that was a thing def have to look into it.

But on the ghost mesh if I add the set current index it doesn’t allow me to scroll through ± 1 Is would you happen to know how to fix this?

Also is there a way I could just make an actor array and an index in the building master then reference those in the ghost bps code so I could then change the array’s actors and index independent of the ghost bp, but for like every mesh. I was ■■■■■■■ with it for a bit but my Ihud stops me from spawning anything when I add another input for “BP_Display_Mesh_Arrays” and hook it all up.

I tried to hook I up like you said but now none of the variant switching will work and all my destroy actor nodes have an accessed none error.

The reason your variant switching isn’t working currently is that you have to events that spawn the houses: One is called CreateBuilding (which is an interface event), and the other is a custom event that you’ve named ‘CustomEvent’. Now, from these pictures, you don’t seem to ever call CustomEvent, which is where you set your building array and current index. I’m not quite sure why you have these two events, as they are mostly the exact same. But, if you want the variant switching to work again, then on the ‘Variant_Next_Event’ and ‘Variant_Pevious_Event’, at the end of these, instead of sending a message to ‘Create Building’, you can call ‘CustomEvent’.

The reason the variant switching doesn’t appear to be working is that when you call the variant next or previous events, they send a message to the CreateBuilding event, which spawns a house with a CurrentIndex of 0, and then sets the CurrentIndex to 0 again.

Okay, so I set it up to call to custom event and the variant switch isn’t working still. I’m not really sure what you mean when you say it sets the index back to 0, cause the setters on the switch events are before it spawns it and then for the other one even if I remove it, it doesn’t work.

But even then the “0” element on the variant array has meshes bound to it on the button I use to initially spawn my house and so if it gets set to 0 I should see the mesh still which isn’t appearing. Maybe if there’s some weird interaction between the parent mesh and the children it’s referencing for some reason?

Cause the building_actor_variants variable is an array of “building_master” so then I add the actors into it per bp’s parent house/store etc. Maybe I also set that up incorrectly?

Thanks for your help thus far been great