Scripts / Writing to file with lua

Author
Message
Flatlander
GameGuru Master
17
Years of Service
User Offline
Joined: 22nd Jan 2007
Location: The Flatlands
Posted: 25th Aug 2018 21:08 Edited at: 28th Aug 2018 19:54
The following is the code I used for an example of how to write to a file information you would like to save about your game. This is straightforward and uses one line of text that will be written to the file. I use parser characters (you use whatever character you want) to separate the bits of data. I use the "|" bar character. The need for parsing was needed because packets of data were sent through the telephone lines one after another forming a huge string of data. This data then needed to be parsed out into the individual packets on the receiving end. Each packet had its own specific need.

The code is loaded with comments.



This is the contents of the file. It's actually one line. I have word-wrap turned on.


Of course, just writing to a file doesn't do any good unless you can read the file. I really haven't looked at methods/commands to do this yet. Also, I'm not sure how to parse out the information as yet. Also, I think I read somewhere that lua will allow you to save table/arrays to a file. Coming soon I guess.
Alienware Aurora R7 with SSD 256GB boot drive ( C: ) and a secondary drive ( D: ) that is 2TB
Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz 3.19 with Intel Turbo-burst
Installed RAM 16.0 GB
64-bit operating system, x64-based processor
Windows 10 Home
NVIDIA GeForce GTX 1070 with 8192 MB GDDR5 and 8095 MB shared system memory
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 26th Aug 2018 09:27
You should take a look at the default save and load system as it does this already, you can copy the same system
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
Flatlander
GameGuru Master
17
Years of Service
User Offline
Joined: 22nd Jan 2007
Location: The Flatlands
Posted: 26th Aug 2018 19:41
Thanks smallg; but I'm doing this to exercise my little grey cells. I am experiencing dementia in my old age and one way to combat or at least prevent it from getting worse too quickly is for me to use my brain as much as I can. Since I was a master programmer in the past I want to use this new language (lua is new to me) to do this. Writing the procedures into a diary or a post in the forums helps with the process. I figured there are some people who would like to also learn more about lua in a step by step manner. I know you want to help, but, let me have my fun. :LOL:
Alienware Aurora R7 with SSD 256GB boot drive ( C: ) and a secondary drive ( D: ) that is 2TB
Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz 3.19 with Intel Turbo-burst
Installed RAM 16.0 GB
64-bit operating system, x64-based processor
Windows 10 Home
NVIDIA GeForce GTX 1070 with 8192 MB GDDR5 and 8095 MB shared system memory
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 27th Aug 2018 09:45
Not encouraging you to stop at all, merely helping with
Quote: "Of course, just writing to a file doesn't do any good unless you can read the file. I really haven't looked at methods/commands to do this yet. Also, I'm not sure how to parse out the information as yet. Also, I think I read somewhere that lua will allow you to save table/arrays to a file. Coming soon I guess."

By pointing you in the direction of a place you can learn from a working example
Though it might be a bit more complicated than you need, in the end the commands and process you want are very similar to the ones you used to write the file in the first place.
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 27th Aug 2018 18:34
Probably the easiest way is to write the table straight out to a file and read it straight back in, i.e. in the file it will be stored as a Lua script so when you read it back in it simply executes the script making the data available again. You can store all of your game data in a single global table which makes it really easy to store/load.

Been there, done that, got all the T-Shirts!
PM
Tarkus1971
Audio Media Maker
9
Years of Service
User Offline
Joined: 24th Feb 2015
Location: England, UK
Posted: 28th Aug 2018 12:24
This is something I am going to try and do, save / load global table, more written paper work to do as "flags" set to 0 or 1 would indicate "task not done" or "task done", then have lua check and sort out what needs to be altered in the game world.

ie

if "table entry 1" = 1 then
Destroy(barricade1)
end

Would need to convert player position and save that to, but it seems easy enough.
Aftershock Quad Core AMD FM2+ 3.5 GHz 8GB Motherboard and Processor, A7700k apu, Asus GT970 STRIX 4gb Nvidia gfx card.
King Korg Synth, Alesis SR18 Drum Machine, Akai MPX8 sample player, Roland Fantom XA Synth, Axus Digital AXK2 Digital Drum Kit, Novation Ultranova Synth, Waldorf Blofeld Synth, Roland D05 Synth Module, Bluedio Victory V Headphones, AKG K141 Studio Headphones, Lenovo Ideapad, with Windows 10 64bit, 8Gb Ram and AMD A10 7th Gen Graphics.
Flatlander
GameGuru Master
17
Years of Service
User Offline
Joined: 22nd Jan 2007
Location: The Flatlands
Posted: 28th Aug 2018 19:42 Edited at: 28th Aug 2018 19:56
Now that we have a file written on the hard disk, "C:\test\example.txt" we can then open the file and read it into a variable. From there we can put each packet of parsed data into a table/array. I consider this an array because I use numerical indices. Everyone is welcome to use the function to parse their strings within their game level.

What I'm doing is reading reference manuals about lua and sharing with you what I'm learning on the way.



Alienware Aurora R7 with SSD 256GB boot drive ( C: ) and a secondary drive ( D: ) that is 2TB
Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz 3.19 with Intel Turbo-burst
Installed RAM 16.0 GB
64-bit operating system, x64-based processor
Windows 10 Home
NVIDIA GeForce GTX 1070 with 8192 MB GDDR5 and 8095 MB shared system memory

Login to post a reply

Server time is: 2024-12-22 19:23:15
Your offset time is: 2024-12-22 19:23:15