Page 1 of 1

file writing (more of general lua I know)

Posted: 09 Jul 2009, 12:54
by Pendrokar
I am working on a building template widget. And I will need a database file where templates would be put.

It would look like this -

Code: Select all

gamename - ba
uniticon - <setuniticon>
templatename - <mytemplate>
new <unitdefid>, <unitname>, <facing>, <posx>, <posz>
new <unitdefid>, <unitname>, <facing>, <posx>, <posz>
new <unitdefid>, <unitname>, <facing>, <posx>, <posz>
...
<another template>
<another template>
...
--|
"--|" ends templates adding to the particular game

I try to save/add templates with file: like this
- http://pastebin.com/m7e6b3133

I dont really know if that works haven't tested because I don't have Spring at work. But the question is about overwriting lines. Because there can be the option to change template name so I have to modify the file where I need to overwrite a line but not when adding a template. :?

Or can someone point out a easier way? such as adding a LUA file which has arrays added? Though writing to files would still be needed.

Re: file writing (more of general lua I know)

Posted: 09 Jul 2009, 14:34
by imbaczek
look around, iirc spring already handles writing to files, check widgethandler for configuration dumping.

lua tables will be easiest, since you're just an include away from having the data parsed and ready to use.

Re: file writing (more of general lua I know)

Posted: 09 Jul 2009, 14:44
by Tobi
Here is an example, writes out a Lua file to save, and loads and runs this Lua file to load. This Lua file then calls some functions which set up the data structure I wanted to save. (AddWaypoint and AddConnection, in this case.)

This is an example of how the data file may look.

Re: file writing (more of general lua I know)

Posted: 09 Jul 2009, 16:44
by Pendrokar
Thanks! Will look into it...

Re: file writing (more of general lua I know)

Posted: 09 Jul 2009, 23:09
by Super Mario
Thanks man for making this Widget that I previously suggested. :-)

Re: file writing (more of general lua I know)

Posted: 17 Jul 2009, 22:10
by Pendrokar
Tobi wrote:Here is an example, writes out a Lua file to save, and loads and runs this Lua file to load. This Lua file then calls some functions which set up the data structure I wanted to save. (AddWaypoint and AddConnection, in this case.)

This is an example of how the data file may look.
Hmm

Code: Select all

  local fname = "craig_maps/" .. Game.mapName .. ".lua"
  local f,err = io.open(fname, "w")
When I try this I always get the error no matter if I create the directory and the file?

Code: Select all

       local game = Game.modShortName
       local fname = "building-templates/" .. game .. ".lua"
       local f,err = io.open(fname, "w")

Re: file writing (more of general lua I know)

Posted: 17 Jul 2009, 22:30
by jK
only widgets can use the io-lib (gadgets still have read access via vfs)

Re: file writing (more of general lua I know)

Posted: 18 Jul 2009, 08:55
by Evil4Zerggin

Re: file writing (more of general lua I know)

Posted: 20 Jul 2009, 14:01
by Tobi
Pendrokar wrote:I always get the error
Which error?
Evil4Zerggin wrote:Another example, using table.save: http://spring1944.svn.sourceforge.net/v ... iew=markup
Cool, didn't even know there was a table.save :-)

Re: file writing (more of general lua I know)

Posted: 20 Jul 2009, 15:04
by Pendrokar
Tobi wrote:
Pendrokar wrote:I always get the error
Which error?
Cannot find directory or file.

Re: file writing (more of general lua I know)

Posted: 20 Jul 2009, 23:23
by Pendrokar
Evil4Zerggin wrote:Another example, using table.save: http://spring1944.svn.sourceforge.net/v ... iew=markup
This worked...

Although I thought the starting directory is already LuaUI\Widgets\!

Thanks!