Talk:Event Reference

From BF2 Technical Information Wiki
Revision as of 03:09, 23 October 2006 by Andrewd (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Standard Events

To register to receive a callback for a particular standard event:

 host.registerHandler('EventName', pythonEventHandler[, parameter])

I'm thinking the third parameter is to control pre/post hooking/handling. I tried registering a function for PlayerConnect and accessing player.getName(), but nothing was returned. Then I set the third parameter to 1 and player.getName() returned their name. --Dackz


(Woody) Some info: a little playing with grep shows that in the standard DICE Python code, all handler registrations use no parameter except the following:

  • PlayerConnect
  • PlayerDisconnect
  • Reset
  • SendCommand
  • StatsResponse
  • UnlocksResponse
  • TimeLimitReached (only one that is registered both with and without a parameter)

Based on the nature of these events, I'm wondering whether the parameter might have something to do with blocking/threading--maybe a "1" parameter means the method should block?


Ok, we've got it: the final parameter indicates whether the pythonEventHandler should always be run (parameter evaluates as "true" (e.g. "1")), or only when the game is in Playing status (i.e., the handler won't run in PreGame and PostGame). --Woody 21:09, 28 Jul 2005 (MDT)


Is there a way to determine the order of handlers on a given event, or at least to ensure a certain order of operations? For example, if I wanted to log players that connected to a server, but players were also booted off the server during PlayerConnect inb another module, is there a way to make certain that the logging only catches those who actually connect? Or is the idea moot because the PlayerConnect would fire through everything, even if the kick were the first thing to happen? --pokerface 11:45, 3 September 2006 (MDT)

Mysterious Events

These are all most likely engine-related and in no way accessible via the Python interface. --Dackz

I suspect you're right; a few of these are accessible, but just a few. Many of them correspond to events in the BFV XML log files--the similarities are a striking testament to commonality in the BFV/BF2 code bases. But since they seem to be inaccessible, it's probably academic. --Woody


Event Patterns

Has anyone been able to get any of the server events to work?

-- Interex

Is there verification on whether or not PlayerConnect fires on EVERY spawn (including the ones that happen during the course of the game)? --pokerface 22:49, 10 Jul 2005 (MDT)


From what I've seen, PlayerConnect only fires when a player connects to the server--not when they spawn. Once they're connected, PlayerSpawn fires every time they spawn. It also appears that, since the server doesn't restart between rounds, once a player connects, there will be no futher PlayerConnect events for that player in subsequent rounds--as far as the server is concerned, they already connected, even if it wasn't within this round. --Woody


The PlayerSpawn event does not seem to get called. Anyone who can confirm that the PlayerSpawn event is broke ? --Quade 05:20, 20 Jul 2005 (MDT)


The PlayerSpawn event was working fine for me last time I checked. --King of Camelot 09:13, 20 Jul 2005 (MDT)


Works fine for me too; here's some working code that I've used:

 import host
 
 host.registerHandler('PlayerSpawn', onPlayerSpawn, 1)
 
 def onPlayerSpawn(playerObject, soldierObject):
     print "----- PlayerSpawn Event; player:", playerObject.getName()

--Woody 09:48, 20 Jul 2005 (MDT)


If the reason you think the PlayerSpawn event is broken is because the code registered for that event isn't working, double check your code. One error will cause code below it to not be executed. I'll use Woody's example to demonstrate:

 import host
 
 host.registerHandler('PlayerSpawn', onPlayerSpawn, 1)
 
 def onPlayerSpawn(playerObject, soldierObject):
     print playerObject.fakeMethod() <------This line won't work
     print "Player Spawned" <-----------------------This line won't be executed

--King of Camelot 10:54, 20 Jul 2005 (MDT)

Thank you guys, I'll try it out. --Quade 03:36, 21 Jul 2005 (MDT)

Newbie Question

Hello Guys, i was looking for an even like onKeyPress(PLayer, Key ,etc....) is there is anything like that I want to create a mod that when a player press a button a chat msg is sent to all players. thanks


The BF2 core game engine only passes "high level" game events to Python scripts; keypresses are considered "low level", and so no, there is no built-in "onKeyPress" event that you can register to receive. This is probably a good thing, though: there can be a lot of keyboard and mouse events during game play, and if these were all activating Python scripts, the game could pretty quickly become unplayable for performance reasons.

If you're really desperate, though, you can probably create such an event yourself: one of the things that makes the admin script (Battlefield 2 Server/admin/default.py, unless you change it to something else) special is that the game engine will automatically call the admin module's update method every 30 milliseconds so the admin script can check for RCon input. You could add your own keyboard polling code to the update method so that after it checks for RCon input, you can check for keypresses, and then take action appropriately.

Be very careful, though: any code inserted this way will be run around 30 times per second, so if it takes very long to do it's thing, you'll screw up game play. Also be careful how you check for keypresses: you need to be sure to do this in a non-blocking manner, or you'll bring the game to a complete halt! You might want to check out the msvcrt module in the Python Standard Library; it has a kbhit() function that does a non-blocking check for keypresses.

--Woody 09:01, 11 May 2006 (MDT)

RoundStart Events

Has anyone managed to use the RoundStart/RoundEnd events? I cant figure out what the parameter arguments for the event handler should be. Anyone had any success with these??? --Andrewd 05:09, 23 October 2006 (MDT)


Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox