3
$\begingroup$
from bge import logic
path = logic.expandPath("//")

def save():
    cont = logic.getCurrentController()
    own = cont.owner 
    # 'info' is what will be saved to the file.
    # Example:
    # info = str(*What you want to save*)

    info = str(own['pink'])+","+str(own['red'])

    file = open(path+str(own)+".txt", 'w')    
    file.write(str(info))


def load():
    cont = logic.getCurrentController()
    own = cont.owner

    file = open(path+str(own)+'.txt','r')
    line = file.readline().replace('\n','').split(',')
    own['pink'] = bool(line[0])
    own['red'] = bool(line[1])
$\endgroup$
1
  • 1
    $\begingroup$ I'm voting to close this question as off-topic because because it is fundamentally about how python interprets the string representation of a Boolean. (not a bad question, just wrong place) $\endgroup$
    – zeffii
    Sep 11, 2016 at 10:41

2 Answers 2

1
$\begingroup$

instead of casting to a boolean you can use them in an expression:

file = open(path+str(own)+'.txt','r')
line = file.readline().split(',')
own['pink'] = line[0] == "True"
own['red']  = line[1] == "True"
$\endgroup$
1
  • 1
    $\begingroup$ I want to add that "bool(string)" will return False when the string is empty (or None), otherwise it returns True regardless of the content $\endgroup$
    – Monster
    Jul 14, 2016 at 5:38
1
$\begingroup$

fundamentally this is a Python specific question / misunderstanding

some_string = "True, False, False, True, False"
items = some_string.split(', ')

print(items)
# ['True', 'False', 'False', 'True', 'False']

print([bool(i) for i in items])
# [True, True, True, True, True]

if you really want to store bools as their string representation your option would be to to use eval.

[eval(i) for i in items]
# [True, False, False, True, False]

or testing directly for equivalence using i=='True' as suggested in the first answer you got.

[(i == 'True') for i in items]
# [True, False, False, True, False]

but you can also (and probably should) store them as their integer-string representation

some_string = "1, 0, 0, 1, 0"
items = some_string.split(', ')

print(items)
# ['1', '0', '0', '1', '0']

print([bool(int(i)) for i in items])
# [True, False, False, True, False]
$\endgroup$

You must log in to answer this question.

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