file writing (more of general lua I know)

file writing (more of general lua I know)

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
Pendrokar
Posts: 658
Joined: 30 May 2007, 10:45

file writing (more of general lua I know)

Post 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.
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

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

Post 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.
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

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

Post 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.
User avatar
Pendrokar
Posts: 658
Joined: 30 May 2007, 10:45

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

Post by Pendrokar »

Thanks! Will look into it...
Super Mario
Posts: 823
Joined: 21 Oct 2008, 02:54

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

Post by Super Mario »

Thanks man for making this Widget that I previously suggested. :-)
User avatar
Pendrokar
Posts: 658
Joined: 30 May 2007, 10:45

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

Post 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")
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

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

Post by jK »

only widgets can use the io-lib (gadgets still have read access via vfs)
User avatar
Evil4Zerggin
Posts: 557
Joined: 16 May 2007, 06:34

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

Post by Evil4Zerggin »

Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

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

Post 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 :-)
User avatar
Pendrokar
Posts: 658
Joined: 30 May 2007, 10:45

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

Post by Pendrokar »

Tobi wrote:
Pendrokar wrote:I always get the error
Which error?
Cannot find directory or file.
User avatar
Pendrokar
Posts: 658
Joined: 30 May 2007, 10:45

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

Post 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!
Post Reply

Return to “Lua Scripts”