View topic - file I/O in widgets and gadgets



All times are UTC + 1 hour


Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: 30 Jan 2012, 10:47 
User avatar

Joined: 07 Sep 2010, 17:47
If I put the code
Code:
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:
[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?


Top
 Offline Profile  
 
PostPosted: 30 Jan 2012, 15:17 
Moderator
User avatar

Joined: 29 Apr 2005, 00:14
Location: #moddev - join it!
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.


Top
 Offline Profile  
 
PostPosted: 30 Jan 2012, 17:23 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: ModalitÃ
FLOZi wrote:
Only widgets have access to output.
-fixt' we can read in files

why no output?


Top
 Offline Profile  
 
PostPosted: 30 Jan 2012, 18:11 
Moderator
User avatar

Joined: 29 Apr 2005, 00:14
Location: #moddev - join it!
smoth wrote:
FLOZi wrote:
Only widgets have access to output.
-fixt' we can read in files

why no output?


Quote:
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:


Top
 Offline Profile  
 
PostPosted: 30 Jan 2012, 19:23 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: ModalitÃ
ah, I was thinking you mean io as in the topic.. I was like wtf.


Top
 Offline Profile  
 
PostPosted: 30 Jan 2012, 19:34 
Moderator
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
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.

Quote:
-fixt' we can read in files
why no output?
read in as with VFS.Include?
I think that is something else.


Top
 Offline Profile  
 
PostPosted: 30 Jan 2012, 19:44 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: ModalitÃ
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...


Top
 Offline Profile  
 
PostPosted: 30 Jan 2012, 20:21 
User avatar

Joined: 07 Sep 2010, 17:47
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.


Top
 Offline Profile  
 
PostPosted: 30 Jan 2012, 20:28 
Moderator
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
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.

Quote:
the data is a matrix calculated from a map (based on height & type)
If it is for 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.


Top
 Offline Profile  
 
PostPosted: 30 Jan 2012, 21:08 
User avatar

Joined: 07 Sep 2010, 17:47
Quote:
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).


Top
 Offline Profile  
 
PostPosted: 30 Jan 2012, 21:18 
Moderator
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
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/source/browse/games/CT/mapconfigs/TitanDuel_res.lua
& the gadget that reads it:
http://code.google.com/p/conflictterra/source/browse/games/CT/LuaRules/Gadgets/tp_resourcespawner.lua


Top
 Offline Profile  
 
PostPosted: 30 Jan 2012, 21:40 
User avatar

Joined: 07 Sep 2010, 17:47
Ah, thanks much.

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


Top
 Offline Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group

Site layout created by Roflcopter et al.