Page 1 of 1

file I/O in widgets and gadgets

Posted: 30 Jan 2012, 10:47
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?

Re: file I/O in widgets and gadgets

Posted: 30 Jan 2012, 15:17
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.

Re: file I/O in widgets and gadgets

Posted: 30 Jan 2012, 17:23
by smoth
FLOZi wrote:Only widgets have access to output.
-fixt' we can read in files

why no output?

Re: file I/O in widgets and gadgets

Posted: 30 Jan 2012, 18:11
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:

Re: file I/O in widgets and gadgets

Posted: 30 Jan 2012, 19:23
by smoth
ah, I was thinking you mean io as in the topic.. I was like wtf.

Re: file I/O in widgets and gadgets

Posted: 30 Jan 2012, 19:34
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.

Re: file I/O in widgets and gadgets

Posted: 30 Jan 2012, 19:44
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...

Re: file I/O in widgets and gadgets

Posted: 30 Jan 2012, 20:21
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.

Re: file I/O in widgets and gadgets

Posted: 30 Jan 2012, 20:28
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.

Re: file I/O in widgets and gadgets

Posted: 30 Jan 2012, 21:08
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).

Re: file I/O in widgets and gadgets

Posted: 30 Jan 2012, 21:18
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

Re: file I/O in widgets and gadgets

Posted: 30 Jan 2012, 21:40
by zoggop
Ah, thanks much.

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