V
V
Valery Albertovich Zhmyshenko2020-10-22 15:55:17
Lua
Valery Albertovich Zhmyshenko, 2020-10-22 15:55:17

How to get array values ​​from files in Love2d?

There is a game engine that is written in lua and uses the love2d framework, it (the engine) stores everything in arrays, for example, wall coordinates.
I want each level to be a separate file in the root of the program, for example game.exe/levels/1.level
I need to get array values, for example player = levelNumber.level(player) how to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dollar, 2020-10-22
@DIvan2000

First you need to decide on the storage format in the file. Since this is Lua, it will be easier to use some text format (rather than binary). Plus, the convenience will be that the file can be easily viewed and / or edited in a text editor.
Next, there determine for yourself what the format will be exactly. For example, you can do this:
57,49,1|45,60,1|40,30,0
Where the first two digits are X and Y, and the last one indicates whether the wall is vertical or horizontal. Separate walls are separated by the symbol |
You will, of course, have some other format, depending on the game.
Next, you need to read the documentation for Love2D on how to work with files. Try creating a file, writing, reading, etc. to get the hang of it. For example, write a numeric variable x to a file and then read it into a variable - y.
Then you need an algorithm that breaks the string into separate elements of the table. That is, first divides into pieces, where the separator is " |". And then each piece also needs to be divided by the separator " ,". Well, then we translate the string pieces into numbers using the Lua function tonumber(). They can then be packaged into any structure you want - any table, simple or complex.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question