The simplest thing you could try is munging a unit in a new side file which uses the lightsaber that you want. Then put a
ReadDataFile() in your user script which loads that unit. This will load the lightsaber weapon alongside the new unit that you've made. It's important to load the weapon before you try to add it to anything else with
SetClassProperty(). The file path in
ReadDataFile() is slightly different when you're pointing to a separate mod though. You don't use
"dc:side" when you're loading a unit from a separate mod. Instead the file path goes from the game's addon folder like this:
ReadDataFile("..\\..\\addon\\ABC\\data\\LVL_PC\\SIDE\\def.lvl", ) where
ABC is the mod folder and
def is the side. I don't know if this method will work for a melee weapon because of the animations, but it is what you do for skin changes and I think adding rifles etc.
Failing that, you can recreate the unit as you want it in a separate side mod. Then you need to load your unit with
ReadDataFile() in your user script using the file path I've described above (
"..\\..\\addon\\ABC\\data\\LVL_PC\\SIDE\\def.lvl"). Finally, you will need to hack code into the
AddUnitClass() function.
SetUpTeams() actually calls
AddUnitClass() so you only need to make changes to
AddUnitClass(). I have some example code where I've replaced Han Solo and the Wookiee Warrior with patched versions:
Code: Select all
-- inject code into AddUnitClass
if AddUnitClass then
-- error check
if AddUnitClass_Bak then
print("Warning: AddUnitClass_Bak is already defined")
end
-- backup the normal AddUnitClass function as AddUnitClass_Bak
AddUnitClass_Bak = AddUnitClass
-- redefine AddUnitClass to something new
AddUnitClass = function(...)
-- ======== Code injected into AddUnitClass ========
-- check every argument for the names of the units that need to be replaced
for i, unit in ipairs(arg) do
if (unit == "all_hero_hansolo_tat") or (unit == "ALL_hero_hansolo_tat") then
-- name of the old han solo has been found
-- replace this with the name of the new han solo
arg[i] = "ptc_all_hero_hansolo"
elseif (unit == "all_inf_wookiee") or (unit == "ALL_inf_wookiee") then
-- name the old wookiee has been found
-- replace this with the name of the new wookiee
arg[i] = "ptc_all_inf_wookiee"
end
end
-- =============================================
-- run the normal AddUnitClass now that the arguments have been modified
AddUnitClass_Bak(unpack(arg))
end
else
print("Error: AddUnitClass function not defined")
end
This adds code to the beginning of
AddUnitClass() by redefining the function. The original code for the
AddUnitClass() function doesn't run until you reach the line
AddUnitClass_Bak(unpack(arg)). In Zerted's docs for the 1.3 patch, there's an example where code is added to the
ScriptPostLoad() function using the same method. First it backs up the code for the
AddUnitClass() function as
AddUnitClass_Bak(). Then you need to go through all the arguments that were passed to
AddUnitClass(), and look for the name of the unit that you want to replace. And then you replace that string with the name of your new unit. Finally, you call
AddUnitClass_Bak() after replacing the unit's name. It's a bit more complicated than that because the function has multiple arguments, but that's the general idea.
Edit section: I still see a problem with changing a melee unit though, in case you need to increase the memory pool sizes for the Combo data. Then you might have to inject code into
SetMemoryPoolSize as a workaround which could get a bit complicated.
I'm sure there are other approaches but these are the 2 ways that jump out at me. Also the second method requires you to get the assets for that unit from somewhere (probably from the original makers of the mod since it's courteous to let them know what you're doing anyway).
You will also need to read the localisation keys from your new side mod in order to name everything. So your user script will need the line
ReadDataFile("..\\..\\addon\\ABC\\data\\_LVL_PC\\core.lvl")
But actually from a game design standpoint, the simplest thing you can do is balance this unit by giving it more health or something that's easier to mod. Or maybe you could use an event to check when this unit deals damage to an object and add a bit extra, or check when this unit gets a kill and give it bonus health or something. That has it's pros and cons too.