file I/O in widgets and gadgets

file I/O in widgets and gadgets

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

Moderator: Moderators

Post Reply
User avatar
zoggop
Posts: 289
Joined: 07 Sep 2010, 18:47

file I/O in widgets and gadgets

Post by zoggop »

If I put the code

Code: Select all

io.output("thing.txt")
io.write("some text")
into a widget, it creates a text file, thing.txt in the root spring directory with the text "some text"

If I put the same code into a gadget, I get this in the infolog:

Code: Select all

[f=0000000] Loading LuaRules
[f=0000000] Failed to load: build_block.lua  ([string "LuaRules/Gadgets/build_block.lua"]:32: attempt to index global 'io' (a nil value))
Help?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: file I/O in widgets and gadgets

Post by FLOZi »

Only widgets have access to io.

If you want to save gadget data, you'll have to pass it from synced to unsynced and have the widget do the io.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: file I/O in widgets and gadgets

Post by smoth »

FLOZi wrote:Only widgets have access to output.
-fixt' we can read in files

why no output?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: file I/O in widgets and gadgets

Post by FLOZi »

smoth wrote:
FLOZi wrote:Only widgets have access to output.
-fixt' we can read in files

why no output?
attempt to index global 'io' (a nil value))
I meant 'only widgets have access to the io table/library.' if we want to be facetious. :wink:
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: file I/O in widgets and gadgets

Post by smoth »

ah, I was thinking you mean io as in the topic.. I was like wtf.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: file I/O in widgets and gadgets

Post by knorke »

gadgets are synced, but file io is not.
eg on one computer writing to thing.txt might fail due to no disk space or whatever but maybe for another player it works -> desync.
I half expected it to work in unsynced gadget but does not work either.
-fixt' we can read in files
why no output?
read in as with VFS.Include?
I think that is something else.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: file I/O in widgets and gadgets

Post by smoth »

knorke wrote:gadgets are synced, but file io is not.
eg on one computer writing to thing.txt might fail due to no disk space or whatever but maybe for another player it works -> desync.
I half expected it to work in unsynced gadget but does not work either
Same, I figured I should be able to do it in gadget unsynced...
User avatar
zoggop
Posts: 289
Joined: 07 Sep 2010, 18:47

Re: file I/O in widgets and gadgets

Post by zoggop »

Oh, yeah, that actually makes sense. Now I just have to figure out how to write/read the data using VFS. It's questionable whether it's worth it--the data is a matrix calculated from a map (based on height & type), which takes only a few seconds to calculate on a single core 1.7ghz processor. It just seems silly to calculate it every time the map loads, because it's going to be the same every time.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: file I/O in widgets and gadgets

Post by knorke »

Lua VFS is only for reading from archives.
http://springrts.com/wiki/Lua_VFS
You can not use it to write a bla.txt and later read it.
the data is a matrix calculated from a map (based on height & type)
If it is for http://springrts.com/phpbb/viewtopic.php?f=13&t=27550 you could just calculate the matrix once on your computer and then include the result in the map.sdz?
Also saves you from having to make some anti-cheat system.
User avatar
zoggop
Posts: 289
Joined: 07 Sep 2010, 18:47

Re: file I/O in widgets and gadgets

Post by zoggop »

you could just calculate the matrix once on your computer and then include the result in the map.sdz?
Oops, yeah that's what I meant by "write/read the data using VFS"--just the reading part. But I need to somehow format the data in a way that's readable using VFS (I get the impression text file with numbers in it won't work).

The good news is it works--AIs play on the map without cheating (i.e. building on the sand).
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: file I/O in widgets and gadgets

Post by knorke »

Basically you just do
gadget:
stuff= VFS.Include("file_to_read.lua")
Spring.Echo (stuff)


file_to_read.lua:
return 100

ofc you can also return multiple things or tables instead of just a number.

example:
http://code.google.com/p/conflictterra/ ... el_res.lua
& the gadget that reads it:
http://code.google.com/p/conflictterra/ ... pawner.lua
User avatar
zoggop
Posts: 289
Joined: 07 Sep 2010, 18:47

Re: file I/O in widgets and gadgets

Post by zoggop »

Ah, thanks much.

(PS the lolfactor of having a variable named lolfactor is pretty high)
Post Reply

Return to “Lua Scripts”