So I’m making an RTS unit system and I’m working on code for a building to produce units.
There are 3 objects. On layer 1 is “building” and on layer 2 is “unit” and “navunit”
all 3 have the property(int) “number” which starts at zero
unit has the property(bool) “selected” which starts at false but is set to true if the unit is clicked
unit always steers to navunit.
Here’s the code for the building when the ‘A’ key is pressed:
from bge import logic as g #Defining variables scene = g.getCurrentScene() object = g.getCurrentScene().objects cont = g.getCurrentController() owner = cont.owner #Declaring more variables number = owner["number"] #Increment number = number + 1 #Adding the objects unitname = scene.addObject("unit", owner) navname = scene.addObject("navunit", owner) #Setting the navigation unitname.actuators["Steering"].target = navname #Setting properties unitname["number"] = number navname["number"] = number
this is the code for the navunit.
Mouse is “mouse over any” and Click is left button. Both are connected to the script.
from bge import logic as g #Defining variables objects = g.getCurrentScene().objects cont = g.getCurrentController() owner = cont.owner #Getting sensors Mouse = cont.sensors["Mouse"] Click = cont.sensors["Click"] number = owner["number"] hitPos = Mouse.hitPosition #find associated unit for object in objects: if "selected" in object: if object["number"] == number: unit = object #Move when user clicks if Click.positive == True: if unit["selected"] == True: owner.position = (hitPos,hitPos,0.5)
The code doesn’t come up with any errors in the console. But when I run the game engine I get a weird problem.
When I press “A”, instead of making one unit it makes 2, with only one selectable. It makes one navunit though.
If I press “A” more than once, it still produces 2 units every time. But if I try to select a unit made later, then it removes the first navunit.
I’m attaching the blend so you can take a look if you want:
problematicCode.blend (478 KB)
Any help would be much appreciated,