Level module

Hi,

Not sure if you determined whether to use the database, but with your level structure you should probably just store it in the database as JSON.

As an example with your data:

local json = require("json") local sqlite3 = require("sqlite3") local path = system.pathForFile( "game.db", system.DocumentsDirectory ) local db = sqlite3.open( path ) --============================================================================= --== SAVE LEVEL DATA --============================================================================= local level = { graphics = { name = "graphics", { kind = "RoundedRect", x = 160, y = 180, height = 160, }, }, physical = { name = "physical", { kind = "Chain", chain = { 140,-740, 140,-500, 380,-500, 380,-380, 140,-380 }, connectFirstAndLastChainVertex = true }, { kind = "Rect", x = 540, y = 40, height = 240, }, }, elements = { { name = "finishPortal", x = 700, y = -780, radius = 30 }, { name = "aswa", x = 200, y = 110, directPaths = {{x=200,y=-360}, {x=200,y=110}}, paramsPaths = {useDelta=false, constantTime=2200, loop = true} }, }, } local lvl\_num = 1 local data = json.encode(level) --table to json local query = string.format("INSERT INTO levels VALUES (NULL, %d, '%s');", lvl\_num, data) db:exec(query) --============================================================================= --== LOAD LEVEL DATA --============================================================================= local lvl\_num = 1 local query = string.format("SELECT data FROM levels WHERE level=%d LIMIT 1;", lvl\_num) for row in db:nrows(query) do local data = json.decode(row.data) --json to table print(data.graphics.name) --graphics end db:close()

I actually just created the database using the DB Browser for SQLite tool (calling it game.db) and put it in the Corona Documents directory sandbox folder. You should be able to create the initial database structure with this query:

db:exec[[CREATE TABLE "levels" ( id INTEGER PRIMARY KEY AUTOINCREMENT, level INTEGER UNIQUE, data TEXT NOT NULL );]]

-dev

hey, i didnt know if sqlite could store json objects like mysql and mssql, now i do :slight_smile:

thats absolutely the better choice in this case.

Well this seems just fine. Great!

If I was granted a last question I would like to know whether reading json decoding consumes many resources or whether it is fast

its fast enough and for your described purpose, you wont notice it.

Hi,

I agree with @anaqim, you won’t notice it.

-dev

Thanks to both!!

Now I have what I need

Hi!

Sorry if I use an old post but I think it is useless to open it a new one…

I have a question I did not think about. 

As I said long ago, I have a lot of modalities in my game. I wonder what the two alternatives are best:

1.Create a database containing all the modes, where each table is equivalent to a modality and contains all the levels

2.Create a database for each mode, each database has only one table that contains the levels of the modality

Of course the end result is the same but I would like to know what is the best choice for performance and speed

Databases are designed for two main things:

Lots of records and search for the few you need.

Data relationships.

If you’re just going to be reading in an entire table in memory and accessing the entire table, a database has more overhead than a flat file perhaps stored as a JSON string for easy parsing.

Multiple single-table databases seem like even more excessive overhead unless there are some relationships between the data (table of players, table of player moves indexed by an id in the player of tables). Clearly, multiple tables in a single database are less overhead than multiple databases. But again, unless you’re using the power of the database to filter data, why not just use flat files?

Rob

Thank you @Rob Miracle

But I do not think I understand it yet.I think that explaining my situation better, I hope I can help me to understand you.

I have several  files called “mode1.lua”, “mode2.lua”, “mode3.lua”  …etc

These files return me 1 table with all levels of that modality ( about 30000 lines, in some cases one level takes 1000 lines)

At this point, the user selects a mode and then a level. I need to retrieve that level and load it.

What is the fastest way to do this? Seeing the amount of data?

At the moment I was loading a record in each table for each level as suggested by @Develephant.

But then I remembered the previous question

Why not simply have mode1.lua, mode2.lua etc. as .lua files in your project and simply require them when you need them? You can for organizational purposes put them in a folder.

And to keep memory under control a bit, you can always un-require the modes you’re not using. (I don’t have the syntax for that off the top of my head, but it’s a couple of lines of code to cause Lua to forget about previously loading that module)

Rob

It seems a good idea, that’s what I had in mind at first.

My preoccupation is that loading the whole table of a mode is both expensive and slow. I have no experience about speed and performance for this I ask for advice here, I just want the best way

If you have it as one big table, that would be a huge performance hit. But if each mode is its own file, then you’re only loading what you need. 

I have a table for each mode.

However, this table contains all levels.

Loading them all is so serious if I only nerve one at a time?

I’m confused. Do you have multiple .lua files or do you have one .lua file with multiple tables? Or one really big table?

Rob

I’m sorry if I do not explain it well.

I started from the beginning.

I have:

mode_1.lua

local M = {} local levels = { --1 { --about 3000 line } --2 { --about 3000 line } --etc to 150 { --about 3000 line } } function M.getLv( num ) return levels[num] end return M

mode_2.lua

local M = {} local levels = { --1 { --about 3000 line } --2 { --about 3000 line } --etc to 150 { --about 3000 line } } function M.getLv( num ) return levels[num] end return M

.

.

.

.

And I have about 10 modalities but insert a new one at each update

To me of course it only serves one level of one mode each time.

Now could you advise me the best way for this?

(json, sqlite, keep these modules, or anything else)

Thanks again for the support

I apologize for the insistence but I do not know who else to turn to me…

I’m not sure I have a good answer for you. I think I understand better.

SQL seems to make sense for something like this because you could do:

SELECT levelData FROM modes WHERE mode = 1 AND level = 1

assuming that your database has columns named “mode”, “level” and “levelData” and that “levelData” is holding a JSON string that can easily be converted to a Lua table and that your table is called “modes”.

That way you are using the database to select a limited set of data from your total set.

Rob

Very well!

Thank you very much for your time

Forgive me if I come back here :frowning:

It will be the last time!

 At the moment save in system.DocumentsDirectory folder  json file like this:

mode_1.json

[{ "unlocked":true, "numLevel":1, "completato":false },{ "unlocked":true, "numLevel":2, "completato":false },{ "unlocked":false, "numLevel":150, "completed":false }]

(for each mod a file).

This is fine or even here is the better sql?

I will never stop saying thank you for the help