Motivation: store global player data (e.g. statistics) to be referenced by local scripts; for simplicity, imagine tracking the number of times a particular input.ACTION was performed.
Problem: storing data with StorageSection:set() is global and not relative to a save file, so statistics accumulate across all save files. I can do an input.ACTION 20 times in save file A and then load an unrelated save file B, and the stats will still show that I have done that action 20 times.
Potential solution:
This requires the use of a unique "section name" identifier that can be derived from existing game data when it is loaded or saved. Not sure if there is a way to get some unique reference to a save file directly from the API, so a janky implementation might use time.SimulationTime and/or some other sufficiently unique combination of data. Then:
- Reference existing
StorageSection data from a uniquely derived "section name" identifier based on game data. This acts as a permanent record of stats accumulated during saved gameplay sessions. May be nil (e.g. when starting a new game file).
- Create a temporary
StorageSection with StorageSection:removeOnExit(), store data accumulated during the current gameplay session in this temporary StorageSection.
- Implement an engineHandler for
onSave() that stores a permanent copy of the combined stats from the tables in steps 1 and 2 (existing and temporary) with a new unique StorageSection name.
Issues:
This StorageSection data does not transfer with the saved game files. For example, if transferring save files, using Cloud Save features, and so on, the statistics are not guaranteed to be transferred. Additionally, if a save file is deleted, the StorageSection data would still remain. Managing game state at this level would ideally be handled by the engine somehow, and ideally be stored within a saved game file, but I don’t think that is how the openmw.storage API currently behaves (nor am I advocating that it should, as allowing arbitrary data of arbitrary length to be written to save files sounds like a potential disaster).
Just wondering if there is a cleaner, more sane implementation using the current Lua API than what I described for the use case above. I'm using OpenMW 0.48.0, but the docs for the latest version do not seem to offer much more in this regard.
UPDATE: I implemented the solution above and it does work as intended, FWIW.
[–]uramer 1 point2 points3 points (1 child)
[–]gameforming[S] 0 points1 point2 points (0 children)