Scripts / Can GGMAX read from and write to external ( lua ) files ?

Author
Message
ericman
19
Years of Service
User Offline
Joined: 16th Feb 2005
Location:
Posted: 7th Jun 2022 06:40
Hello,
I have an idea about a "serious game" ( edutainment game ) that would require a communication of data ( strings / values in a variable for instance ) between GGMAX and a third-party exe I would build with third-party software.

But as mentioned in the title : Can an exe generated with GGMAX read from and write to external ( lua ) files ( at runtime )

Is there an example around please ?
Thanks !
PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 7th Jun 2022 08:38
lua can read and write from files using the os commands but it won't be able to execute the modified lua code itself as the script won't be reloaded (might be possible if you edit for another map i guess), normally this is only used for writing to text files to get some variable values etc.
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
ericman
19
Years of Service
User Offline
Joined: 16th Feb 2005
Location:
Posted: 7th Jun 2022 10:43
Thanks for your answer!
Actually just reading / writing from a text file would be enough, since it would just be about storing / retrieving data : string ( text ) or value.
==> so I guess from your answer that a .txt file would work.
==> Maybe .ini file too ? That would be great.

I mentioned lua ( I'm no programmer ) in case GGMAx could only interact with lua scripts.

PM
3com
10
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 7th Jun 2022 12:25
@ ericman
Please look at savegame/loadgame.lua files are writing and reading text files there.
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics
cpu mark: 10396.6
2d graphics mark: 947.9
3d graphics mark: 8310.9
memory mark 2584.8
Disk mark: 1146.3
Passmark rating: 3662.4

PM
ericman
19
Years of Service
User Offline
Joined: 16th Feb 2005
Location:
Posted: 7th Jun 2022 15:58
Hello, thank you @3com, I will take a look.

( First read ended up with " ouch" for me the non programmer
PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 7th Jun 2022 19:29 Edited at: 7th Jun 2022 19:30
if you are creating the file it doesn't really matter what extension you give it as the data put in will be managed by you (in most cases this will be human-readable string)

it's pretty straight forward, just need to know the arguments for the operations (i.e. if you want to overwrite or append etc)
to write to a file
--open a file to write to ("w" mode should make a new file if it doesn't exist)
file = io.open("myfile.dat", "w")
io.output(file)
--the actual data you want to write ("\n" makes a new line after)
io.write(variable1.. "\n")
--you can do this part multiple times if you want to break the data up
--eg
io.write(variable2 .. "\n")
--close your file so it saves and you can open it again later etc
io.close(file)


now you can access your file using a similar method
--the file you want to open to read from ("r" means read mode)
file = io.open("myfile.dat", "r")
--good idea to check the file exists before getting data from it
if file ~= nil then
io.input(file)
--to read each line use "*line" or to read the whole file use "*all" or nothing
--so to read our saved file with 2 lines getting them individually we could use
value1 = io.read("*line")
value2 = io.read("*line")
io.close(file)
end
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
ericman
19
Years of Service
User Offline
Joined: 16th Feb 2005
Location:
Posted: 8th Jun 2022 15:32
Thank you smallgg, I will study your code above.
PM

Login to post a reply

Server time is: 2024-11-23 07:59:09
Your offset time is: 2024-11-23 07:59:09