This wiki was in read-only mode for many years, but can now be edited again. A lot of information will need to be updated.

BZFS API examples: Difference between revisions

From BZFlagWiki
Jump to navigation Jump to search
Zehra (talk | contribs)
m added simple playerscorechange example
Zehra (talk | contribs)
added another example, tick event
Line 2: Line 2:


=Examples List=
=Examples List=
'''bz_ePlayerScoreChanged'''
* '''bz eTickEvent'''
* '''bz_ePlayerScoreChanged'''
 
==bz eTickEvent==
'''Note:''' This is commonly used for timers. Since the delay between intervals is unpredictable, [[bz_setMaxWaitTime]] is sometimes used to specify what is the max duration between intervals.
 
Starter code from official docs: [https://www.bzflag.org/documentation/developer/bzfs_api/events/ www.bzflag.org/documentation/developer/bzfs_api/events/bz_etickevent/]
bz_TickEventData_V1* dataObject = (bz_TickEventData_V1*)eventData;
// Data
// ---
// (double)      eventTime - Local Server time of the event (in seconds)
 
Here's an example of how to implement a simple timer system with it:
// This code is outside of the tick event.
double timer; // This is used when we trigger it, possibly via slash command or game start event.
int countdown = 5; // This gives us 5 seconds.
// This is the code used in the tick event.
if ((dataObject->eventTime - timer) > 1.0) { // We check if a second has passed since our last timer update.
  timer += 1.0;
  countdown -= 1;
  bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS, "%d", countdown);
  if (countdown <= 0) {
  // Do something here.
  }
}
 


==bz_ePlayerScoreChanged==
==bz_ePlayerScoreChanged==
Line 8: Line 36:


Starter code from official docs: [https://www.bzflag.org/documentation/developer/bzfs_api/events/bz_eplayerscorechanged/ www.bzflag.org/documentation/developer/bzfs_api/events/bz_eplayerscorechanged/]
Starter code from official docs: [https://www.bzflag.org/documentation/developer/bzfs_api/events/bz_eplayerscorechanged/ www.bzflag.org/documentation/developer/bzfs_api/events/bz_eplayerscorechanged/]
  bz_PlayerScoreChangeEventData_V1* dataObject = (bz_PlayerScoreChangeEventData_V1*)eventData;
  bz_PlayerScoreChangeEventData_V1* dataObject = (bz_PlayerScoreChangeEventData_V1*)eventData;
   
   
Line 17: Line 46:
  // (int)          lastValue - The old amount of element score the playerID had.
  // (int)          lastValue - The old amount of element score the playerID had.
  // (double)      eventTime - Time local server time for the event.
  // (double)      eventTime - Time local server time for the event.
 


Here's an example of how we can use the player score changed event to modify player scores and keep them tied to specific game play accomplishments.
Here's an example of how we can use the player score changed event to modify player scores and keep them tied to specific game play accomplishments.

Revision as of 01:02, 3 November 2025

Various examples of the BZFS API. This may be used a quick reference or guide for getting started and contains various tips or tricks for using it.

Examples List

  • bz eTickEvent
  • bz_ePlayerScoreChanged

bz eTickEvent

Note: This is commonly used for timers. Since the delay between intervals is unpredictable, bz_setMaxWaitTime is sometimes used to specify what is the max duration between intervals.

Starter code from official docs: www.bzflag.org/documentation/developer/bzfs_api/events/bz_etickevent/

bz_TickEventData_V1* dataObject = (bz_TickEventData_V1*)eventData;

// Data
// ---
// (double)       eventTime - Local Server time of the event (in seconds)

Here's an example of how to implement a simple timer system with it:

// This code is outside of the tick event.
double timer; // This is used when we trigger it, possibly via slash command or game start event.
int countdown = 5; // This gives us 5 seconds.

// This is the code used in the tick event.
if ((dataObject->eventTime - timer) > 1.0) { // We check if a second has passed since our last timer update.
 timer += 1.0;
 countdown -= 1;
 bz_sendTextMessagef(BZ_SERVER, BZ_ALLUSERS, "%d", countdown);
 if (countdown <= 0) {
  // Do something here.
 }
}


bz_ePlayerScoreChanged

Note: Using any of the player score modifying/changing functions without any conditionals will lead to infinite loops.

Starter code from official docs: www.bzflag.org/documentation/developer/bzfs_api/events/bz_eplayerscorechanged/

bz_PlayerScoreChangeEventData_V1* dataObject = (bz_PlayerScoreChangeEventData_V1*)eventData;

// Data
// ---
// (int)          playerID - Player that has had a change of score.
// (bz_eScoreElement) element - The type of score that is being changed.
// (int)          thisValue - The new amount of element score the playerID has.
// (int)          lastValue - The old amount of element score the playerID had.
// (double)       eventTime - Time local server time for the event.


Here's an example of how we can use the player score changed event to modify player scores and keep them tied to specific game play accomplishments.

// Example of keeping a score tied.
if (dataObject->element == bz_eWins) {
 if (WinsFrom[dataObject->playerID] != dataObject->thisValue) { // We lookup from our WinsFrom array,
  //...to see if the players wins matches our score system, if not, we adjust it.
  bz_setPlayerWins(dataObject->playerID, WinsFrom[dataObject->playerID]);// This keeps our score in sync, but it doesn't keep us in a loop.
 }
}