Page 1 of 1
I/O Library
Posted: Thu Oct 03, 2013 11:34 pm
by Marth8880
Is the Lua I/O library supported in Zero Engine? I need to use io.read to read the contents of a text document and output junk and junk, like so:
Code: Select all
io.input(TESTwhat.txt)
local testioinput = io.read("*all")
print("ZeroEngineSuckageLevel: " .. testioinput)
The text file called "TESTwhat.txt" is in the _LVL_PC folder like I would assume it should be, and "YOU LOST THE GAME" is the contents of the file; the file is encoded as ANSI. (Does it need to be something else?

)
The game didn't crash, but the debugger did output this:
Code: Select all
Message Severity: 3
C:\Battlefront2\main\Battlefront2\Source\LuaHelper.cpp(312)
CallProc failed: (none):0: attempt to index global `TESTwhat' (a nil value)
stack traceback:
(none): in function `ScriptPostLoad'
I will use ScriptCB_IsFileExist() if I absolutely must, but there are a lot of configuration parameters I need and I'd hate to have to deal with a ton of text files and whatnot. :p
Source:
http://www.lua.org/pil/21.1.html
Re: I/O Library
Posted: Fri Oct 04, 2013 12:33 am
by tirpider
I'm using the lua4 fileopen, appendto, readfrom, and such from SWBF1 quite successfully.
You should be able to get to and from a file using those lua5 io. equivalents. (I don't know what they are, but there are a thousand more help sites for lua5 than for lua4.)
I did have to play around with a FileExists like statment to figure out paths.
SWBF1 likes it's paths to be relative to the gamedata folder, where the exe is.
For example: "Data\\_LVL_PC\\MODS\\Debug_Log.txt"
I'd be surprised if SWBF2 were any different.
Alternatively, just creating a file from lua and seeing where it lands would illuminate it as well.. but yeah.
That error mentions an "attempt to index global". It seems to think you are trying to index into a table. io.read("*all") may be returning a table? (I'm guessing. 5 is significantly different than 4, especially in the i/o department.)
-edit
no.. i just read that *all means the whole file. So I'm not sure where it's getting the attempting to index error.
How about quotes?:
io.input("TESTwhat.txt")
Re: I/O Library
Posted: Fri Oct 04, 2013 1:04 am
by THEWULFMAN
We tried with and without quotes, same result.
Re: I/O Library
Posted: Fri Oct 04, 2013 1:26 am
by tirpider
assert should give a descriptive error message:
Code: Select all
local testioinput = assert(io.read("*all"))
But I don't know if read will return anything other than nil on error.
Based on the example close to the bottom of
http://www.lua.org/pil/21.2.html
Code: Select all
local testfile = assert(io.open("TESTwhat.txt", "r"))
local testioinput = testfile:read("*all")
testfile:close()
print("ZeroEngineSuckageLevel: " .. testioinput)
Uses open to get a handle, then reads using that, instead of grabbing stdin with input.
-edit
didn't include a way to see if testfile contained an error... another print?
Re: I/O Library
Posted: Fri Oct 04, 2013 1:45 am
by Marth8880
Hmm, this is what I get:
Code: Select all
Message Severity: 3
C:\Battlefront2\main\Battlefront2\Source\LuaHelper.cpp(312)
CallProc failed: (none):0: attempt to call field `open' (a nil value)
stack traceback:
(none): in function `SoundFX'
(none): in function `ScriptInit'
Re: I/O Library
Posted: Fri Oct 04, 2013 3:16 am
by tirpider
I am unfamilier with all of lua's syntax, so I am not certain that it's ok in those examples I gave.
But since it's complaining about nil, I'm not convinced that it just isn't finding the file.
Here are some examples of file opening on the unofficial FAQ:
http://www.luafaq.org/#T2.3
using the simple and complete model for handling files.
My instinct tells me the code from your OP is correct, but needs more path.
As much of a hassle mungeing and testing is, perhaps try a barrage of paths till one works.
Or try putting a copy of the file in every folder to see if the code you have finds one of them.
If the last one works without error, then rewrite the code so that it writes something to the file, so you can go find the changed one and know what it considers root for pathing.
Typically, lua looks in the directory the script was run from. In the case of our hosted scripting environment, that would be the place where the exe is located.
(Lua4 has none of all the io. stuff. Blessing and a curse, I suppose.)
Re: I/O Library
Posted: Fri Oct 04, 2013 7:14 am
by ANDEWEGET
I'm not sure if it works with built-in stuff like io but maybe try dump(io) to see what io contains?
The last error message seems to say that io.open doesn't exist.
Re: I/O Library
Posted: Fri Oct 04, 2013 9:48 am
by Maveritchell
I've tried a few of the io functions before without success, specifically "io.write," which should almost be path-agnostic (because it should just gin up a file).
Re: I/O Library
Posted: Fri Oct 04, 2013 11:21 am
by THEWULFMAN
ANDEWEGET wrote:I'm not sure if it works with built-in stuff like io but maybe try dump(io) to see what io contains?
The last error message seems to say that io.open doesn't exist.
This is what we got.
Code: Select all
Message Severity: 3
C:\Battlefront2\main\Battlefront2\Source\LuaHelper.cpp(312)
CallProc failed: (none):0: attempt to call global `dump' (a nil value)
stack traceback:
(none): in function `ScriptPostLoad'
This is what my ScriptPostLoad looks like.
Re: I/O Library
Posted: Fri Oct 04, 2013 11:58 am
by ANDEWEGET
I thought dump() comes with Lua, but it actually doesn't (it's really useful, so I guess it seemed logical to me). It's just a global command for CryEngine Lua scripting which is the only environment I've really used Lua in.
You could write the recursive dump yourself, though:
Link.
Re: I/O Library
Posted: Fri Oct 04, 2013 12:57 pm
by THEWULFMAN
Ugh. Alright, well, at least this wasn't our only option. Would have made our lives a heck of a lot easier though. Thanks everyone for your help, we really appreciate it.
Re: I/O Library
Posted: Tue Oct 08, 2013 5:21 am
by [RDH]Zerted
The io module isn't included. You can tell by dumping the global state (table _G) to the debug output and looking at all the functions. Download the output from my website (Lua globals at...) or use the Fake Console command "Print Nested Globals" (will take a bit of time to finish running). To make it easier to print tables, v1.3 added uf_print() in utility_functions2.lua. Documentation:
The table output can be difficult to read, so if you want a generated listing of all available functions, check out my SWBF2 v1.3 Full API documentation. Ignore all the xml and look at the two lists at the top of the file.
If you want to learn how the games save non-profile things to files look at ifs_freeform_menu.lua, ifs_freeform_main.lua, ifs_freeform_load.lua, ifs_saveop.lua, ifs_meta_main_input.lua, and popup_loadsave2.lua or use my very hackish SaveAndLoadUtils script. There's a GT topic on that somewhere... Download it off my website and read the usage guide in data_SAV/Common/scripts/SaveAndLoadUtils.lua. If you're attempting to save when there's an existing metagame state you'll have to adjust my script to use the campaign state (ScriptCB_[Save/Clear/Load/]CampaignState vs ScriptCB_SaveMetagameState). Don't save too often. The game takes about 6 seconds to save, it clears any stored metagame states, and it'll pop through ifs_saveop screens (potentially bad if the user is in the middle of another pop-up). I forget what I learned about ScriptCB_SaveMissionSetup...
Website:
zerted.getmyip.com ->
SWBF2 Section -> look around
Latest SaveAndLoadUtils version: v1 r13 - March 29th, 2009 (r12 is fine too)