Comparing properties

Hi guys,

I was playing around in the game engine, and was hoping that I could use a sensor to compare the value of two properties in an object and output true or false based on the outcome. However it does not seem that it is possible and I have found no help. It seems like something really basic that I shouldn’t have to deal with python for.

What would be the easiest or most intuitive way to compare values?

what do you want to do if it is true?

is this something that needs to happen 1 time, or all the time or?

You can use the expression controller

define two properties:

Compare them when they change:


True Level Triggering -> perform the evaluation on subsequent changes to the property
Evaluation Type: Changed -> it does not matter how the value changed, just that it changed. The how will be checked by the expression controller
Property: value Observe the changing properties (e.g. if you can guaranty maxValue never changes you can skip the according sensor)

Expression: You can evaluate property values by property name and sensor states by sensor names.

Examples:
“value <= maxValue” corresponds to Python code


controller = bge.logic.getCurrentController()
owner = controller.owner
if owner["value"] &lt;= owner["maxValue"]:               # value &lt;= maxValue
    for actuator in controller.actuators:
        controller.activate(actuator)
else:
    for actuator in controller.actuators:
        controller.deactivate(actuator)

“not Collision and value <= maxValue” corresponds to Python code


controller = bge.logic.getCurrentController()
owner = controller.owner
if (not controller.sensors["Collision"].positive and      # not Collision and 
    owner["value"] &lt;= owner["maxValue"]):             # value &lt;= maxValue
    for actuator in controller.actuators:
        controller.activate(actuator)
else:
    for actuator in controller.actuators:
        controller.deactivate(actuator)

Remark: When you use the expression controller the according property and sensor names should begin with
a…z, A…Z, _
and can continue with:
a…z, A…Z, _ , 0…9

[table=“width: 500, class: outer_border”]

[th]fine[/th]
[th]invalid[/th]


A4
4A


maxValue
max value


max_value
max-value

[/table]

Watch the console for potential error messages like this:


[1.250000Error in Errorhandling routine.[max.Value not found]]

+1 to monsters idea

Thank you very much Monster! I’ll be sure to look at other uses for this as well! You’re so active on here and never disappoint. :slight_smile:

Sorry for the double post, I’m still wrapping my head around math with properties.
I’ve set up a property that basically acts as a display for another property value, but only the last two digits, so when the value is 242, it will just display 42.

I did this by subtracting 100 until the value was 99 or below. However, this “recursive” way of doing it is too slow, as it takes several logic ticks to complete for higher values. An easy way to do it in one step would be to round down the 242 to 200 and subtract that instead.

Question: can I round properties, or is there another way to do this in a number of steps independent of the size of the value?

You can do that with a little python magic.

Define your two properties. One Integer called ‘bigValue’, and one Integer called ‘smallValue’ (the one we want the ‘chopped-off’ number to go)
Run this script with a Property sensor (like Monster set up above) set to ‘changed’ and bigValue as the value (the script should only need to run when this value changes)…


from bge import logic

own = logic.getCurrentController().owner
value = str(own['bigValue'])[-2:]
own['smallValue'] = int(value)

We are converting the integer value of bigValue to a string and using list slicing to return only the last two characters. We then convert the sliced string back to an int and set it as the value of smallValue.

You can use the modulo operation at the Property Actuator

Mode: Assign
Property: last2Digits
Value: value % maxValue

This always works. There is no need to check if the value is over a certain limit.