Thanks for the reply! The unfortunate thing is that not many people seem to be developing games yet, despite the fact that it seems that many of us working on MicroPython projects are learning while doing, so hopefully others in the community can find inspiration to make programs of their own. As I'm keen to finish up NanoMem and figure out the mystery that will be combining animation and velocity in my second project (I expect SaurRun has some functions that might be helpful to learn from in that regard), I'll see if I can finish my function rewrites and implementation of save data saving/loading in the next few days.
If anyone working on their own Thumby projects might be able to help discern how to use the following functions from pickle to serialize and deserialize variables and lists to a defined txt file, I'll post what I've gathered so far on the subject:
The following is the pickle function, edited to allow for its use without a seperate pickle module:
# Save data serialization
HIGHEST_PROTOCOL=0
def dump(obj,f,proto=0):
f.write(repr(obj))
def dumps(obj,proto=0):
return repr(obj).encode()
def load(f):
s=f.read()
return loads(s)
def loads(s):
d={}
s=s.decode()
if "(" in s:
qualname=s.split("(",1)[0]
if "." in qualname:
pkg=qualname.rsplit(".",1)[0]
mod=__import__(pkg)
d[pkg]=mod
return eval(s,d)
If it helps, the following description was also listed in the documentation describing the function, though it wasn't too helpful for me:
import pickle
data = {'1':b'test\nmore', '2':1.414, '3': [11, 12, 13]}
s = pickle.dumps(data)
print('Human readable data:', s)
v = pickle.loads(s)
print('Decoded data (partial):', v['1'])
Human readable data: {"2": 1.414, "3": [11, 12, 13], "1": "test\nmore"}
Decoded data (partial): test
more
s = ujson.dumps(data)
# pickle produces a bytes object whereas ujson produces a string
u.write(s.encode())
# Pickle:
# u.write(s)
u.write(b'\n')
# receiver
s = u.readline()
v = ujson.loads(s) # ujson can cope with bytes object
# ujson and pickle can cope with trailing newline.
The following was my initial attempt at adding theoretical variable saving functionality to NanoMem, and while it includes what I think is the intended method of opening, reading, and writing to files, I have yet to figure out how to combine it with the pickle functionality to save and load the variable that determines whether or not the user has unlocked hard mode and the index of six high score values that are currently saved if the current score following a "desync" is greater than one of the three saved high scores for the current gamemode:
thumby.files.openFile(/Games/NanoMem/data.txt)
thumby.files.readFile(1)
if(thumby.files.readFile()=='h'):
h=1
thumby.files.closeFile()
bl(s1e2,33,29,6,7)
elif(thumby.files.readFile()=='r'):
thumby.files.closeFile()
else:
thumby.files.openFile(/Games/NanoMem/data.txt,w)
thumby.files.writeFile(r000000000000000000000000)
thumby.files.closeFile()
Should it be of any use, the following is the aforementioned function that saves high scores in NanoMem, with an i value of range three for the three high score values saved, which itself is defined by the hard_mode variable that places it in the corresponding sub-index of the highScores index:
for i in range(3):
if s>highScores[hard_mode][i]:
highScores[hard_mode].insert(i,s)
del highScores[hard_mode][3]
break
Assuming an efficient function for saving and loading variable data is standardized, perhaps it will prove helpful to future users in saving time in developing their own games.