Any way to optimize this script?

I’m working on a rewind system that lets you record game play and re-watch/rewind the game. This works by having an always sensor with pulse mode on, attached to a script I wrote that saves the position, orientation, scale, and color of the objects in the scene that have properties with respective names so the script knows which objects to record and which ones not to. The script appends the worldPosition, localOrientation, localScale, and color of the object to a file. I know I can improve framerate by setting the skip rate of the sensor to more than 0 but that will decrease the “framerate” of the rewind playback. Is there any way in to optimize the script with opening/closing files and such to improve the framerate of the game? attached is the zip file with the game. extract it before opening it.

BGE Rewind.zip (134 KB)

I would suggest writing each “tick’s” data to a list, and at the end of the game (when recording stopped) write to file with json.dump(lines, file)

And each frame write every object to the same list


all_frame_data = [
{'obj1': [...], 'obj2': [...]},
{'obj1': [...], 'obj2': [...]},
{'obj1': [...], 'obj2': [...]},
]

This involves one read/write step

But if I add the data to a list, wouldn’t the list disappear each time the program is executed?

If you create an object in another py file that isn’t loaded every frame, only imported, you can import that dictionary into scripts and modify it without it being reset each frame.

dict.py:


myDict = {}

example.py:


from dict import myDict

...

You could also use logic.globalDict but I wouldn’t recommend it since it is a lot of data in memory that won’t be emptied when restarting the game.

Thanks everyone that really helped!

@mrputulips, if you import a module that won’t be freed until the game exits either.

You can store it in a game object, and have objects register themselves to the data container each frame. Yoy might use globaldict for that. Otherwise, have a manager iterate over all object, and store the data in the manager

Sent from my SM-G920F using Tapatalk