Skip to main content
Tweeted twitter.com/StackBlender/status/812699605066514432
deleted 190 characters in body; edited tags; edited title
Source Link
David
  • 49.7k
  • 40
  • 162
  • 318

Using a custom property in if statements in the BGE

Alright so I've been stuck on this, I tried to ask earlier, but I didn't explain it very well. I have a new approach, just not sure I'm doing it right.

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

The goal is so that whenever I hit TABTab 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. At this point I know imI'm trying to use the property in the code, just pretty sure imI'm typing it out wrong. Anyone able to show me what I'm doing wrong?

Using a custom property in if statements

Alright so I've been stuck on this, I tried to ask earlier, but I didn't explain it very well. I have a new approach, just not sure I'm doing it right.

Here is my code, I think i have a way to have it do what i want, just not sure how to call to a custom property on an object, I thought the code I had was right, but i feel like im 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 im trying to use the property in the code, just pretty sure im typing it out wrong. Anyone able to show me what I'm doing wrong?

Using a custom property in if statements in the BGE

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.

Source Link

Using a custom property in if statements

Alright so I've been stuck on this, I tried to ask earlier, but I didn't explain it very well. I have a new approach, just not sure I'm doing it right.

Here is my code, I think i have a way to have it do what i want, just not sure how to call to a custom property on an object, I thought the code I had was right, but i feel like im 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 im trying to use the property in the code, just pretty sure im typing it out wrong. Anyone able to show me what I'm doing 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()