Best way to store data as tables

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
UnicronLucas
Prole
Posts: 3
Joined: Mon Jan 30, 2023 10:42 pm

Best way to store data as tables

Post by UnicronLucas »

I may be completely overthinking this, but I am curious about the best way to store large data. I am trying to make my first game, and it will be a card game, similar to Magic: the gathering. But I am not sure how to store the data, right now I am writing a big old lua file that will have like a parent class for all cards with card specific Id's and card types, then children classes for each card type aka player cards or magic cards. after I create those I am going to write code that will input cards one by one. Is there a better way to do this? I keep thinking I am doing something wrong or maybe overthinking it?

thanks in advance.
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

Re: Best way to store data as tables

Post by BrotSagtMist »

Funny that we have this topic again today.

I would personally use a format in the form of

Code: Select all

name val1 val2 val3 TEXT
name val1 val2 val3 TEXT
name val1 val2 val3 TEXT
This can be read pretty comfortable with something like:

Code: Select all

 for line in love.filesystem.lines("Cards") do
  local a,b,c,d,e=string.match(line,"(%g*)%s?(%g*)%s?(%g*)%s?(%g*)%s?(%g*)")
  Cards[#Cards+1]={name=a,attack=b,defense=c,description=d,e}
 end
--or if subtables are needed just name it.
  a,b,c,d,e=string.match(line,"(%g*)%s?(%g*)%s?(%g*)%s?(%g*)%s?(%g*)")
  Cards[a][#Cards[a]+1]={b,c,d,e}
As long as all cards have a similar amount of data fields parsing this way stays easy.
Oh yea this also means that the cards ID is automatically its line number in the cards file, be smart, save a step.
Generating stuff as lua file works good on read in, but the generating step can quickly grow out of hand so its probably not optimal for large data amounts.
obey
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: Best way to store data as tables

Post by RNavega »

UnicronLucas wrote: Mon Jan 30, 2023 11:12 pm after I create those I am going to write code that will input cards one by one. Is there a better way to do this? I keep thinking I am doing something wrong or maybe overthinking it?
What does "input" mean in this context?
UnicronLucas
Prole
Posts: 3
Joined: Mon Jan 30, 2023 10:42 pm

Re: Best way to store data as tables

Post by UnicronLucas »

I meant like I create a class that has cards, and then I use like

Code: Select all

 card1 = Card:new(alot of stuff here that say what the card can do) 
this just felt very, not smart to me, and like there should be a better way to do it
UnicronLucas
Prole
Posts: 3
Joined: Mon Jan 30, 2023 10:42 pm

Re: Best way to store data as tables

Post by UnicronLucas »

BrotSagtMist wrote: Tue Jan 31, 2023 2:21 am Funny that we have this topic again today.
And sorry I tried to search it, but I wasn't sure what to search for now I think I might go for your second approach, as I am thinking of different card types having different parts so I think I would need subtables, a table for each card type.

your way does make sense to me though, thank you!
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Best way to store data as tables

Post by darkfrei »

I am using the tab-separated values Tab-separated values. Not this one, but other way:
The first value is a index or key, the second is a value, but if the third is available then second and all next tab-saparated values are in list (sequence).
Limitations: no correct saving for tab symbol in any string; no tables inside the saved table (except list).
Pro: readability, safe.


So the save file is just

Code: Select all

--savegame.tsv
b	yes
d	text
c	false
a	true
e	2	3	4	true	text
1	1
2	true
3	false
4	text
5	long text with spaces
6	list	with	tabs
true	yes
false	no

--	comment example
also comment while no tabs
this file has priority while not saved to the appdata
Loading and saving of values:

Code: Select all

local ST = require ('save-tsv')

function love.load()
	NiceTable = ST.load ("savegame")
end

Code: Select all

function love.keypressed(key, scancode, isrepeat)
	if scancode == "s" then
		ST.save ("savegame", NiceTable)
	elseif scancode == "x" then
		ST.remove ("savegame")
		NiceTable = ST.load ("savegame")
	elseif key == "escape" then
		love.event.quit()
	end
end
2023-01-31T10_19_06-Untitled.png
2023-01-31T10_19_06-Untitled.png (20.26 KiB) Viewed 3264 times
Attachments
save-tsv-02.love
(1.92 KiB) Downloaded 86 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: Best way to store data as tables

Post by RNavega »

UnicronLucas wrote: Tue Jan 31, 2023 3:12 am I meant like I create a class that has cards, and then I use like

Code: Select all

 card1 = Card:new(alot of stuff here that say what the card can do) 
Thanks for clarifying.

Because of 'information entropy' (the limit of how concise you can be when describing a complex thing), there will always be some place where you'll need to have that list of properties / alot of stuff about the card.

So in your place I'd just use what I'm more comfortable with, even if it's defining the entire card deck directly in a Lua file.
The goal here is to get to the gameplay part already so you can invest the most of your time in balancing your game and making it fun. Like "pick your battles", I can optimize / convert the cards format later if it's not working for me.
User avatar
deströyer
Prole
Posts: 32
Joined: Thu Jun 27, 2013 7:59 pm
Contact:

Re: Best way to store data as tables

Post by deströyer »

Depends a bit on the type of data (eg. is it information about GUI layouts, a tilemap, etc.) but for a bunch of different game elements with varying abouts of stats/properties I usually use just a big list of tab separated values in the form
index - stat - value - comment (if any)

eg. my gamedata.txt looks like this

Code: Select all

weapon_cutlass	weaponDamage	15
weapon_cutlass	weaponVariance	0.33
weapon_cutlass	weaponSpeed	10
weapon_cutlass	requiredStatType	strength
weapon_cutlass	requiredStatAmount	9
weapon_cutlass	damageType	slashing
weapon_cutlass	equipSlot	equip_weapon
weapon_cutlass	image	gameObjects
weapon_cutlass	quad	blade_03

armor_cloth	armorValue	5
armor_cloth	requiredStatType	strength
armor_cloth	requiredStatAmount	8
armor_cloth	equipSlot	equip_armor
armor_cloth	image	gameObjects
armor_cloth	quad	armor_01

armor_leather	armorValue	15
armor_leather	equipSlot	equip_armor
armor_leather	image	gameObjects
armor_leather	quad	armor_03
armor_leather	requiredStatType	strength
armor_leather	requiredStatAmount	8
then I have a string.gmatch( ) that checks through each line, adds it to a table, and when I reference the key armor_leather I get all the stats and values for armor_leather, for example
It's pretty flexible and hasn't fallen over on me.
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Best way to store data as tables

Post by darkfrei »

Oh, here is a nice way to do something like list of all values:
Image

https://forums.factorio.com/viewtopic.php?t=45107
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests