Comparing Properties

Hi All,

I am trying to set up a high-score system for a game I am developing. I would like this system to save a property to a txt file only if the property value is less than another property in my game, however the property saves to the text file even when it is greater than the other. Here is the python script I am using:

import bge

def save(cont):

own = cont.owner
owner = controller.owner
controller = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()
cube = scene.objects ["CHARACTER"]
maze = scene.objects ["MAZE"]

#properties
prop = (own['prop'])
load =  (own['load'])


save_key = cont.sensors['save']

path = bge.logic.expandPath("//")


if save_key.positive and if cube ["prop"] < maze ["load"]:
  
    with open(path + "save.txt", 'w') as savefile:
        savefile.write("{0}".format(str(prop)))

Thank you for your help!

if save_key.positive and if cube [“prop”] < maze [“load”]:

Possibly the problem is the second “if”.



if save_key.positive and cube ["prop"] &lt; maze ["load"]:

Looks like you just haven’t indented the code:


if save_key.positive and if cube ["prop"] &lt; maze ["load"]:
    with open(path + "save.txt", 'w') as savefile:
    savefile.write("{0}".format(str(prop)))

Thanks for the help,
I tried both of your suggestions but the property still saves even if it is greater that the other property.
The property is a timer, is it possible that the second half of the if then statement:
"cube [“prop”] < maze [“load”] is found to be true while the timer is counting up? If so how can I make this statement so both conditions have to happen at the same time?

Thanks.

OMG! sorry not:
if save_key.positive and if cube [“prop”] < maze [“load”]:
try:
if save_key.positive and cube [“prop”] < maze [“load”]:

The code should look like:



import bge

def save(cont):

    own = cont.owner
    owner = controller.owner
    controller = bge.logic.getCurrentController()
    scene = bge.logic.getCurrentScene()
    cube = scene.objects ["CHARACTER"]
    maze = scene.objects ["MAZE"]

    #properties
    prop = (own['prop'])
    load = (own['load'])

    save_key = cont.sensors['save']

    path = bge.logic.expandPath("//")

    if save_key.positive and cube ["prop"] &lt; maze ["load"]:

        with open(path + "save.txt", 'w') as savefile:
        savefile.write("{0}".format(str(prop)))

The fact that the code is getting to the point of actually writing the ‘save.txt’ is very strange. Do you see any errors in the system console? I don’t use the BGE so I don’t know how the save function is called and which property cube[“prop”] or maze[‘load’] holds the incrementing timer, however you can just add a print statement like:


if cube ["prop"] &lt; maze ["load"]:
    print("YES! cube prop is less than maze load")
else:
    print("No! cube prop is not less than maze load")

Sorry this really needed to be posted in the BGE Support forum I think.

Printing may help for debugging.


print(cube["prop"])
print(maze["load"])
# also this will print True or False
print("cube ["prop"] &lt; maze ["load"])
# and check the types of your objects, see if they are same as what you are expecting.
print(type(cube["prop"]))
print(type(maze["load"]))