2
$\begingroup$

Here is my code, I think I have a way to have it do what I want, just not sure how to access a custom property on an object. I thought the code I had was right, but I feel like I'm missing something (or did something wrong).

The goal is so that whenever I hit Tab it increases the value of the custom property 'unit' by 1, thus changing which trackObject I am placing when I click. (the TAB/Property change is done in the logic bricks). At this point I know I'm trying to use the property in the code, just pretty sure I'm typing it out wrong.

import bge
import bpy
from bge import render
render.showMouse(1)

def main():

cont = bge.logic.getCurrentController()
own = cont.owner

scene = bge.logic.getCurrentScene()

trackObject1 = scene.objects ["trackObject1"]
trackObject2 = scene.objects ["trackObject2"]
trackObject3 = scene.objects ["trackObject3"]
trackObject4 = scene.objects ["trackObject4"]
trackObject5 = scene.objects ["trackObject5"]

unit = bpy.data.objects["player"]["unit"] #pretty sure this isnt right...   

mouseOver = cont.sensors ["mouseOver"]
click = cont.sensors ["click"]
readyMove = cont.sensors ["readyMove"]

trackTo = cont.actuators ["trackTo"]
move = cont.actuators ["move"]

if mouseOver.positive:
    target = mouseOver.hitObject
    if click.positive and unit == 1:
        trackObject1.worldPosition = mouseOver.hitPosition 
        else if click.positive and unit == 2:
        trackObject2.worldPosition = mouseOver.hitPosition
        else if click.positive and unit == 3:
        trackObject3.worldPosition = mouseOver.hitPosition
        else if click.positive and unit == 4:
        trackObject4.worldPosition = mouseOver.hitPosition
        else if click.positive and unit == 5:
        trackObject5.worldPosition = mouseOver.hitPosition


if readyMove.positive:
    cont.activate(move)
    cont.activate(trackTo)

if own.getDistanceTo(trackObject1) < 2:
    else if own.getDistanceTo(trackObject2) < 2:
    else if own.getDistanceTo(trackObject3) < 2:
    else if own.getDistanceTo(trackObject4) < 2:
    else if own.getDistanceTo(trackObject5) < 2:
    cont.deactivate(move)
    cont.deactivate(trackTo)

main()
$\endgroup$
1

1 Answer 1

2
$\begingroup$

You debugged part of your problem in your code. You mixed blender's api (bpy), in with the game engine's (bge).

The line unit = bpy.data.objects["player"]["unit"] will do nothing in the game.
You already defined the scene in your script, so that line with bpy needs to be:

unit = scene.objects["player"]["unit"]

That line is accessing the game property "unit" on the object named "player".


Your last if statement is not correct. You need something in the if statement's bodies, even just pass will work.

$\endgroup$
2
  • $\begingroup$ Thank you very much for the help, i got it to work! I'm sure ill be asking more questions later, but for now I can continue! $\endgroup$ Commented Dec 20, 2016 at 20:03
  • 1
    $\begingroup$ @TimFromInternet since this solved your problem, please accept the answer. That makes this as solved, and lets other know that there is an answer that worked for you. $\endgroup$
    – David
    Commented Dec 21, 2016 at 5:41

You must log in to answer this question.

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