Page 1 of 1

Using ScriptCB_IsFileExist() in an addme.lua

Posted: Wed Oct 08, 2008 12:37 pm
by Aman/Pinguin
I want the KotOR mode in my map to only show up if the conversionpack is installed, so I did this:
Hidden/Spoiler:
function AddNewGameModes(missionList, mapName, newFlags)
for i, mission in missionList do
if mission.mapluafile == mapName then
for flag, value in pairs(newFlags) do
mission[flag] = value
end
end
end
end


local sp_n = 0
local mp_n = 0
local Convofile = "side\\rvs.lvl"
sp_n = table.getn(sp_missionselect_listbox_contents)

sp_missionselect_listbox_contents[sp_n+1] = { isModLevel = 1, mapluafile = "PTC%s_%s", era_c = 1, mode_con_c = 1, mode_eli_c = 1, mode_hunt_c = 1, era_a = 1, mode_con_a = 1, era_k = 1, mode_con_k = 1, mode_eli_k = 1, mode_xl_c = 1,}
mp_n = table.getn(mp_missionselect_listbox_contents)
mp_missionselect_listbox_contents[mp_n+1] = sp_missionselect_listbox_contents[sp_n+1]


AddDownloadableContent("PTC","PTCa_con",4)
AddDownloadableContent("PTC","PTCc_con",4)

if( ScriptCB_IsFileExist(Convofile) == 1 ) then
AddDownloadableContent("PTC","PTCk_con",4)
end


AddDownloadableContent("PTC","PTCc_eli",4)

if( ScriptCB_IsFileExist(Convofile) == 1 ) then
AddDownloadableContent("PTC","PTCk_eli",4)
end


AddDownloadableContent("PTC","PTCc_hunt",4)
AddDownloadableContent("PTC","PTCc_xl",4)

newEntry = nil
n = nil

ReadDataFile("..\\..\\addon\\PTC\\data\\_LVL_PC\\core.lvl")
I tried it ingame and it gave me a "Cannot open Mission/PTCk_con" error after I started the (still shown) KotOR mode.

So I tried this:
Hidden/Spoiler:
function AddNewGameModes(missionList, mapName, newFlags)
for i, mission in missionList do
if mission.mapluafile == mapName then
for flag, value in pairs(newFlags) do
mission[flag] = value
end
end
end
end


local sp_n = 0
local mp_n = 0
local Convofile = "side\\rvs.lvl"
sp_n = table.getn(sp_missionselect_listbox_contents)

sp_missionselect_listbox_contents[sp_n+1] = { isModLevel = 1, mapluafile = "PTC%s_%s", era_c = 1, mode_con_c = 1, mode_eli_c = 1, mode_hunt_c = 1, era_a = 1, mode_con_a = 1, if( ScriptCB_IsFileExist(Convofile) == 1 ) then era_k = 1, mode_con_k = 1, mode_eli_k = 1, end mode_xl_c = 1,}
mp_n = table.getn(mp_missionselect_listbox_contents)
mp_missionselect_listbox_contents[mp_n+1] = sp_missionselect_listbox_contents[sp_n+1]


AddDownloadableContent("PTC","PTCa_con",4)
AddDownloadableContent("PTC","PTCc_con",4)

if( ScriptCB_IsFileExist(Convofile) == 1 ) then
AddDownloadableContent("PTC","PTCk_con",4)
end


AddDownloadableContent("PTC","PTCc_eli",4)

if( ScriptCB_IsFileExist(Convofile) == 1 ) then
AddDownloadableContent("PTC","PTCk_eli",4)
end


AddDownloadableContent("PTC","PTCc_hunt",4)
AddDownloadableContent("PTC","PTCc_xl",4)

newEntry = nil
n = nil

ReadDataFile("..\\..\\addon\\PTC\\data\\_LVL_PC\\core.lvl")

which gave me a munge error.


Now I have no idea how I could do that.
Any help would be great. Thanks.

Re: Using ScriptCB_IsFileExist() in an addme.lua

Posted: Wed Oct 08, 2008 1:22 pm
by Maveritchell
Kill the parentheses around ScriptCB_IsFileExist(Convofile) == 1. With the parentheses there your if statement doesn't make sense... I think. No doubt I'll be corrected if I'm wrong.

Re: Using ScriptCB_IsFileExist() in an addme.lua

Posted: Wed Oct 08, 2008 1:28 pm
by Aman/Pinguin
Still getting a munge error. :? Thanks for you answer though.

Image

Re: Using ScriptCB_IsFileExist() in an addme.lua

Posted: Wed Oct 08, 2008 1:38 pm
by Maveritchell
I'd just try "if ScriptCB_IsFileExist(Convofile)"

No "== 1"

Re: Using ScriptCB_IsFileExist() in an addme.lua

Posted: Wed Oct 08, 2008 1:42 pm
by Teancum
Yep, I'm not 100% on how lua does it, but in standard coding it'd be

if ScriptCB_IsFileExist(Convofile) then //If the file exists
AddDownloadableContent("PTC","PTCk_con",4) //Add this mode
end

Re: Using ScriptCB_IsFileExist() in an addme.lua

Posted: Wed Oct 08, 2008 1:44 pm
by Aman/Pinguin
Well, this is how pandemic used it:

Code: Select all

local movieFile = "movies\\tut" .. langShort .. gMovieTutorialPostFix .. ".mvs"
if( ScriptCB_IsFileExist(movieFile) == 0 ) then
movieFile = "movies\\tuteng" .. gMovieTutorialPostFix .. ".mvs"
end        
Note the "== 0"

I will try it anyway.

EDIT: Nope didn't work.

Re: Using ScriptCB_IsFileExist() in an addme.lua

Posted: Wed Oct 08, 2008 1:53 pm
by Teancum
You're also running a function inside an array. I don't know that LUA can do that.

Where's Zerted when you need him?

Re: Using ScriptCB_IsFileExist() in an addme.lua

Posted: Wed Oct 08, 2008 1:56 pm
by Aman/Pinguin
Teancum wrote:Where's Zerted when you need him?
You just read my mind.

Re: Using ScriptCB_IsFileExist() in an addme.lua

Posted: Wed Oct 08, 2008 2:14 pm
by Maveritchell
Teancum wrote:You're also running a function inside an array. I don't know that LUA can do that.
I don't know that it can, and that may be part of the problem. However, it was still an issue in the first trial he did (where he didn't even try the CB inside the array), so I don't know if it's THE problem.

Re: Using ScriptCB_IsFileExist() in an addme.lua

Posted: Wed Oct 08, 2008 4:35 pm
by [RDH]Zerted
Teancum wrote:You're also running a function inside an array. I don't know that LUA can do that.

Where's Zerted when you need him?
I'm at work taking a quick break between tasks to refresh my mind :angel: :emp: . I can solve this thread when I get back home. The quick answer, is that yes in Lua you can have a function statement in an array (thats how AddUnitClass() still works when people wrongly put it in the SetupTeams section), but I'm not sure about an if statement. Of course, the way it is added is wrong (1. you forgot the ending comma).

The easiest and user unfriendly way to do it is to use AddNewGameModes(). The better ways are to use a few if statements or a few functions (which would you prefer?). The better way is more complex code wise, but won't have to loop through every map already added to the list like AddNewGameModes() does.

You can have three different answers. Pick one before I get home...

Re: Using ScriptCB_IsFileExist() in an addme.lua

Posted: Thu Oct 09, 2008 9:53 am
by Aman/Pinguin
[RDH]Zerted wrote:The easiest and user unfriendly way to do it is to use AddNewGameModes(). The better ways are to use a few if statements or a few functions (which would you prefer?). The better way is more complex code wise, but won't have to loop through every map already added to the list like AddNewGameModes() does.

You can have three different answers. Pick one before I get home...
What bad thing would happen if I would pick the easy way and use AddNewGameModes()? Does that loop through every added gamemode somehow affect the game or whatever heavy?

Also, how hard would the "if-statements-way" be? Since I'm still pretty bad at scripting (I think). :?

Re: Using ScriptCB_IsFileExist() in an addme.lua

Posted: Thu Oct 09, 2008 12:56 pm
by [RDH]Zerted
I shouldn't have mentioned the function way, that just makes it more complicated then it needs to be and the code I posted in this post is better then the other ways I had first thought of anyway...

Nothing really bad happens, but it will slow the game down slightly. Every time the game scans the addme scripts (on startup and when moving from map to shell), it will loop through all the known missions when it processes an addme using AddNewGameModes. It is not much of an issue for one map, but if all the maps did this, you would easily notice the slower loading times.

How To Do It
Hidden/Spoiler:
* Add your normal missions normally
* Put your new code after the normal AddDownloadableContent() lines.
* First check to see if the needed side file exists[code]if ScriptCB_IsFileExist(Convofile) == 0 then
print("Cannot find "..Convofile or "[Oops, what file?]"..". Skipping PTC's extra missions")
else
print("Found "..Convofile..". Adding PTC's extra missions")
end[/code]* Next you want the single player (SP) map selection screen to show the check boxes for the extra missions[code]sp_missionselect_listbox_contents[sp_n+1].era_k = 1 --adds the k era checkbox
sp_missionselect_listbox_contents[sp_n+1].mode_con_k = 1 --adds the con mode checkbox
sp_missionselect_listbox_contents[sp_n+1].mode_eli_k = 1 --adds the eli mode checkbox[/code]
* Next you want the multi player (MP) map selection screen to also show the check boxes for the extra missions[code]mp_missionselect_listbox_contents[mp_n+1] = sp_missionselect_listbox_contents[sp_n+1][/code]* Finally you want AddDownloadableContent to do whatever it is it does[code]AddDownloadableContent("PTC","PTCk_con",4)
AddDownloadableContent("PTC","PTCk_eli",4)[/code]
* You need to place each code segment in the correct location. Some segments may be inside other segments.
Random Notes:
* .. combines two strings
* Just like in math, 1+1 is the same as (1+1) or even ((((1)+(1))))
* Some languages require if( xxx ) then xxx. In Lua if xxx then xxx is fine too.
* Lua doesn't need semicolons at the end of its lines, but it still works if you add them
* I do not know what ScriptCB_IsFileExist() returns when the file exists (it wouldn't be hard to find out... I'm too lazy/busy). I only know it returns a zero when the file doesn't exist.
* The v1.3 patch uses ScriptCB_IsFileExist() in a bunch of different places. For the settings files, custom user scripts, preview videos, etc...

Re: Using ScriptCB_IsFileExist() in an addme.lua

Posted: Thu Oct 09, 2008 1:50 pm
by Aman/Pinguin
Works perfectly! Thanks Zerted! :) Also thanks Mav and Teancum for trying to help. :wink:

Just for future reference, here the addme.lua how it looks like now:
Hidden/Spoiler:
[code]function AddNewGameModes(missionList, mapName, newFlags)
for i, mission in missionList do
if mission.mapluafile == mapName then
for flag, value in pairs(newFlags) do
mission[flag] = value
end
end
end
end


local sp_n = 0
local mp_n = 0
local Convofile = "side\\rvs.lvl"
sp_n = table.getn(sp_missionselect_listbox_contents)

sp_missionselect_listbox_contents[sp_n+1] = { isModLevel = 1, mapluafile = "PTC%s_%s", era_c = 1, mode_con_c = 1, mode_eli_c = 1, mode_hunt_c = 1, era_a = 1, mode_con_a = 1, mode_xl_c = 1,}
mp_n = table.getn(mp_missionselect_listbox_contents)
mp_missionselect_listbox_contents[mp_n+1] = sp_missionselect_listbox_contents[sp_n+1]

AddDownloadableContent("PTC","PTCa_con",4)
AddDownloadableContent("PTC","PTCc_con",4)
AddDownloadableContent("PTC","PTCc_eli",4)
AddDownloadableContent("PTC","PTCc_hunt",4)
AddDownloadableContent("PTC","PTCc_xl",4)

if ScriptCB_IsFileExist(Convofile) == 0 then
print("Cannot find "..Convofile or "[Oops, what file?]"..". Skipping PTC's extra missions")
else
print("Found "..Convofile..". Adding PTC's extra missions")

sp_missionselect_listbox_contents[sp_n+1].era_k = 1 --adds the k era checkbox
sp_missionselect_listbox_contents[sp_n+1].mode_con_k = 1 --adds the con mode checkbox
sp_missionselect_listbox_contents[sp_n+1].mode_eli_k = 1 --adds the eli mode checkbox
mp_missionselect_listbox_contents[mp_n+1] = sp_missionselect_listbox_contents[sp_n+1]

AddDownloadableContent("PTC","PTCk_con",4)
AddDownloadableContent("PTC","PTCk_eli",4)
end

newEntry = nil
n = nil

ReadDataFile("..\\..\\addon\\PTC\\data\\_LVL_PC\\core.lvl")[/code]
Maybe this should be added to the FAQ?