I didn't really debug your code, but if
returned# is only 1 or 0 why are you setting it to 0 when it already is?
Code:
if returned# ~= 1 then returned# = 0
Reads: if returned# is not equal to 1 then returned# = 0
I wrote and tested a method/function that you could stick into any map to enable your rule. It also scales to any amount of teams. Intrestingly, I don't think you need to re-enable the region (but I left that section in anyway). When the flag gets reset, I think the capture region reactivates. I didn't look into why this happens (maybe the AI goal code does it?).
This table contains the capture regions. The first value is team 1's capture region. The second value is team 2's capture region. The thrid value would be team 3's capture region... Node: team 0 is not supported, but can be if you need it. Generally, this code would be in ScriptPostLoad(). In the example, the rule will not be enforced on team 3.
Code: Select all
--table holds the capture regions for each team following the rule
--use nil to skip that team from following the rule
local regions = {
"team1_capture",
"team2_capture",
nil,
"team4_capture_region",
}
--enable the rule
cantCaptureIfOwnFlagTaken( regions )
This is the method that enables the rule. You can put it in the Lua outside of any other method.
Code: Select all
--Rule by Ace_Azzameen_5: "You can't capture the enemy flag if your flag has been taken"
function cantCaptureIfOwnFlagTaken( captureRegions )
--rule pointless if there are no know capture regions
if captureRegions == nil then return end
--when a team's flag is taken, prevent that team from being able to score
OnFlagPickUp(
function( flag, carrierObj )
--output for debugging
print("cantCaptureIfOwnFlagTaken(): OnFlagPickup()")
--get the team whose flag has been taken
if flag == nil then return end
local team = GetObjectTeam( flag )
--get the team's capture region
local region = captureRegions[team]
if region == nil then return end
--disable the team's capture region so it cannot score
DeactivateRegion( region )
--output for debugging
print("cantCaptureIfOwnFlagTaken(): OnFlagPickUp(): Team = " .. team .. ", Region = " .. region .. ", Action = Disabled")
end
)
--when a team's flag is returned, allow that team to score
OnFlagReset(
function( flag, carrierObj )
--output for debugging
print("cantCaptureIfOwnFlagTaken(): OnFlagReset()")
--get the team whose flag has been returned
if flag == nil then return end
local team = GetObjectTeam( flag )
--get the team's capture region
local region = captureRegions[team]
if region == nil then return end
--enable the team's capture region so it can score
ActivateRegion( region )
--output for debugging
print("cantCaptureIfOwnFlagTaken(): OnFlagReset(): Team = " .. team .. ", Region = " .. region .. ", Action = Enabled")
end
)
--output for debugging
print("cantCaptureIfOwnFlagTaken(): Rule now in effect")
end
Something else to point out, OnFlagReturn() is never used in any official Lua script. The docs say its for when a friendly unit returns it's own flag. In CTF, you normally can't pickup your own flag. If you want when the flag gets reset after being dropped or carried too long use OnFlagReset(), which isn't in the docs but is used in the scripts.
Can you really make it so a carrier player can press a button and drop the flag without dieing, then pick it up again? Or was I not being specific enough?
I assumed you meant when a player died, he dropped the flag and another could pick it up (which is what normally happens, so its possible). The way you want it might not work too well. If you drop a flag, it drops right where you are. You'd pick it right back up. An example of this might be like in space CTF when you fly into a hanger while holding the flag. You may be able to change a unit properity to prevent the unit from picking up flags, then force that unit to drop the flag.
In order to determine when a player wants to drop the flag, he would have to trigger an event callback, such as OnCharacterDispenseControllable(), meaning he needs to be given a drop flag weapon when picking up the flag.
I'm thinkign to drop a flag, you kill the flag object then respawn it at the unit's location (works like the teleporter).