I'm currently working on a game that contains a handful of "data libraries" with various information to look up, which was completely fine to do entirely via GDScript dictionaries (containing ID numbers as keys, and custom objects as values) while I was setting up, but I'd like to swap over to a better structure now that I know it works.

I've been looking around at possible solutions (CSV, JSON, Resources, etc), but I'm mainly running into two issues with them:

  1. I need to be able to put (preferably anonymous) functions as some of the object fields. As far as I can tell, this isn't possible with either CSV or JSON, and it might be possible with Resources
  2. I want to have a single file per library, as opposed to one file per entry. This is fine with CSV, and can be fine with JSON, but from what I've seen it's not an option for Resources (I would need both a resource file and a script file for each entry, right?)

To give an idea what I'm working with, this is an entry in one of the library dictionaries I'm using at the moment:

"015": Card.new("Shield Bash", CardType.ATTACK_MELEE, {CardCost.STAMINA:1, CardCost.MOVE:1}, CardRarity.COMMON,
		[TargetType.ENEMY, TargetPattern.SHOT_3], 50, true, 1, [],
		"Runs up to target and deals 2 blunt damage. +1 damage for every tile moved. +2 damage if a shield is equipped.",
		func(s,t,card):
			var dmg = 2
			if s.get_shield(): dmg += 2
			var path = s.find_path(t.current_position)
			if path: dmg += path[1].size() - 2
			s.move_to(path[1][path[1].size() - 2], true, true)
			var di = DamageInstance.new(dmg)
			s.deal_damage(t, di, card))

Does anyone have any recommendations on how I could go about this? Worst comes to worst I can settle for having multiple files if there's no alternatives, but being able to store custom functions in each entry is a hard requirement.

  • xyz replied to this.

    paranoodle Make a resource class that contains all data and methods that operate on that data and save an object of that class.

      xyz How would I go about storing the anonymous function in that solution? And it would lead to one file per object, correct?

      • xyz replied to this.

        xyz I mean as in, where would I put the lambda that, in the example in my OP, every card has (which gets stored as an object variable). I can't just type code in a Callable export variable in a Resource, right?

        • xyz replied to this.

          paranoodle Use regular methods instead of lambdas. What exactly are those functions needed for?

            xyz Every entry has its own unique function though, that's the whole point, they're not class methods. They have to be stored/passed as Callable at some point.

            • xyz replied to this.

              paranoodle Callables cannot be serialized. You'll probably need to rethink your architecture.

                xyz Okay, thank you! That's the answer I was looking for then, I'll just stick to using a dictionary of objects if that's the case.