SetUnitCount not working [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
jedimoose32
Field Commander
Field Commander
Posts: 938
Joined: Thu Jan 24, 2008 12:41 am
Projects :: Engineering Degree
Location: The Flatlands of Canada

SetUnitCount not working [Solved]

Post by jedimoose32 »

Hi everyone.

I'm attempting to have my defending team's unit count reduced to 10 units on the map, toward the end of the battle. However the SetUnitCount code does not seem to be working and there are still over 50 units on the defending team after the code runs. Here is the function it runs from. You'll find the SetUnitCount bit about halfway down:
Hidden/Spoiler:
[code]
function LastStand()
if GetObjectTeam("cp6") == ATT and GetObjectTeam("cp7") == ATT then
--Tell the humans where to go next.
ShowMessageText("level.dda.objectives.laststand1",1)
ShowMessageText("level.dda.objectives.laststand2",2)
--Let's muck around with the spawn weights to produce a better final battle sequence.
SetProperty("cp6", "AISpawnWeight", "500") --FOR THE REPUBLIIIIIC
SetProperty("cp7", "AISpawnWeight", "500") --ALSO FOR THE REPUBLIIIICCCC
SetProperty("cp8", "AISpawnWeight", "750") --For the CIS...
--And it's actually pretty vital that they try and fight over this particular CP.
SetProperty("cp8", "Value_DEF_CIS", "2000")
SetProperty("cp8", "Value_ATK_Republic", "2000")
--We'll remove distractions.
SetProperty("cp1", "Value_DEF_Republic", "0")
SetProperty("cp2", "Value_DEF_Republic", "0")
SetProperty("cp3", "Value_DEF_Republic", "0") --etc
AICanCaptureCP("cp6", DEF, false)
AICanCaptureCP("cp7", DEF, false)
AICanCaptureCP("cp8", ATT, true)
--To avoid the Battle of Thermopylae...
DEF_ReinforcementCount = GetReinforcementCount(DEF)
SetReinforcementCount(DEF, DEF_ReinforcementCount + 100)
SetUnitCount(DEF, 10)
--And we start the countdown. After 4min Republic loses unless they cap the CP
StartTimer(endgame)
ShowTimer(endgame)
OnTimerElapse(
function(endgame)
if GetObjectTeam("cp8") == DEF and DEF_ReinforcementCount > 0 then
SetReinforcementCount(ATT, 0)
end
DestroyTimer(endgame)
end,
endgame
)
OnFinishCaptureName(
function (repvictory)
if GetObjectTeam("cp8") == ATT then
SetReinforcementCount(DEF, 0)
end
end,
"cp8"
)
else
ShowMessageText("level.dda.objectives.pushback1",1)
ShowMessageText("level.dda.objectives.pushback2",2)
end
end
[/code]
EDIT: SOLVED
My idea of what SetUnitCount does behind the scenes was wrong this whole time. I thought it would check the map to see how many units are alive and if that number was more than the new unit count setting then it would stop spawning AI until the number of alive units dropped to below the newly set count. However, the game doesn't seem to care how many are already alive, it just keeps turning out new units. When I tested my various "solutions" this is what was happening. The game just kept replacing the units my team was killing at a rate roughly equivalent to that at which we were destroying them.

So... my solution (and I'm sure this is one of many different possible solutions to this) is this:
Hidden/Spoiler:
[code]
SetUnitCount(2,15)
AllowAISpawn(2,false)
can2Spawn = CreateTimer("can2Spawn")
SetTimerValue(can2Spawn, (35))
StartTimer(can2Spawn)
OnTimerElapse(function(TwoCanSpawn)
AllowAISpawn(2,true)
DestroyTimer(can2Spawn)
end
,can2Spawn)
[/code]
Essentially as soon as I set my new unit limit I stop the AI on team 2 from spawning for 35 seconds and then let them spawn again. Eventually I'll probably implement a more sophisticated solution such as tracking how many units are running around and preventing AI spawning until that number = 15. For now this works fine and seems pretty balanced.
TL;DR: Marth was right, see below
Last edited by jedimoose32 on Thu Nov 06, 2014 4:31 pm, edited 1 time in total.
Marth8880
Resistance Leader
Posts: 5042
Joined: Tue Feb 09, 2010 8:43 pm
Projects :: DI2 + Psychosis
Games I'm Playing :: Silent Hill 2
xbox live or psn: Marth8880
Location: Edinburgh, UK
Contact:

Re: SetUnitCount not working

Post by Marth8880 »

But are other units still spawning? The limit is set, but there are still probably more spawned beforehand.
jedimoose32
Field Commander
Field Commander
Posts: 938
Joined: Thu Jan 24, 2008 12:41 am
Projects :: Engineering Degree
Location: The Flatlands of Canada

Re: SetUnitCount not working

Post by jedimoose32 »

Yes. Other units are still spawning. I've continued the battle for 5+ minutes after the switch should have happened and it is pretty clear that there are still 70 CIS units on the map (the number 70 is defined at team setup). I'm not sure what I've missed.

Edit: I tried setting up my CIS team as you see below, without the unit count parameters after the unit names, because I saw that that's how they were set up in kas2c_c.lua and I thought it may fix things. I tried removing the unit counts altogether but found there were too many snipers, so I re-added some of them, in small enough quantities that the ones that are defined should still equal less than 10:

Code: Select all

cis = {
			team = CIS,
			units = 55,
			reinforcements = 820,
			soldier  = { "cis_inf_rifleman"}, --9,55
			-- assault  = { "cis_inf_rocketeer",1, 4},
			engineer = { "cis_inf_engineer",2,3}, --1,5
			sniper   = { "cis_inf_sniper",2,2}, --1,5
			officer = {"cis_inf_officer",2,3}, --1,5
			-- special = { "cis_inf_droideka",1, 4},
		}
Unfortunately this did not help. After the switch was supposed to happen I gave it a few minutes, killed at least 40 or 50 units, and then went over a small hill to see a stream of 25+ CIS units running toward me from their CP. I don't understand how this is not working.
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: SetUnitCount not working

Post by [RDH]Zerted »

There's a GetUnitCount(team) you could use to double check that the unit count is changing. FC uses SetUnitCount() for the 'Add Units to Team 1/2' commands.

I'd recommending using FC to kill all the AI units a couple times to make sure that the existing units aren't the problem.

The other thing to check is to see if SetUnitCount works to increase the unit count. Maybe decreasing doesn't work (but I thought it did).

Everything else works right? DEF's reinforcements change? DEF is defined? Nothing in the debug log?
jedimoose32
Field Commander
Field Commander
Posts: 938
Joined: Thu Jan 24, 2008 12:41 am
Projects :: Engineering Degree
Location: The Flatlands of Canada

Re: SetUnitCount not working

Post by jedimoose32 »

DEF is defined. The reinforcements increase appropriately as well. The whole function works perfectly except for the SetUnitCount. I'll try the tests you suggested.

Edit: I re-read your comment about the FC command, and then I went and tried to use it, and the prompt says negative numbers are accepted. So my theory (untested as of this moment because I'm tired of testing for this evening) is that SetUnitCount does not "set" the unit count but instead increases it if a positive value is used and decreases it if a negative value is used.
Again, this is totally theoretical but it would explain why I was still seeing a truckload of SBDs heading my way... the command was adding 10 units to the CIS side instead of setting it to 10.

I will report back with my findings soon.
hunpeter12
Command Sergeant Major
Command Sergeant Major
Posts: 260
Joined: Mon Apr 18, 2011 2:53 pm
Projects :: Underground City The Complex [WIP]
Games I'm Playing :: SWBF2

Re: SetUnitCount not working

Post by hunpeter12 »

It would make sense, since the FC command is called Add units to team.
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: SetUnitCount not working

Post by [RDH]Zerted »

No, that's not how those FC commands work. Here's their code:

Code: Select all

		ff_AddCommand( "Add Units to Team 2", nil, function()
			local temp = function( rate )
				if not rate then return end
				SetUnitCount(2, GetUnitCount(2) + rate)
			end
			
			ff_AskUser("Please enter the amount of units to add to team 1 (negative numbers allowed)", temp, ifs_fakeconsole )
		end, nil)
It takes the existing number of units and combines that with whatever number the user typed.
jedimoose32
Field Commander
Field Commander
Posts: 938
Joined: Thu Jan 24, 2008 12:41 am
Projects :: Engineering Degree
Location: The Flatlands of Canada

Re: SetUnitCount not working

Post by jedimoose32 »

So maybe what I'll try is

Code: Select all

SetUnitCount(2, GetUnitCount(2) - 45)
That format pretty clearly works for the FC command so I may as well give it a shot.

Edit: I'm actually going to try making a variable for the CIS unit count before I call SetUnitCount. My code looks like this now:

Code: Select all

DEF_UnitCount = GetUnitCount(2)
SetUnitCount(2, DEF_UnitCount - 45)
Edit 2: Still no luck with this at all. Just to confirm that I'm trying to go about this correctly, this is the value I'm trying to change:
Hidden/Spoiler:
[code]
cis = {
team = CIS,
units = 55, <-----------------------------
reinforcements = 820,
soldier = { "cis_inf_rifleman",2,50}, --9,55
-- assault = { "cis_inf_rocketeer",1, 4},
engineer = { "cis_inf_engineer",2,3}, --1,5
sniper = { "cis_inf_sniper",2,2}, --1,5
officer = {"cis_inf_officer",2,3}, --1,5
-- special = { "cis_inf_droideka",1, 4},
}
[/code]
And the command I'm using:

Code: Select all

SetUnitCount(team, count)
Maybe I'm going about this all the wrong way?

Edit 3: For Science, I changed this to zero:
Hidden/Spoiler:
[code]
cis = {
team = CIS,
units = 0, <--------------------------------
reinforcements = 820,
soldier = { "cis_inf_rifleman",2,50}, --9,55
-- assault = { "cis_inf_rocketeer",1, 4},
engineer = { "cis_inf_engineer",2,3}, --1,5
sniper = { "cis_inf_sniper",2,2}, --1,5
officer = {"cis_inf_officer",2,3}, --1,5
-- special = { "cis_inf_droideka",1, 4},
}
[/code]
...and then had my SetUnitCount ADD units instead. Like so:

Code: Select all

DEF_UnitCount = GetUnitCount(2)
SetUnitCount(2, DEF_UnitCount + 40)
And it definitely did not work. There were absolutely no CIS units at any point on this map. So something's up with my code, maybe?

Edit 4:

Code: Select all

		cis = {
			team = CIS,
			units = 55,
			reinforcements = 820,
			soldier  = { "cis_inf_rifleman",2,50}, --9,55
			-- assault  = { "cis_inf_rocketeer",1, 4},
			engineer = { "cis_inf_engineer",2,3}, --1,5
			sniper   = { "cis_inf_sniper",2,2}, --1,5
			officer = {"cis_inf_officer",2,3}, --1,5
			-- special = { "cis_inf_droideka",1, 4},
		}
	}
	
	SetUnitCount(2,10) <----------------------
This worked, however it sets the unit count to 10 when the game starts and I still can't change it later on. I even tried setting up a function with no conditions (just gets called right at the start of the round) that should immediately change the unit count but that does nothing.

Edit 5: Solved it! See first post for the solution.
Post Reply