Page 1 of 1

Add a unit class globally?

Posted: Wed May 16, 2012 10:14 am
by Teancum
I have a bit of a situation with the Xbox mod. While all maps in Instant Action have a new class (the Commando) I had to keep all campaign scripts stock (no Commando). The reason being is that it breaks the campaign -- objectives are uncompletable, etc. Now I've found that the same issue exists in Galactic Conquest (many maps have issues) and so I have to revert to the original conquest luas for the default maps, or so I thought.

Then it dawned on me. Why not just create a function to add the unit class on startup, then put it in a lua file that always runs? So that's what I want to do: put it in setup_teams.lua. Then the new class will always load, regardless of the map and mode being loaded. The question is whether I can call the ScriptInit() and ScriptPostLoad() functions from setup_teams.lua to read the data files then add the unit class. I would assume so, but I'm not sure and won't have access to my modding PC soon. Anyone know?

Re: Add a unit class globally?

Posted: Thu May 31, 2012 11:53 pm
by [RDH]Zerted
Do you know why it might be breaking all the missions? I wouldn't have expected that. It may or may not be possible to get around the issue. My guess with only the info from your post is that adding a unit is changing the unit id of some class that is directly referenced in the scripts.

I'm a little confused with what you're trying to put into setup_teams.lua. The game already calls ScriptInit and ScriptPostLoad, so trying to call them again would cause issues. If you want to detect when the game calls ScriptInit (but really you want to know when AddUnitClass is called correct?), then put a wrapper around that function:

Code: Select all

if OrgAddUnitClass ~= nil then print('Someone else already created a OrgAddUnitClass...  Something will be lost!') end
OrgAddUnitClass = AddUnitClass
AddUnitClassCalled = false
AddUnitClass = function(...)   --... is the correct code, it stands for variable arguments
    if AddUnitClassCalled == false then
        --warning: you may or may not have to make sure the team has already been created.  If so, check method's team parameter and only add your custom unit on the same team.  And you'll have to change AddUnitClassCalled to a 9 element array of AddUnitClassCalled[team]
        --TODO read in your data file and load your custom units
        --Note: this will be triggered on the first AddUnitClass function call.  Mess with the if statement if you want to add your unit after a sepcific unit, only on a specific team, after the 3rd call, or etc...
        AddUnitClassCalled = true
    end

    return OrgAddUnitClass(unpack(args))   --don't forget the return keyword, missing that caused v1.3's Leia spawn bug.  Also I'm guessing on the 'args' name.  It might be 'arg'.  Check Lua's documentation on variable function arguments
end
Also, setup_teams on the PC is replaced by every mod map (it's in modder's mission.req so their custom version will override the default one), so you can't make a change there and have it effect every map unless things are different on the XBox (and they very well could be for your setup).