Events#
The interaction between the BF2 engine and Python programs is largely event-driven: when an “event” happens in a game, BF2 checks to see if any Python programs have registered to be notified of that particular event; if they have, they are called (a “callback”). One way to think about this: the events are “hooks” into the game engine to which you can attach your own Python code.
Multiple callbacks can be registered for any given event; all registered callbacks will be executed when the event occurs. For example, if three different programs have registered for the “PlayerConnect” event, then when a new player connects, all three of those programs will be run.
Types of Events#
The BF2 engine supports five types of events
Standard Events#
host.registerHandler('EventName', pythonEventHandler[, parameter])
This registration will typically be done in the init() method of an object, but could be done anywhere in your code.
Game Status Events#
The BF2 engine generates these events when the status of the game changes.
host.registerGameStatusHandler(pythonEventHandler)
The possible states game status can be in (as enumerated in bf2.GameStatus
) are:
NotConnected
PreGame
Playing
Paused
EndGame
RestartServer
Game status events are usually used to kick off pre-game initialization or post-game clean-up processing.
Trigger Events#
“Trigger” events activate when a vehicle comes within a specified distance of an object.
createHemiSphericalTrigger(object, triggerEventHandler, objectName, radius[, data])
bf2.triggerManager.createRadiusTrigger(object, triggerEventHandler, objectName, radius[, data])
In standard Battlefield 2, trigger events are used to track scoring for flag captures (triggers activate whenever a player enters or leaves a fixed radius around each control point), as well as effects such as birds (birds are triggered to take flight whenever a player enter their trigger radius).
Timer Events#
- bf2.Timer(timerEventHandler, delta, alwaysTrigger[, data])#
Timer events activate when a certain amount of time has elapsed.
In standard Battlefield 2, timer events are used to cause periodic status checks related to scoring, ranking, punishing TKs, etc.
See the description of the
bf2.Timer
class for details.
Descriptions of Events#
EA/DICE have not published a list of events, but here is a list of events that have been discovered so far.
- host.EventName(handlerParameter1, handlerParameter2, ...)#
The
EventName
is the name of the event to register for with host.registerHandler
; the parameters are the parameters your handler should receive when called.- Parameters:
playerObject – An instance of
bf2.PlayerManager.Player
playerID – As players connect to the server they are assigned playerID numbers from “1” up
squadID (int) – Squads for each side are independently numbered beginning at “1” and increasing thereafter. Players not on a squad, including team commanders, are assigned to squad “0”
teamID (int) – 0 for US, 1 for China or MEC
Server Events#
- host.ServerReady()#
TBD
- host.BeginRound()#
TBD
- host.EndRound()#
TBD
Player Events#
- host.PlayerConnect(playerObject)#
Fires when a player connects to the game.
- host.PlayerSpawn(playerObject, soldierObject)#
Occurs when a player spawns, and associates a
playerObject
“spirit” with asoldierObject
“body”.
- host.PlayerChangeTeams(playerObject, humanHasSpawned)#
- Parameters:
humanHasSpawned – 1 if the player the event is firing for is a human (as opposed to an AI). Does not fire due to team change from “setTeam”!
- host.PlayerChangeWeapon(playerObject, oldWeaponObject, newWeaponObject)#
Player has changed which weapon they are holding.
- host.PlayerChangedSquad(playerObject, oldSquadID, newSquadID)#
SquadID
is 0 for players who are not members of any squad (including the team commander).
- host.PlayerScore(playerObject, difference)#
Difference is change to player’s score (positive or negative).
- host.PlayerHealPoint(givingPlayerObject, receivingSoldierObject)#
Occurs whenever a player heals another player; there is no indication of how much health was restored.
- host.PlayerRepairPoint(givingPlayerObject, receivingVehicleObject)#
Occurs whenever a player repairs a vehicle; there is no indication of how much damage was repaired.
- host.PlayerGiveAmmoPoint(givingPlayerObject, receivingPhysicalObject)#
Occurs whenever a player replenishes the ammo in another player or vehicle. There is no indication of how much ammo was replenished.
- host.PlayerTeamDamagePoint(playerObject, victimSoldierObject)#
Occurs whenever a player damages another player on their own team. There is no indication of how much damage was done.
- host.PlayerKilled(victimPlayerObject, attackerPlayerObject, weaponObject, assists, victimSoldierObject)#
This event occurs when a player is “critically injured”, but still capable of being revived. If any players assisted in the kill, then “assists” will be a tuple of tuples: the top-level tuple will contain one lower-level tuple for each assisting player; the lower-level tuples will each have two elements: a
playerObject
for one of the assisting players, and a number indicating the type of assist (1=targeting assist, 2=kill damage assist, 3=driver assist).
- host.PlayerRevived(revivedPlayerObject, medicPlayerObject)#
Player has been revived from a “critically injured” condition to full health by a medic using “shock paddles”. Players can only be revived in the interval between a
PlayerKilled()
event and a subsequentPlayerDeath()
event.
- host.PlayerDeath(playerObject, soldierObject)#
This event occurs when a player is decisively dead, and cannot be revived; they will only return to the game by respawning.
soldierObject
is the soldier “body” (which is now discarded) for theplayerObject
“spirit” (which will persist through the next spawn).
- host.PlayerBanned(playerObject, time, type)#
TBD
- host.PlayerKicked(playerObject)#
Player has been kicked off the server.
- host.PlayerDisconnect(playerObject)#
Player has disconnected from the server.
Vehicle and Kit Events#
- host.EnterVehicle(playerObject, vehicleObject[, freeSoldier])#
Player represented by
playerObject
has entered the vehicle represented byvehicleObject
.freeSoldier
is 1 if the player is a passenger of the vehicle and is able to use weapons/objects of his kit. i.e. seat 4(+) of a black hawk.
- host.ExitVehicle(playerObject, vehicleObject)#
Player has exited a vehicle.
- host.VehicleDestroyed(vehicleObject, attackerObject)#
vehicleObject
has been destroyed byattackerObject
.
- host.PickupKit(playerObject, kitObject)#
Player has picked up a kit. This occurs both when a player physically picks up a kit, as well as when a player spawns.
- host.DropKit(playerObject, kitObject)#
Player has dropped a kit; can occur either because a player has died, or because a player has picked up a different kit.
Team Events#
- host.Reset(data)#
TBD
- host.ChangedCommander(teamID, oldCommanderPlayerObject, newCommanderPlayerObject)#
Occurs when a team changes commanders; the old or new commander may be “None”.
- host.ChangedSquadLeader(squadID, oldLeaderPlayerObject, newLeaderPlayerObject)#
Occurs when a squad changes squad leaders; the old or new squad leader may be “None”.
Game Events#
- host.ControlPointChangedOwner(controlPointObject, attackingTeamID)#
TBD
- host.TimeLimitReached(value)#
Event fires when the fixed time limit for a round expires (not sure what “value” is)
- host.TicketLimitReached(team, limitID)#
TBD
Command Events#
- host.ConsoleSendCommand(command, args)#
This event is triggered by someone using the
pythonHost.sendCommand
console command. Unfortunately, this command/event mechanism appears to be disabled in non-ranked servers. (As a workaround, you can achieve a similar effect creating and registering your own custom RCon commands.
- host.RemoteCommand(playerId, cmd)#
Event fires when a game client gives an
RCon
(remote console) command.cmd
is the command the player gave. This event is used by the admin module to receive and processRCon
commands from players;RCon
commands from TCP connections are received by the admin module directly, and do not fire this event.
- host.ClientCommand(command, issuerPlayerObject, args)#
This event occurs when a game client issues certain in-game commands. The only such commands identified so far are: command=100 means player wants to punish teammate for teamkill; command=101 means player wants to forgive. In both cases, args=1. This response is generated by the player responding to an on-screen prompt caused by a call to the
bf2.gameLogic.sendClientCommand
method.
Miscellaneous Events#
- host.PlayerUnlocksResponse(succeeded, player, unlocks)#
Unlocks are requested and received asynchronously. This event is triggered when the response for player (which is an object of type Player) are received. If the ranking server did not successfully find the player in question,
succeeded
is set to false, else true. unlocks is a list of kit ids, which are 11, 22, 33 .. 77.
- host.PlayerAwardsResponse()#
TBD
- host.ChatMessage(playerId, text, channel, flags)#
channel
isSquad
,Team
,Global
,ServerTeamMessage
orServerMessage
. Text is the text of the message sent, prefixed byHUD_TEXT_CHAT_TEAM
orHUD_TEXT_CHAT_SQUAD
if the message is sent to the Team or Squad channel; there is no prefix if it is sent to the Global channel.playerId
is -1 for all server messages. If the player sending the message is dead, their message is also prefixed byHUD_CHAT_DEADPREFIX
. (Strings such asHUD_TEXT_CHAT_TEAM
,HUD_CHAT_DEADPREFIX
, and so on, are used as keys by the game engine to match and replace with localized text.)
- host.PlayerStatsResponse(succeeded, player, response)#
TBD
- host.Update(data)#
TBD
Non-Standard Events#
- host.GameStatusChangedEvent(status)#
This type of event is registered for with
host.registerGameStatusHandler()
.status
is an integer status code enumerated bybf2.GameStatus
.
- host.TriggerEvent(triggerID, object, vehicle, enter, userData)#
This type of event is registered for with
bf2.triggerManager.createHemiSphericalTrigger
orbf2.triggerManager.createRadiusTrigger
; triggertriggerID
is activated whenvehicle
comes within the preset radius ofobject
.
Mysterious Events#
These strings were found in the server executable, and appear to be internal game engine events. They seem to not be accessible from Python, except in a few cases, where there is a correspondence between these “events” and Python events (e.g. “BeginRoundEvent” may be the same as “BeginRound”). For more information, see BF2 Internals.
- host.RemoveSpawnGroupEvent()#
- host.CreateSpawnGroupEvent()#
- host.ContentCheckEvent()#
- host.UAVEvent()#
- host.MissileInitEvent()#
- host.UnlockEvent()#
- host.MedalEvent()#
- host.ToggleFreeCameraEvent()#
- host.VoipSessionEvent()#
- host.RequestEvent()#
- host.PythonCommandEvent()#
- host.EndOfRoundEvent()#
→ EndRound
- host.BeginRoundEvent()#
→ BeginRound
- host.TargetDirectionEvent()#
- host.VoteEvent()#
- host.VoipPlayerMuteEvent()#
- host.SupplyDropEvent()#
- host.CommanderCamEvent()#
- host.VoipOnOffEvent()#
- host.AmbientEffectAreaEvent()#
- host.StickyProjectileEvent()#
- host.ArtilleryEvent()#
- host.SpottedEvent()#
- host.SetSquadLeaderEvent()#
- host.SetAcceptOrderEvent()#
- host.KickBanEvent()#
- host.RankEvent()#
- host.InviteEvent()#
- host.IssueSquadOrderEvent()#
- host.SetPrivateSquadEvent()#
- host.ChangeSquadNameEvent()#
- host.KilledByEvent()#
- host.RadioMessageEvent()#
- host.CommanderEvent()#
- host.LeaveSquadEvent()#
- host.JoinSquadEvent()#
- host.StringBlockEvent()#
- host.HandleDropEvent()#
- host.HandlePickupEvent()#
- host.ChangePlayerNameEvent()#
- host.PostRemoteEvent()#
- host.ExitVehicleEvent()#
→ ExitVehicle
- host.EnterVehicleEvent()#
→ EnterVehicle
- host.DestroyPlayerEvent()#
- host.DestroyObjectEvent()#
- host.CreateKitEvent()#
- host.CreateObjectEvent()#
- host.CreatePlayerEvent()#
- host.DataBlockEvent()#
- host.ConnectionTypeEvent()#
- host.ChallengeResponseEvent()#
- host.ChallengeEvent()#
Event Patterns#
While each event documented here corresponds to a discrete change of game state, there are certain actions that happen in the game that cause a fixed sequence of events to happen every time-a PlayerSpawn event, for example, is always followed by a PlayerChangeWeapon()
event and a PickupKit event. This section documents some of these recurring patterns.
Player Spawn#
-
Player becomes associated with a
soldierObject
. -
From
None
to aweaponObject
.
Player Killed#
-
The victim drops their kit.
-
If player is inside a vehicle.
Player Revived#
-
For attacker.
-
Victim drops their kit.
-
Revived player picks up the nearest kit [hopefully, but not necessarily, their old one]; if there is no kit nearby, they default to picking up an “assault” kit.
-
For medic who did the revive.
Flag Change#
-
When neutralized.
PlayerScoreEvent()
Score for having neutralized flag, repeated for each attacker in CP radius.
-
When capture complete.
PlayerScoreEvent()
Score for having completed capture, repeated for each attacker in CP radius.
Player Kicked#
Player Banned#
Overall Game State Change#
StatusChange()
To
PreGame
.-
This event doesn’t happen in the first round after a server starts.
StatusChange()
To
Playing
; stay here until enough players connect.StatusChange()
To
PreGame
, once enough players have connected.StatusChange()
To
Playing
-the real game round now begins.Play game, beginning with players spawning in.
StatusChange()
To
EndGame
.-
For each player still in game at end.