Wiki

Clone wiki

EFLC-Multiplayer / Timers and Loops

#!c++

#include <mainNetwork.h>
#include "apiVehicle.h"
#include "apiPlayer.h"
#include <time.h>

const int max_players = 16;

void timedFunction()
{
    //Lets make a simple speedometer
    apiMath::Vector3 velocity;
    int currentVehicle = NULL;
    float speed = 0.0f;
    char speedoMsg[15] = "";

    //Start looping the players
    for(unsigned int i = 1; i <= max_players; i++)
    {
        if(apiPlayer::isOn(i))
        {
            currentVehicle = apiPlayer::get(i).getDriving();
            if(currentVehicle != NULL && apiVehicle::isVehicle(currentVehicle))
            {
                velocity = apiVehicle::getVehicle(currentVehicle).getVelocity();
            //Converts the Velocity vector to Speed (miles per hour)
                speed = std::sqrtf(velocity.x * velocity.x + velocity.y * velocity.y + velocity.z * velocity.z) * 1.609f;

                sprintf_s(speedoMsg, "Speed: %i", (int)speed);
                apiPlayer::get(i).drawInfoText(speedoMsg, 600);
            }
        }
    }
}

int main()
{
   //Creates the IVMP network logic
   if(initRaknet(8888, "Server Name", "Server location", "Server WebSite", false))
   {        
        clock_t nextCall = 500; //Defines an uptime to trigger the SpeedoMeter

        while(true)
        {
           pulseServer();

           clock_t currentTime = clock(); //Clock() returns the server uptime in MS
           if(currentTime > nextCall)
           {
               timedFunction();
               nextCall = currentTime + 500; //Resets the timer
           }           

           Sleep(100);
        }
   }
   return 1;
}

Updated