How to make AI spawn buildings correctly? (I have my own solution but its just bad)

[Age of Empires type RTS]
I have made AI spawn buildings, technicaly it works, BUT its horrible
Buildings are floating, they are too close to each other etc.


Here is my solution, ignore the torrent of green lines, I was trying to draw on screen where AI is trying to build buildings but it seems to have weird offset or I just dont know how it works…



So does anybody know how to make it look better?

You are only using the variables on the AI actor to build the building. Which might not have enough information to make it look good. In Age of Empires, player places down a framework of building and move the AIs to build it. It’s the player spawning the building from UI, not the AI actor.

I don’t see a lot of games letting AI to just spawn a building. Usually the player plans the building and AI builds it according to player’s plan.

You will need a decoy building. Move the decoy around and update collision detection.

Now on the decoy actor itself handle unbuildable cases. Like collision to other buildings/pawn/environment.
image

Also check Z axis to make sure the building isn’t floating. Or on top of another.

To make building spawning look good is boiled down to handling unbuildable cases and setting up detections.

EQS would be good for this

By AI I mean enemy AI not controlled by player and I gave the name Age of Empires just as a refference, I just want to have AI that can spawn buildings if it has enough resources but I just cant get it to work nicely when it comes to checking if the spot that AI chose to build a building is free of other buildings or pawns… As of right now it just builds on top of the building rather than choosing another location (and sometimes it DOES choose another location which makes it even weirder…)

Another idea still including a decoy building but this time it’s attached to the AI enemy actor itself. When an AI wants to build a building, spawn a decoy and attached to self.
image
If the decoy collision excluding own AI actor is buildable, not overlapping other building/environment. Detach the decoy building and move the AI away from where it’s gonna put the building. Or keep walking around until it finds a buildable space.
image
After that, spawn the building at decoy location and destroy the decoy.
image

The proper RTS AI would have map knowledge to a certain degree. What Auran13 suggest may be a way to generate map knowledge. Mine is just a suggestion to make building space not overlap or stacking.

Im not sure if we understand each other correctly, I want the simplest (enemy not player controlled) AI possible whose job is to find a suitable place for a building which does not overlap other buildings, environment or pawns and as long as it has enough resources, just spawn the building (later if ever Im going to make it look better as in make it spawn foundations and THEN send workers to build the building as of right now I need the bare bones of that system) I need this project to pass exam not to sell it to public I have most of it done and besides main menu and this very issue I have pretty much everything done… But the AI part is the hardest for me :frowning:

It would be nice if I could just get the buildings box boundries (I have box collision component on every building) and just check if it collides with something and if it does, dont spawn it and check for another spot and if it doesnt simply take away neccesary resources from stockpile and spawn the building…

This EQS system looks promising but its just another thing I know nothing about and just adds time to learn, and I dont have much time :frowning:
What I have created so far (and shown here in pictures) is the best thing I currently came up with and it ALMOST works as I need it to…

usually these types of games would have a grid, and you’d just check the grid if the spot is blocked or not,

EQS kinda does this by creating a temp grid and scores each point, ie not blocked, prefers closer to center etc

the simplest way for you to recreate this is to do like 20+ line traces down in a grid shape, you can do this with a for loop. which will give you a rough idea of whats blocking or not

You cannot check the building’s bounding box without the building being created. That’s why I suggest using a decoy.

You can always just move the building by itself after creation. If your map has no height difference, just set the Z to some static value when moving/spawning.


Check more overlapping actors(environment /pawns) or do not add class filter at all if you want. The randomness may move the building very far and random.

If you use always spawn, adjust location when spawning. The initial location’s Z might be move up like your Building on top of another. Do Z checking besides overlapping actor.

Your solution almost works, I mean the AI does look into different places to build the building and even builds the default buildin however :


I cannot get the building to build from the ai ( the AI is set in blueprint TownHall) here in the picture above I finally got it to somewhat work, I mean I get the townhall itself but for some reason the BuildingToBuild variable always comes out as NULL… it feels like the “begin play” in ghost building starts before the townhalls BuildingToBuild is set? Even though townhall exists the moment you start playing the game? Or mayby Im getting something wrong… I tried using Cast To but with same result (although I can barely understand how it works…) and I tried using interfaces but again with the same result.

  1. this is what ghost building receives from town hall
  2. unreal tells me its null
  3. this is proof that I indeed got the town hall blueprint
  4. this is supposed to give me the name of the building to build but because the refference doesnt exist it spits nothing…

This is the Town hall BP where the information about building to build is set (nr 3 in output log)

I dont know what Im doing wrong but Im fighiting with it for a while now and it drives me crazy… If I set the building to build to a specific building in the ghost it works perfectly fine but I just cant get the correct building from the AI to be set in the ghost :frowning:

Oh my god… I somehow missused the interface… I gave up on it and just removed it and now it works perfectly fine!!! :smiley:

I guess somehow the interface didnt get the BuildingToBuild variable and so in Ghost Building it was set to null and so it replaced the variable of BTB AFTER Ghost got it from the townhall (first picture in last reply) and so it was set to NULL right before “Spawn actor from class” node :man_facepalming:

Final Bp

Here I put the counter so I dont get the infinite loop problem

Here I clear the array of overlapped things so they are not taken into account the next time loop is executed

Here is whole picture of the loop

and here it is with the spawning desired building and destroying the ghost

Those are the changes I made to the loop provided by @Kiray and I hope someone will find it usefull in the future :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.