SA-MP Forums Archive
IF get vehicle speed? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: IF get vehicle speed? (/showthread.php?tid=665199)



IF get vehicle speed? - Lixyde - 25.03.2019

So i when you pass 210 KM in your car it should send the message that you are driving too fast.


I tried it, but something doesnt work.


Code:
stock GetVehicleSpeed(vehicleid)
{
	    new Float:vx, Float:vy, Float:vz, Float:vel;
        vel = GetVehicleVelocity(vehicleid, vx, vy, vz);
        vel = (floatsqroot(((vx*vx)+(vy*vy))+(vz*vz))* 181.5);
        return Float:vel;
}

public OnFilterScriptInit()
{
	print("\n--------------------------------------");
	print(" Blank Filterscript by your name here");
	print("--------------------------------------\n");
	return 1;
}

public OnFilterScriptExit()
{
	return 1;
}

public VehicleSpeed(playerid)
{
	if(IsPlayerInAnyVehicle(playerid))
	{
		 if(GetVehicleSpeed(GetPlayerVehicleID(playerid)) >= 210)
		 {
			  SendClientMessage(playerid, COLOR_RED, "You drive too fast, cops are going to chase you now!");
			  IsWanted[playerid] = 1;
		 }
	}
	return 1;
}
1. I have argument mismatch on return Float:vel; i changed it to return vel; again..

2. It doesnt work at all, im passing 210 KMH and more, but there is no message. help?


Re: IF get vehicle speed? - Heress - 25.03.2019

https://sampforum.blast.hk/showthread.php?tid=364124

Tag mismatch because in if you write 210, change to 210.0


Re: IF get vehicle speed? - 4D1L - 25.03.2019

Quote:
Originally Posted by Lixyde
View Post
So i when you pass 210 KM in your car it should send the message that you are driving too fast.


I tried it, but something doesnt work.


Code:
stock GetVehicleSpeed(vehicleid)
{
	    new Float:vx, Float:vy, Float:vz, Float:vel;
        vel = GetVehicleVelocity(vehicleid, vx, vy, vz);
        vel = (floatsqroot(((vx*vx)+(vy*vy))+(vz*vz))* 181.5);
        return Float:vel;
}

public OnFilterScriptInit()
{
	print("\n--------------------------------------");
	print(" Blank Filterscript by your name here");
	print("--------------------------------------\n");
	return 1;
}

public OnFilterScriptExit()
{
	return 1;
}

public VehicleSpeed(playerid)
{
	if(IsPlayerInAnyVehicle(playerid))
	{
		 if(GetVehicleSpeed(GetPlayerVehicleID(playerid)) >= 210)
		 {
			  SendClientMessage(playerid, COLOR_RED, "You drive too fast, cops are going to chase you now!");
			  IsWanted[playerid] = 1;
		 }
	}
	return 1;
}
1. I have argument mismatch on return Float:vel; i changed it to return vel; again..

2. It doesnt work at all, im passing 210 KMH and more, but there is no message. help?
I would not advise to using a stock function as it hides errors.

I would change your function signature to this: Float:GetVehicleSpeed(vehicleid). By adding a float tag to the function header, you are telling the compiler you are going to be returning a float.

As such, the line `return Float:vel` would become `return vel` as the function is expected to return a floating point number because of how we used the tag Float.


Re: IF get vehicle speed? - SymonClash - 25.03.2019

Your code will NEVER work because you're not calling ANYWHERE the VehicleSpeed function. It's just static, yes you added a message, yes you added the km/h check, but when and how VehicleSpeed gets called?

You have to call it somewhere to be sure the player will get the message. Place it in a timer maybe? (By checking if the player is in the vehicle and in the driver seat obv). Otherwise yours is just a "static" function, stays there just to increase lines in your code, nothing more.

And i suggest you to use this:

pawn Code:
GetVehicleSpeed(vehicleid, type)
{
    //Type 1: MPH - Any other type: KM/H
    //GetVehicleSpeed(GetPlayerVehicleID(playerid), 1);
   
    new s;
    new Float:x, Float:y, Float:z;
    GetVehicleVelocity(vehicleid, x, y, z);
    switch(type)
    {
        case 1: s = floatround((floatsqroot(floatpower(x, 2) + floatpower(y, 2) + floatpower(z, 2)))*112.1577, floatround_round);
        default: s = floatround((floatsqroot(floatpower(x, 2) + floatpower(y, 2) + floatpower(z, 2)))*180.5000, floatround_round);
    }
    return s;
}



Re: IF get vehicle speed? - Pottus - 25.03.2019

Why not do it like this.

Code:
static bool:IsSpeeding[MAX_PLAYERS];

public OnPlayerUpdate(playerid)
{
	// Get the seat this determines everything about what the player is doing with
	// the vehicle and if they are even in one
	new seat = GetPlayerVehicleSeat(playerid);

	// Is the player speeding?
	if(IsSpeeding[playerid])
		// Can't be speeding if not in a vehicle
		if(seat == -1)
			IsSpeeding[playerid] = false;
		// Player is driving
		else if(seat == 0)
		{
			// Have they stopped speeding yet?
		    if(GetVehicleSpeed(GetPlayerVehicleID(playerid)) <= 210)
		    {
				// Not speeding anymore let them know
		        IsSpeeding[playerid] = false;
		        SendClientMessage(playerid, COLOR_GREEN, "You have stopped speeding watch out for the cops");
			}
		}
	}
	// Player is not speeding
	else
	{
		// Player is driving
		if(seat == 0)
		{
			// Player is speeding issue the warning set as wanted
			if(GetVehicleSpeed(GetPlayerVehicleID(playerid)) >= 210)
			{
				SendClientMessage(playerid, COLOR_RED, "You are drivin too fast, thw cops are going to chase you now!");
				IsWanted[playerid] = 1;
			}
		}
	}
	return 1;
}



Re: IF get vehicle speed? - Lixyde - 26.03.2019

So i added what SymonClash said.
and added what Pottus


It works. But not everything.

The full code:

Code:
new IsWanted[MAX_PLAYERS];
static bool:IsSpeeding[MAX_PLAYERS];

GetVehicleSpeed(vehicleid, type)
{
    //Type 1: MPH - Any other type: KM/H
    //GetVehicleSpeed(GetPlayerVehicleID(playerid), 1);

    new s;
    new Float:x, Float:y, Float:z;
    GetVehicleVelocity(vehicleid, x, y, z);
    switch(type)
    {
        case 1: s = floatround((floatsqroot(floatpower(x, 2) + floatpower(y, 2) + floatpower(z, 2)))*112.1577, floatround_round);
        default: s = floatround((floatsqroot(floatpower(x, 2) + floatpower(y, 2) + floatpower(z, 2)))*180.5000, floatround_round);
    }
    return s;
}

public OnPlayerUpdate(playerid)
{
    new seat = GetPlayerVehicleSeat(playerid);
    if(IsSpeeding[playerid])
    {
	    if(seat == -1)
	    {
		    IsSpeeding[playerid] = false;
	    }
	    if(seat == 0)
	    {
		    if(GetVehicleSpeed(GetPlayerVehicleID(playerid)) <= 210)
		    {
			    IsSpeeding[playerid] = false;
			    SendClientMessage(playerid, COLOR_RED, "You stopped driving fast, watch out for the cops");
		    }
	    }
    }
    else
    {
        if(seat == 0)
		{
			if(GetVehicleSpeed(GetPlayerVehicleID(playerid)) >= 210)
			{
				SendClientMessage(playerid, COLOR_RED, "You are drivin too fast, thw cops are going to chase you now!");
				IsWanted[playerid] = 1;
			}
		}
    }
    return 1;
}

1. I have argument mismatch on:

Code:
if(GetVehicleSpeed(GetPlayerVehicleID(playerid)) <= 210)
and on:

Code:
if(GetVehicleSpeed(GetPlayerVehicleID(playerid)) >= 210)
I tried changing 210 with 210.0 and again its the same.



2. When i decrease my vehicle speed and gets below 210. it should send the message:

Code:
SendClientMessage(playerid, COLOR_RED, "You stopped driving fast, watch out for the cops");
But it doesnt work?. There is no message send


Re: IF get vehicle speed? - raydx - 26.03.2019

Tag mismatch can be related to missing second argument (type) - GetVehicleSpeed(vehicleid, type).

Do it like that:

if(GetVehicleSpeed(GetPlayerVehicleID(playerid), 2) <= 210)


Re: IF get vehicle speed? - zepfiz - 27.03.2019

I got:

number of arguments does not match definition.

On:

line 51: if(GetVehicleSpeed(GetPlayerVehicleID(playerid, 2)) <= 210)
line 62: if(GetVehicleSpeed(GetPlayerVehicleID(playerid, 2)) >= 210)

If i remove the problem. then i have tag mismatch on them.


Re: IF get vehicle speed? - Lokii - 27.03.2019

PHP Code:
if(GetVehicleSpeed(GetPlayerVehicleID(playerid), 2) <= 210)
if(
GetVehicleSpeed(GetPlayerVehicleID(playerid), 2) >= 210