1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. GTA Network forum is now in archive mode.

API.drawText - MPH Speed and Vehicle Health Percentage

Discussion in 'Snippets' started by Ariana, Jan 18, 2017.

  1. Ariana

    Ariana Member

    Messages:
    33
    Likes Received:
    18
    Joined:
    Dec 26, 2016
    this simple clientside resource displays the current MPH and health of a vehicle. It also includes colours for the vehicle health based on the percentage

    This does not use CEF.

    [​IMG]
    [​IMG] [​IMG]

    vehstats.js

    Code:
    var res_X = API.getScreenResolutionMantainRatio().Width;
    var res_Y = API.getScreenResolutionMantainRatio().Height;
    
    API.onUpdate.connect(function () {
        var player = API.getLocalPlayer();
        var inVeh = API.isPlayerInAnyVehicle(player);
    
            if (inVeh) {
                var veh = API.getPlayerVehicle(player);
                var vel = API.getEntityVelocity(veh);
                var health = API.getVehicleHealth(veh);
                var maxhealth = API.returnNative("GET_ENTITY_MAX_HEALTH", 0, veh);
                var healthpercent = Math.floor((health / maxhealth) * 100);
                var speed = Math.sqrt(
                    vel.X * vel.X +
                    vel.Y * vel.Y +
                    vel.Z * vel.Z
                    );
                var displaySpeedMph = Math.round(speed * 2.23693629);
                API.drawText(`${displaySpeedMph}`, res_X - 60, res_Y - 200, 1, 255, 255, 255, 255, 4, 2, false, true, 0);
                API.drawText(`mph`, res_X - 20, res_Y - 180, 0.5, 255, 255, 255, 255, 4, 2, false, true, 0);
                API.drawText(`Health:`, res_X - 70, res_Y - 225, 0.5, 255, 255, 255, 255, 4, 2, false, true, 0);
                API.drawText(`${healthpercent}%`, res_X - 20, res_Y - 225, 0.5, 255, 255, 255, 255, 4, 2, false, true, 0);
                if (healthpercent < 60)
                {
                    API.drawText(`${healthpercent}%`, res_X - 20, res_Y - 225, 0.5, 219, 122, 46, 255, 4, 2, false, true, 0);
                }
                if (healthpercent < 30)
                {
                    API.drawText(`${healthpercent}%`, res_X - 20, res_Y - 225, 0.5, 219, 46, 46, 255, 4, 2, false, true, 0);
                }
            }
    });
     
    Zellcore, WhiteFoxMax, GroX0r and 5 others like this.
  2. zynxcom

    zynxcom New Member

    Messages:
    3
    Likes Received:
    1
    Joined:
    Jan 13, 2017
    thank you!! Very useful!
     
    Ariana likes this.
  3. xForcer

    xForcer Member

    Messages:
    18
    Likes Received:
    5
    Joined:
    Jan 4, 2017
    Question - How do you get the exact coordinate you want with
    API.getScreenResolutionMantainRatio()

    I see everyone uses "- 60" and similiar.
     
  4. 1B0Y

    1B0Y Member

    Messages:
    35
    Likes Received:
    10
    Joined:
    Dec 23, 2016
    API.getScreenResolutionMaintainRatio will just return the maximum width/height of the screen. Using -60 will move the UI to the left, as it'll be rendered off-screen if it wasn't. Apart from that, just plays with maths lol. Hope that helps Forcer ;-)
     
    Ariana likes this.
  5. M3L7Z

    M3L7Z Active Member

    Messages:
    66
    Likes Received:
    13
    Joined:
    Jan 7, 2017
    If u want to use Kmph:
    PHP:
    var res_X API.getScreenResolutionMantainRatio().Width;
    var 
    res_Y API.getScreenResolutionMantainRatio().Height;

    API.onUpdate.connect(function () {
        var 
    player API.getLocalPlayer();
        var 
    inVeh API.isPlayerInAnyVehicle(player);

            if (
    inVeh) {
                var 
    veh API.getPlayerVehicle(player);
                var 
    vel API.getEntityVelocity(veh);
                var 
    health API.getVehicleHealth(veh);
                var 
    maxhealth API.returnNative("GET_ENTITY_MAX_HEALTH"0veh);
                var 
    healthpercent Math.floor((health maxhealth) * 100);
                var 
    speed Math.sqrt(
                    
    vel.vel.+
                    
    vel.vel.+
                    
    vel.vel.Z
                    
    );
                var 
    displaySpeedkmph Math.round(speed 3.6);
                
    API.drawText(`${displaySpeedkmph}`, res_X 100res_Y 200125525525525542falsetrue0);
                
    API.drawText(`Kmph`, res_X 20res_Y 1800.525525525525542falsetrue0);
                
    API.drawText(`Health:`, res_X 70res_Y 2250.525525525525542falsetrue0);
                
    API.drawText(`${healthpercent}%`, res_X 20res_Y 2250.525525525525542falsetrue0);
                if (
    healthpercent 60)
                {
                    
    API.drawText(`${healthpercent}%`, res_X 20res_Y 2250.52191224625542falsetrue0);
                }
                if (
    healthpercent 30)
                {
                    
    API.drawText(`${healthpercent}%`, res_X 20res_Y 2250.5219464625542falsetrue0);
                }
            }
    });
     
    Last edited: Feb 2, 2017
    Ariana likes this.
  6. Ariana

    Ariana Member

    Messages:
    33
    Likes Received:
    18
    Joined:
    Dec 26, 2016
    yup just assumed MPH has best since LS in the US :=P
     
    M3L7Z likes this.
  7. xForcer

    xForcer Member

    Messages:
    18
    Likes Received:
    5
    Joined:
    Jan 4, 2017
    Thanks Ryder!
     
  8. Jorge Santamaria

    Jorge Santamaria Member

    Messages:
    38
    Likes Received:
    7
    Joined:
    Dec 8, 2016
    This is very good, I liked it. But it seems that the life of the vehicle only works with impacts of weapons, when the vehicle crashes against objects does not lose life.
     
  9. Ariana

    Ariana Member

    Messages:
    33
    Likes Received:
    18
    Joined:
    Dec 26, 2016
    I tested it by crashing into walls and it worked fine?
     
  10. WhiteFoxMax

    WhiteFoxMax Member

    Messages:
    9
    Likes Received:
    3
    Joined:
    Jan 28, 2017
    Hi,
    The Kmph was overlaying with the speed itself(On my screen) so I added 40 to the res_x on it:
    Code:
    API.drawText(`${displaySpeedkmph}`, res_X - 100, res_Y - 200, 1, 255, 255, 255, 255, 4, 2, false, true, 0);
    @Ariana - Thanks a lot! saved us some time :=)
     
    M3L7Z likes this.
  11. M3L7Z

    M3L7Z Active Member

    Messages:
    66
    Likes Received:
    13
    Joined:
    Jan 7, 2017
    @WhiteFoxMax .. yeah thx, i have forgotten to change it here
     
  12. mondmann

    mondmann Member

    Messages:
    8
    Likes Received:
    0
    Joined:
    Mar 21, 2017
    why do you multiply with 2.236.. for mph and with 3.6 for kmph?
     
  13. Davidowiz

    Davidowiz Member

    Messages:
    6
    Likes Received:
    0
    Joined:
    Apr 20, 2017
    Looks nice and is working well :=)
     
    Last edited: Apr 25, 2017
  14. MrPancakers

    MrPancakers Active Member

    Messages:
    90
    Likes Received:
    19
    Joined:
    Aug 2, 2016
    Because MPH and KMPH isn't the same thing, going 10 MPH isn't the same as 10 KMPH so they need different variables.
     
  15. mondmann

    mondmann Member

    Messages:
    8
    Likes Received:
    0
    Joined:
    Mar 21, 2017
    well, i mean that is obviously, but why 2.236 and 3.6 and not 5 and 10 ?
     
  16. Scrome

    Scrome Member

    Messages:
    19
    Likes Received:
    3
    Joined:
    Jan 23, 2017
    You are calculating the speed in meters per seconds. If you want to "transfer" this unit into miles per hour or kilometres per hour you need different factors. These factors are just given by the numbers 2.236 for m/s to mph and 3.6 for m/s to kmph.
     
  17. mondmann

    mondmann Member

    Messages:
    8
    Likes Received:
    0
    Joined:
    Mar 21, 2017
    That clears it up, thank you!
     

Share This Page