I'm working on an RTS in BGE, and I am not sure how I could make it so you can place buildings on flat surfaces (or unless there is an easier way). I would love to know how to place buildings when you click a certain button to place it.

I don't really need anything fancy like price and stuff like that yet. I really have no idea if Python is required to do this.


1 Answer 1


Warning: this is a long answer. It may take a while for some of the images to load. For those of you who don't like reading: The working .blend files are at the bottom of this answer.

Basic Mechanics

You first need an empty (named Arrow) to follow the mouse. This empty will add the buildings.

You will need a script like this.

from bge import logic

def main():
    scene = logic.getCurrentScene()
    cont = logic.getCurrentController()
    mouse_over = cont.sensors["MouseOver"]
    if mouse_over.positive and scene.objects["Arrow"]['ActiveYesNo?'] :
        tracker = scene.objects["Arrow"]
        tracker.worldPosition = mouse_over.hitPosition
        tracker.worldPosition.z += 1

And use this on your empty.

enter image description here

That way your empty will always track to your mouse.

Now all you need to do is make it add the buildings when you click and it is in a valid position. You can do this by adding some more simple logic on your empty.

enter image description here

Now when the Boolean property OkSpot? is true, and the left mouse button is pressed, it will add your building object. however this doesn't help us much if there is nothing to toggle that property when the placement spot is valid.

enter image description here

Now when the arrow receives the message GoodSpot it will set the Boolean OkSpot? to true and, when the arrow receives the message BadSpot it will set the Boolean OkSpot? to false. Now we are done with the empty (named Arrow), but we now need something to send those messages.

We obviously need some sort of collision detector. In this case, a simple cube will work. Add a cube and parent it to your empty (named Arrow).

We need to edit the cube so that it is slightly above the ground.

enter image description here

Now when it is on the slightest slope, it will be colliding with the ground. It is also important that you change your Collision object's collision type from static to sensor, because static objects can't detect collisions with other static objects and, I assume that your ground will be a static object. You also should set it's collision bounds to Box.

enter image description here

Now that we have our collision object detecting collisions properly, we still need it to send that information to the arrow. We can do this by using this logic setup.

enter image description here

Now we can test it. Everything should be working so far. the building we will be adding is just a cube for now.

enter image description here

The basic mechanics should be working now to place the buildings. skip to the end if you don't care about how your game looks.

If you want to be make it much more complicated, you can have two other no-collision duplicates of the building that will be added, one transparent green, and the other transparent red. (to show when the selection is valid.) Then you can make the arrow directly connected to the red and green objects telling it when the selection is or is not valid. TO make this simpler, first we should set the maximum draw type on your collision detector to wire. we also need to make it invisible.

enter image description here

enter image description here

Now if we look at the viewport, it should look like this.

enter image description here

Now we can add the red and green cubes. These are just graphics to make it look pretty and are actually irrelevant for the basic mechanics. The system already works fine (skip to the end if you don't need it to look good).

Now, both your red and green cubes should be no-collision and both parented to the main empty (still named Arrow).

enter image description here

enter image description here

Now all we need to do is make them toggle their visibility depending on whether or not the selection is valid. we can do this by adding two visibility actuators to both Cube Red and Cube Green then we need to add another property sensor on the empty (yes, it is still named Arrow) to detect when the Boolean OkSpot? is false.

If you don't like reading but are ok with pictures, skip this part.

we already have a property sensor to detect when it is true, so we can re-use that one. If we connect them to the appropriate visibility actuators on both cubes, it should change color accordingly. I have named the and controllers ok and not to make connecting them simpler. The and controller that is connected to the property sensor that detects when OkSpot? is true, receives a positive pulse when the arrow is in a valid spot. because we are going to use the green cube to show when the selection is valid, it should be visible when the selection is valid. the red cube should not be visible when the selection is valid. connect the other *and * controller to the visibility actuators that are still unused.

enter image description here

Now that everything is working, we can test it again.

enter image description here

Everything seems to be working fine.

We are now going to add the button to toggle on/off the build system. If you only want it to work and look cool, skip to the very end where the blend files are. Doing this will make a big mess of the logic, and I recommend also downloading the simplified blend so you can get an idea of how this works without the messy logic of the working one.

We can now add the button to turn on/off the arrow.

We need an overlay scene with a camera and a button. the button can be something fancy, or just a plane with logic.

enter image description here

enter image description here

Now we can go back to the main scene and add a scene actuator to one of the objects to add the HUD scene. It is true that you could put it on any one of your objects, But I usually add an empty and put it somewhere where it is easily accessible. This empty holds all of the Game start/end and Scene add/remove logic.

enter image description here

We also need the mouse to display. In newer versions, this is much easier than before.

enter image description here

Now we need to modify the logic on the original empty (still named Arrow). When logic starts to get this messy, you can un-check the check boxes to make editing easier.

Note that doing this also disables the logic bricks so be sure to re-enable them before testing.

enter image description here

From the newly added logic on the Arrow, now when you place a building, it will deactivate the arrow.

We need to add a way so that clicking the Build button on the HUD will activate it add a new one.

enter image description here

We should test it now to see if this part is working. Don't forget to re-enable the logic that we un-checked

enter image description here

This part is working. However, we can still see the arrow if it isn't active. This can be fixed easily enough.

You need to significantly change the logic on your arrow to match this. (Simplified) enter image description here (Expanded) enter image description here

There are two blend files below. One is without that mess, one has it working.

We must test it again.

enter image description here

If you had any trouble following that, you can always look at, take apart, and/or modify the blend files.

Blend file without button to build (always has arrow active) - Logic is simple.

Blend file with button to build (Click button to activate arrow, click to place cube, clicking also deactivates arrow so you must click build button for each cube you want to add again) - full working demo. Logic is harder to understand.

  • $\begingroup$ For some reason the .gifs sometimes will not work :-( $\endgroup$ Commented Apr 10, 2015 at 19:14
  • $\begingroup$ @X-27 The second .blend file is missing $\endgroup$
    – J Sargent
    Commented Apr 12, 2015 at 19:08
  • 1
    $\begingroup$ @NoviceInDisguise Fixed it, GiantCowFilms was very nice and made that blend file #27 on the blend exchange server, and I still had the link in the question linking to the previous link location. $\endgroup$ Commented Apr 13, 2015 at 0:26
  • $\begingroup$ BTW you can use mouse wheel to rotate them. $\endgroup$
    – ruckus
    Commented Jul 7, 2015 at 15:43
  • 2
    $\begingroup$ 27, You are a true asset to the blender community!!! Thank you for your detailed answer!!! Knowledge is the gift that keeps on giving!!! Thanks again! $\endgroup$
    – user22190
    Commented Feb 25, 2016 at 3:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .