Sys.save and functions in tables (error)

Hey all, just started working on saving and attempted to use sys.save on the data I have so far and got an error “unsupported value type in table: function”

Before I go refactor things I just wanted to confirm that you can’t save functions inside tables? I tested some more with simple tables and this seems to be the case.

I structured most of my data in an OOP way using metatables and “class functions” but I’m not terribly deep so I can pull it all apart. I guess this isn’t the lua/Defold way but the Python roots are hard to shake :slight_smile:

Thanks!

3 Likes

Yup. You can’t. Functions cannot be serialised. Just tables, strings, numbers and booleans (and I think sys.save() also supports Defold hashes, but I’m not certain). What I do is that most of my game’s data stores have a .save() function that converts the data to a serialisable form and returns it and a .load(data), that does the opposite (unserialises the data and populates the data store with it).

4 Likes

Thanks for the quick response! That makes sense, same reason they can’t be sent through messages I guess.

Out of curiosity, what is the “Defold way” of structuring things? It’s my first project so I’m creating a simple turn based game inspired by rock, paper, scissors so it is going to be a lot of: player action > enemy action > determine result > update game state

Scrapping the OOP approach, my new thought was to structure it like:

  • game_functions.module to contain all the functions (possibly superfluous but does reduce clutter in scripts)
  • player_controller.script to import game_functions and trigger them via messaging
  • enemy_controller.script to import game_functions and trigger them via messaging
  • game_logic.script to take messages and return results messages to player_controller.script and enemy_controller.script
  • gui_scripts to handle gui updates as necessary

Example: Player selects move > triggers enemy NPC to select move > Game has to compare moves to check results > update game state (like health points) based on results

Hopefully I’m not making a mountain out of a mole hill with the approach

1 Like

Check out this thread: Game architecture in Defold
I already answered there

5 Likes

Thanks!!

2 Likes