Create queue on clicked

Hello everyone! I’m currently making a RTS and i got a spawning issue. When i click a button, the actor spawns at the building, however when i put a delay on, and select another building it just spawns at the newly selected building when det delay is done. It should spawn at the selected building when the button was clicked.

Best regards

1 Like

your spawn is set to spawn wherever the “selected building” is, so if you select a different building before the spawn takes place then you have set a new spawn location. to solve this you just need to have a variable which stores the selected building locally before the delay then use the variable as the spawn location.

to accomplish this just right click the selected actor output pin and select promote to variable, this will give you a set variable node. connect the set variable to the exe pin on the cast, then connect the delay to the set variables exe output pin, then the delay to the spawn. now take the return pin from the set variable node and plug that into the get actor location node.

hmm on closer inspection this may not be exactly right since your working in a widget and not in the building actor itself. given your setup you may actually need to look into using an array to create a que system. basically have a array of structs with the struct made of unit to spawn, spawn location, and may be delay time. given its a rts though you will still probably want to put this functionality in the building blueprints themselves. if you put it in the building bp then it would be scripted like: on click → get player controller → cast to playercontrollerX → get selected building → get BuildQueArray → add unique.

ok so heres a little example of what i was talking about with having the build que in the buildings themselves. oh and note my thinking here is rts games like starcraft and the command and conquer games.

ok so to start first create a struct which contains the needed info, build time and unit to build. then create a building base class so that when you have different types of buildings you can just cast to the base and this functionality will work for all children. then create a variable, make it array, and make its type the struct created earlier. now create a custom event to begin the script, i also added a little retriggerable delay to eliminate bugs from the event being called many times in quick succession. then we get the array and make sure its populated be checking its length (length, greater than, branch). we then get index 0 and split the struct to access its parts. we connect the build time to the delay and the unit to build to the spawn actor (ignore the spawn location pin as its not needed). i used a spawn loc scene actor here as the place the unit will spawn from the building. then after the spawn remove index 0 and call the build unit event again to loop the script. the loop will make it so the building keeps spawning units from the que until the array is empty.

now in the widget all we need to do is on click get the player controller and cast it to the one we are storing the selected building in. we then get the build que in that actor reference and add a unique index. the index we add will be populated by another instance of the struct from the widget. finally we call the build unit event so the script begins, this is in case the building is not already running the loop we need it to start.

You have no idea how glad i am for your help, very elaborate! I’ll give it a shot as the first thing by tomorrow, then i’ll let you know and apply this as answered. I’m looking forward to try it out and good weekend to you Thompson!

hmmm i just wrote a response but it didnt post odd. anyway no prob happy to help. theres one issue with the script i posted you will need to change out the add unique node for the normal add node to make things work correctly. the add unique will only add one instance to the array instead of the multiple that your looking to add, the add node will fix that. i wrote up the previous post based on theory and didnt have a change to test til a little while ago.

Got one issue, when i try to get Build que in the widget, i’m not able to connect the Selected Building to Target in Build Que, they are not compatible. Might be something in the struct, i’ve set Build Time as a float and Unit To Build as an Actor Class Reference? And i assume that the Unit To Build Info is just a variable? Best Regards

i’m not able to connect the Selected Building to Target in Build Que, they are not compatible

in my case the selected building variables type i set to BP_buildingBase which is the base class of my buildings. if yours is set to something like actor then you can either change its type or put in a cast node to identify the reference.

And i assume that the Unit To Build Info is just a variable

yup its a variable. also its type is the same as the struct to make things simple. you could however just split the struct pin on the add node and use whatever input you like.

I just added a cast node, and works like a charm so far. Your help is much appriciated, thank you Thompson!