I/O Library

In this forum you will find and post information regarding the modding of Star Wars Battlefront 2. DO NOT POST MOD IDEAS/REQUESTS.

Moderator: Moderators

Post Reply
Marth8880
Resistance Leader
Posts: 5042
Joined: Tue Feb 09, 2010 8:43 pm
Projects :: DI2 + Psychosis
Games I'm Playing :: Silent Hill 2
xbox live or psn: Marth8880
Location: Edinburgh, UK
Contact:

I/O Library

Post 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? :o )

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
User avatar
tirpider
Lance Corporal
Posts: 97
Joined: Fri Nov 25, 2011 3:58 pm

Re: I/O Library

Post 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")
THEWULFMAN
Space Ranger
Posts: 5557
Joined: Tue Aug 17, 2010 3:30 pm
Projects :: Evolved 2
Location: Columbus, Ohio
Contact:

Re: I/O Library

Post by THEWULFMAN »

We tried with and without quotes, same result.
User avatar
tirpider
Lance Corporal
Posts: 97
Joined: Fri Nov 25, 2011 3:58 pm

Re: I/O Library

Post 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?
Marth8880
Resistance Leader
Posts: 5042
Joined: Tue Feb 09, 2010 8:43 pm
Projects :: DI2 + Psychosis
Games I'm Playing :: Silent Hill 2
xbox live or psn: Marth8880
Location: Edinburgh, UK
Contact:

Re: I/O Library

Post by Marth8880 »

Hmm, this is what I get: :o

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'
User avatar
tirpider
Lance Corporal
Posts: 97
Joined: Fri Nov 25, 2011 3:58 pm

Re: I/O Library

Post 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.)
User avatar
ANDEWEGET
Ancient Force
Ancient Force
Posts: 1266
Joined: Tue Apr 01, 2008 8:42 am
Location: Germany
Contact:

Re: I/O Library

Post 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.
User avatar
Maveritchell
Jedi Admin
Jedi Admin
Posts: 7366
Joined: Mon Aug 21, 2006 11:03 pm

Re: I/O Library

Post 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).
THEWULFMAN
Space Ranger
Posts: 5557
Joined: Tue Aug 17, 2010 3:30 pm
Projects :: Evolved 2
Location: Columbus, Ohio
Contact:

Re: I/O Library

Post 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.
Hidden/Spoiler:
[code]function ScriptPostLoad()


--This defines the CPs. These need to happen first
cp1 = CommandPost:New{name = "cp1"}
cp2 = CommandPost:New{name = "cp2"}
cp3 = CommandPost:New{name = "cp3"}
cp4 = CommandPost:New{name = "cp4"}



--This sets up the actual objective. This needs to happen after cp's are defined
conquest = ObjectiveConquest:New{teamATT = ATT, teamDEF = DEF,
textATT = "game.modes.con",
textDEF = "game.modes.con2",
multiplayerRules = true}

--This adds the CPs to the objective. This needs to happen after the objective is set up
conquest:AddCommandPost(cp1)
conquest:AddCommandPost(cp2)
conquest:AddCommandPost(cp3)
conquest:AddCommandPost(cp4)

conquest:Start()

EnableSPHeroRules()

dump(io)
io.input("TESTwhat.txt")
-- local testioinput = io.read("*all")
print("ZeroEngineSuckageLevel: " .. testioinput)
local testioinput = assert(io.read("*all"))

end[/code]
User avatar
ANDEWEGET
Ancient Force
Ancient Force
Posts: 1266
Joined: Tue Apr 01, 2008 8:42 am
Location: Germany
Contact:

Re: I/O Library

Post 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.
THEWULFMAN
Space Ranger
Posts: 5557
Joined: Tue Aug 17, 2010 3:30 pm
Projects :: Evolved 2
Location: Columbus, Ohio
Contact:

Re: I/O Library

Post 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.
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: I/O Library

Post 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:
Hidden/Spoiler:
--
---- Description ----
-- Attempts to display the contents of the given table
-- WARNING: tables with nested tables pointing back at themselves or a parent table will cause this function to enter a recursive, infinite loop
--
---- Parameters ----
-- data - the table to display
-- nested - true if should attempt to display any nested tables (tables inside of tables etc...)
-- depth - the current nested level
--
---- Returns ----
-- (nothing)
--
function uf_print( data, nested, depth )

Example:
uf_print( _G, false, 0) --prints the first level of the global table (_G)
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)
Post Reply