Scripting Question

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
User avatar
Sky_216
Droid Pilot Assassin
Droid Pilot Assassin
Posts: 2086
Joined: Mon Feb 13, 2006 3:28 am
Projects :: No Mod project currently.
Games I'm Playing :: I have not listed any games yet
xbox live or psn: No gamertag set

Scripting Question

Post by Sky_216 »

Just wondering.......




Is it possible to set up a single .lua scipt that can load multiple missions depending on saved variables (the things Zerted made code for)? So say for example it checks for certain variables before loading units/map/objectives, if its one thing it loads one script, if its another it loads another etc?
ScriptInit and ScriptPostLoad are both called at the start of a mission automatically, correct? So could they be used to check for certain variables, and then the actual mission script to be loaded have functions with different names? Or do the things put in ScriptPostLoad and ScriptInit have to be in those functions?
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: Scripting Question

Post by [RDH]Zerted »

The game only calls those two functions automatically. From them, it doesn't matter too much how/where you load your map, only that the correct things get loaded. A few of the functions don't work in ScriptPostLoad, so things related to those have to be done in ScriptInit. The actual function names don't matter, but things have to be loaded when the game is in the states it expects them to be loaded.

My save script requires ifs_saveop, so at least a subset of ingame.lvl needs to be loaded before you can use it.

You will have to do a little trickery. The load script uses a callback function to say when its done loading the data. You would want that to get called while the game is still in ScriptInit, but you can't guarantee ScriptInit won't finish first. One idea would be to loop in ScriptInit until the callback function gets called. I don't know how SWBF2 does threading in relation to its Lua scripts... Another way would be to try and remove the timer from the load functions and hope the game loads the data fast enough.

In theory, it should be possible. But there is a little more behind the scenes than it first seems (though it might not end up being a problem so...)

This sounds like it might be a useful script. If you get it working, I might have time in June to convert it into a generic script anyone could easily use.
User avatar
Sky_216
Droid Pilot Assassin
Droid Pilot Assassin
Posts: 2086
Joined: Mon Feb 13, 2006 3:28 am
Projects :: No Mod project currently.
Games I'm Playing :: I have not listed any games yet
xbox live or psn: No gamertag set

Re: Scripting Question

Post by Sky_216 »

Results so far: haven't tested saving/loading yet, but its possible to at random load two different modes (in this case I just switched at random between stock CW and stock GCW contest). Had only the randomising function in scriptinit, and a call for another function that sets which era to load. The basic ScriptPostLoad was empty, all the stuff for it being loaded in other functions that differed depending on what era was loaded - Game still seems to work fine.
Hidden/Spoiler:
function ScriptInit()

Era = math.random(1,2)
Start()

end

function Start()
if Era == 1 then
REP = 1;
CIS = 2;
ATT = REP;
DEF = CIS;
CWInit()
CWLoad()
elseif Era == 2 then
ALL = 2
IMP = 1
ATT = 1
DEF = 2
GCWInit()
GCWLoad()
end
end
Haven't tested loading different modes yet, hopefully that'll work too. :)


Zerted, couple of questions:

1 - For saving data, what exactly is a "table"? The section contained by "local data" in your example provided with saveloadutils?

2- I plan on using your save function to store a set of values based on variables set during each mission - eventually planning to use: 1 variable for mission/map to load, a set of variables for which weapons/player stats to use and a set for determining which future missions are available. How can these be saved/loaded?

3 - Are the calls for scripts at the top of each script loaded before everything else?

4 - Is all thats needed to use save/load from ingame lvl the saveop file? Could I put this inside a lvl file all by itself, load it in ScriptInit and that would allow the loading process to work?



Anyway thanks for help so far, will try and get around the ScriptInit problem......
User avatar
lucasfart
Sith
Sith
Posts: 1440
Joined: Tue Feb 24, 2009 5:32 am
Projects :: No Mod project currently.
Games I'm Playing :: I have not listed any games yet
xbox live or psn: No gamertag set
Location: Australia

Re: Scripting Question

Post by lucasfart »

So let me get this straight.

If we want to do this we need to have a copy of both era's load and init functions in the one lua, with the heading of function CW/GCWInit or function CW/GCWLoad for scriptinit and scriptpostload respectively, assuming we use the same function names as you.

So we would need to remove the side numbers/names at the start?
User avatar
Sky_216
Droid Pilot Assassin
Droid Pilot Assassin
Posts: 2086
Joined: Mon Feb 13, 2006 3:28 am
Projects :: No Mod project currently.
Games I'm Playing :: I have not listed any games yet
xbox live or psn: No gamertag set

Re: Scripting Question

Post by Sky_216 »

lucasfart wrote:So let me get this straight.

If we want to do this we need to have a copy of both era's load and init functions in the one lua, with the heading of function CW/GCWInit or function CW/GCWLoad for scriptinit and scriptpostload respectively, assuming we use the same function names as you.

So we would need to remove the side numbers/names at the start?
Yep.

Naming conventions for the "child" functions are irrelevant (I think). In my case this was just a test and I eventually intend it for making an rpg/campaign based on a single IA mission spanning multiple maps; not for random eras, but yes you could do that.

In the test example also having CW/GCWLoad was also pointless cos they were the same. Was just seeing if it'd work.

Going to test to see if it works for different modes too (Con and CTF). Hopefully will.
User avatar
lucasfart
Sith
Sith
Posts: 1440
Joined: Tue Feb 24, 2009 5:32 am
Projects :: No Mod project currently.
Games I'm Playing :: I have not listed any games yet
xbox live or psn: No gamertag set
Location: Australia

Re: Scripting Question

Post by lucasfart »

I don't see why it shouldn't, as long as you make sure to load the right scripts for each. Apart from that, it should be almost identical to setting up era's, shouldn't it?

Also, slightly offtopic, but am i right in guessing that this is the last big feature you had planned for sp dark space play?
User avatar
RED51
Jedi
Jedi
Posts: 1024
Joined: Sun Sep 07, 2008 6:25 pm
Games I'm Playing :: SWBFII

Re: Scripting Question

Post by RED51 »

So let me get this straight. What you are trying to do is make a mode the loads a random mode every time, like CTF and conquest, all in one script?
User avatar
Maveritchell
Jedi Admin
Jedi Admin
Posts: 7366
Joined: Mon Aug 21, 2006 11:03 pm

Re: Scripting Question

Post by Maveritchell »

RED51 wrote:So let me get this straight. What you are trying to do is make a mode the loads a random mode every time, like CTF and conquest, all in one script?
There'd be no need for any save functions with that; what you're talking about is easy (actually something I've considered doing) and is no different (except in scale) from loading a random sky.
User avatar
Sky_216
Droid Pilot Assassin
Droid Pilot Assassin
Posts: 2086
Joined: Mon Feb 13, 2006 3:28 am
Projects :: No Mod project currently.
Games I'm Playing :: I have not listed any games yet
xbox live or psn: No gamertag set

Re: Scripting Question

Post by Sky_216 »

RED51 wrote:So let me get this straight. What you are trying to do is make a mode the loads a random mode every time, like CTF and conquest, all in one script?
No. The randomness was just for testing purposes. What I'm trying to do is set up an RPG campaign (multi mission) based on only one script. Hence need save and load to determine mission/map to load, which weapons/player stats to use and which future missions are available.
Maveritchell wrote:
RED51 wrote:So let me get this straight. What you are trying to do is make a mode the loads a random mode every time, like CTF and conquest, all in one script?
There'd be no need for any save functions with that; what you're talking about is easy (actually something I've considered doing) and is no different (except in scale) from loading a random sky.
What he said. Just for the record its also quite possible to load random maps too. And foliage...and objectives......pretty much whatever. Just depends on how much scripting and extra content you're willing to make.
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: Scripting Question

Post by [RDH]Zerted »

A table is a Lua data type. You can look it up in the Lua tutorials. Consider it an array/dictionary/key-value pairs/map. It contains data. Think of it as a list of variables.

The data must be in a table for it to be saved/loaded. That is the only requirement/constraint. You can structure it any way you want. I can't really tell you a best way to do it.

When a script file is read it is executed; meaning the code in it is run. Think of a script file as one giant function that you didn't have to name. So yes: if there is code at the top of the file, it is run before the things under it. When you read in another script, that entire script is executed as if it were a single function.

Anything ifs_saveop depends on would also have to be loaded. I don't know what all that is offhand, but I'm sure there's something. Scripting wise, loading ingame.lvl runs through game_interface.lua. v1.3 added code to that file to load the custom user scripts, but you don't need that to see what the game is doing.

Don't worry about loading ingame.lvl yet. Do that normally. Just try to get a different map/mode loaded working first.
User avatar
Sky_216
Droid Pilot Assassin
Droid Pilot Assassin
Posts: 2086
Joined: Mon Feb 13, 2006 3:28 am
Projects :: No Mod project currently.
Games I'm Playing :: I have not listed any games yet
xbox live or psn: No gamertag set

Re: Scripting Question

Post by Sky_216 »

Hey Zerted, would this section below (inside ScriptInit) work as a loop to stop ScriptInit finishing before everything's loaded?

I looked through some basic lua tuts, do I need the "a" bits to keep the loop going?

The idea is loadingcomplete would be set to 1 as a global for the last part of the loading function.
Hidden/Spoiler:
loadingcomplete = 0

a = 1
while loadingcomplete == 0 do
a = a + 1
if loadingcomplete == 1 then break end
end

Oh and different maps/modes works fine, at least when using random.
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: Scripting Question

Post by [RDH]Zerted »

You don't need the 'a'. A loop doesn't have to do anything to exist. However, I should point out that using a 'busy loop' to take up time is one of the worst things one can do in a program. But I don't know what trying to sleep in Lua will do to SWBF2.
User avatar
Sky_216
Droid Pilot Assassin
Droid Pilot Assassin
Posts: 2086
Joined: Mon Feb 13, 2006 3:28 am
Projects :: No Mod project currently.
Games I'm Playing :: I have not listed any games yet
xbox live or psn: No gamertag set

Re: Scripting Question

Post by Sky_216 »

[RDH]Zerted wrote:You don't need the 'a'. A loop doesn't have to do anything to exist. However, I should point out that using a 'busy loop' to take up time is one of the worst things one can do in a program. But I don't know what trying to sleep in Lua will do to SWBF2.
Ah...in that case would it be better just to run a timer in ScriptInit to ensure the load has time to work?


Any other suggestions would be appreciated too.
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: Scripting Question

Post by [RDH]Zerted »

You can't use a timer because ScriptInit will return right after you start the timer. The goal is to prevent ScriptInit from finishing until you are able to load the saved values and call the correct custom mission loading function.

A quick search brings up this thread, which isn't promising. Try it with the loop first to see if it works.
Post Reply