Buff unit's damage in scripts? [Solved]

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
Tempo
Recruit Womprat Killer
Posts: 12
Joined: Mon Jan 04, 2021 10:22 pm
Projects :: No Mod project currently.
xbox live or psn: No gamertag set

Buff unit's damage in scripts? [Solved]

Post by Tempo »

I'm trying to custom-balance a mod I downloaded. There's a melee class that I think is too weak, and I want to buff them. I know you can't modify weapons in mission scripts, so I tried giving them a hero's lightsaber with the same moveset, but the game crashes when the unit loads (either in-game, or on the class select menu,) possibly because the idle animations are different. I looked in the "rage" weapon file, and tried applying its "BuffOffenseMult" to the class, but that did nothing. Is there a way to buff damage this way, or is there some other work-around?
Last edited by Tempo on Mon Mar 22, 2021 11:48 pm, edited 1 time in total.
User avatar
Kingpin
Jedi
Jedi
Posts: 1096
Joined: Fri Sep 13, 2013 7:09 pm
Projects :: The Sith Wars II
Location: Denver, CO
Contact:

Re: Buff unit's damage in scripts?

Post by Kingpin »

Do you have access to the source files or are you working from what's already munged? I feel like there are lots of ways to go about this, but figuring that out should give you a better place to start. If you have the unmunged assets, you can go into the weapon ODFs and tweak as needed.
Tempo
Recruit Womprat Killer
Posts: 12
Joined: Mon Jan 04, 2021 10:22 pm
Projects :: No Mod project currently.
xbox live or psn: No gamertag set

Re: Buff unit's damage in scripts?

Post by Tempo »

no, I don't have the odf's
User avatar
cbadal
Corporal
Corporal
Posts: 155
Joined: Sun Jan 18, 2015 5:23 pm
Projects :: SWBF2 XBOX Mod Environment
Games I'm Playing :: SW Battlefront 2
xbox live or psn: No gamertag set
Contact:

Re: Buff unit's damage in scripts?

Post by cbadal »

I've done something like this in 'ScriptPostLoad()' before:

Code: Select all

-- #################### Chaos Boba Fett ###########################
	SetClassProperty("imp_hero_bobafett", "WeaponAmmo1", "7")  -- the ammo goes fast, don't give too much because it'll make him way OP   
	SetClassProperty("imp_hero_bobafett", "WeaponAmmo2", "75") -- get him upto chaos levels of ammo 
	SetClassProperty("imp_hero_bobafett", "WeaponAmmo3", "75") -- get him upto chaos levels of ammo 
	SetClassProperty("imp_hero_bobafett", "WeaponAmmo4", "75") -- get him upto chaos levels of ammo 

Last edited by cbadal on Wed Mar 10, 2021 2:00 pm, edited 1 time in total.
Sporadia
Corporal
Corporal
Posts: 151
Joined: Thu Jan 24, 2019 11:02 pm
Projects :: No Mod project currently
Games I'm Playing :: None
xbox live or psn: No gamertag set

Re: Buff unit's damage in scripts?

Post by Sporadia »

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.
User avatar
AnthonyBF2
Sith
Sith
Posts: 1255
Joined: Wed Aug 21, 2013 3:55 pm
Projects :: PS2+PSP Overhaul

Re: Buff unit's damage in scripts?

Post by AnthonyBF2 »

You can use SleepKiller's unmunge tool to rip the ODFs, then compared the ODFs against the compiled LVL file, and edit the damage values in hex editor.
Post Reply