RTS setup a static mesh switch with a button

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 e 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 e to cycle through or q to cycle previous.

Any help appreciated.

Editing the answer since you weren’t just speaking of static meshes, but actual actors instead.

What I’d do, is to create a struct with these variables. The struct only needs two variables: StaticMesh (a static mesh reference), and ActorClass (a class of type ForestWoodHouse). With this struct you can still just change the static mesh of the house ref, but when you actually “place” the house, you’d be able to grab the actor class from the struct, and spawn that actor.

Here’s the struct I made

First of all, you need to change the variable type of the Variants variable. Instead of an array of static meshes, you change the type to an array of the struct type you just made. I called mine STRUCT_Variations.

260512-tmp2.png

Now just change how you retrieve the static mesh. So instead of retrieving this directly from the array, you need to split the struct to gain access to the static mesh. Here’s that implemented:

Now, whenever you wish to construct this certain variation house that you’ve chosen through the use of “Q” and “E”, you can spawn a new actor of the current class that you’ve chosen from the struct. Here’s an example (I’m just using the “ENTER” key for this example to spawn the actor):

I don’t know if you’re using some sort of global ghost constructable blueprint, but if you’re not, you’ll have to destroy the HouseRef actor after spawning the variation actor, just like this:

260516-tmp5.png

I hope this guides you towards a sustainable solution :slight_smile:

Thank you man. Gonna try this tonight after work.

Sure, no problem. Let me know how it goes.

Okay so I understand this pretty well but this is my fault I didn’t write my question good enough. Basically my blueprint for each building looks like this.

And they are all in a parent child relationship so I’m doing most of my blueprint scripting in the “building master” which from there the buildings are separated out.

building master is a parent of house master so. Building master>housemaster>wood house>forest wood house variants>(individual bps) like fwhv1, fwhv2, etc…

And so I’m having trouble with this particularly cause I really don’t understand how I would tell it to switch to an entirely different bp with just a keystroke.

Thanks for your help thusfar.

It never ceases to amaze me how incredibly helpful this community is. Thank you for this one man

I apologize in advanced for the noob question but what is the node your using to link the target to the int? i tried dragging off my ghost building ref and doing “length” but it just wants to make a new array for some reason. I apologize again for the noob question.

No problem at all. It’s the variable called variants on your house ref. You can drag from the house ref to get the variants, and drag of the variants to get the length

If this answer solves this issue for you, would you mind marking it as the correct answer. This helps the rest of the community to see that this is a solution :slight_smile:

Would you happen to know what I’m doing incorrect here. The correct house is spawning when I use the button but it isn’t switching variants yet. ForestWHBP has 2 childs so I’m referencing the parent, is this what is causing the problem? The parent for now just has a plane cube for its meshes and so I’m wondering if it’s possible to default the bp to be the first variant and not the parent. Here’s some screenshots that might help you.

Sure. On your Forest_Wood_House_BP, you can set the default static mesh on your Forest WH Ref on the event Begin Play like this:

Make sure that your default value for the CurrentIndex variable is 0

I have everything setup exactly as you do yet the mesh still isn’t switching, I apologize for being dumb man. I’m still super new to this but I can’t seem to figure out why my code isn’t working. Like it’s all compiling correctly yet when I use my keyboard it’s not switching any

Looking at your CreateBuildingEvent, you spawn the actor, but then destroy it right after it’s been spawned. That means that your Ghost building is not actually of blueprint type Forest_Wood_House_BP, it just has the same static mesh as the parent class. If you wish to use this youtube series as a guide, you need to adapt the code to work the way you want it. For this specific scenario, you could add the variants struct array to your ghost building. That does however require you to add the Variantsvariable on the ghost building as well as the Forest_Wood_House_BP blueprint.

Luckily, I know this of series that you’re following, so you should just extract the variants array from the spawned actor, and add it as a parameter to your Spawn method. From here, you set the new Variants array of your ghost building. Hope this makes sense, otherwise just ask further :slight_smile:

So I add a variants variable to both the ghost and the parent bp. Then add it as a parameter to the create building event. But after that I get confused.

I’ve given you an example below. This is how you’d create a new variable called Variants of your struct type array and set this variable in your OnSpawn event.

It still isn’t working “Blueprint Runtime Error: “Accessed None”. Blueprint: Forest_Wood_House_BP Function: Execute Ubergraph Forest Wood House BP Graph: EventGraph Node: Set Static Mesh” (the one that sets the default mesh(I have it setup the exact same.))) And the keystroke to switch meshes still isn’t working either.

Here’s how I set it up in the ghost.

You have your Q and E event on your Forest_Wood_House_BP right? You need to move that to your ghost machine since you despawn your forest house immediately. Besides, in your second screenshot, you don’t need to set the variants variable, as you do that in your OnSpawn event. Simply pass the value from your SpawnActor, just as you do with the display mesh and the construction proxy.

Okay I did that but I’m still getting the accessed none error on the set static mesh for some reason. I tried to do an isvalid check but that is still returning the same error.

Seems I only get the error when I hook up the default mesh nodes.

I’d suggest that you create a new question here on the site, since this is now a different issue. You initially asked for help with changing the static mesh. You got that help,but now that you’re getting an error, I’d say that’s a new issue, and perhaps someone else could help with that, or at least we could get some better overview of the new issue :slight_smile:

seems to me people are way over complicating this. if you have a bunch of blueprint classes and you want to cycle through them then all you really need is to have an array which contains the classes, and a function which spawns and despawns the class based on a index. the trick to it is that you set the variable type to be that of the parent class. This is based on the additional information you provided [here][1].

below is an example of how this could be done. the basics of how this example works is when a button is presses in a widget we spawn in a building from one of the arrays, then when we want to switch the building thats shown we destroy the current building and spawn in a new one from the array either the index prior or after.

when the button in the widget is pressed it calls the buildingTemp event which spawns the initial building from the array and sets the current building (picture1). also in picture one you will see a method for placing the current building wherever the cursor is. in picture two you will see how we scroll through the buildings by increasing or decreasing the index int, we also need to ensure that the index is in range at this point too by comparing the current index to either the last index or to 0. once we’ve modified the index and ensured its inrange we destroy the current building actor, spawn the new one, and set the current building var. the last bit of script is placing the building which consists of just un-setting the current building var since the building will already be in the place it should. the last picture shows another method to simplify doing the variants since im assuming its just different meshes for different field types ice world, wood, etc.