Help with a bug
#1

Hi, first of all I must say that I'm a total newbie at scripting. I had never scripted in Pawn, and my only programming experience consists of some .bat files (for example I have one that, when double clicked, launches Samp, Xfire and Fraps in a row), and some spare and easy programs that I wrote with Visual Basic almost 14 years ago, when I was still using Windows 95 (although I still remember concepts like variables, booleans and control structures like if, elseif and goto).

Well, let's go ontopic: I have tried to make a script that, if you are driving a vehicle and its vehicle health goes below 240, it sets its health back to 240 (so it will be still on fire but won't explode instantly), and then ejects you from the vehicle. The code I have written is the following:

Код:
public OnVehicleDamageStatusUpdate(vehicleid, playerid)
{
    new veh, Float:health;
    veh = GetPlayerVehicleID(playerid);
    GetVehicleHealth(veh, health);
    if(health <= 240.0)
    {
        SetVehicleHealth(veh, 240.0);
	new Float:x, Float:y, Float:z;
	GetPlayerPos(playerid, x, y, z);
        SetPlayerPos(playerid, x, y, z+1);
	return 1;
     }
     return 1;
}
I have used SetPlayerPos instead of RemovePlayerFromVehicle because SetPlayerPos ejects you from the vehicle instantly, without the exiting animation.
However my script is buggy and, as you can see in the following video, it ejects me also when getting in a new vehicle. Besides, there are a few times in which it fails at setting the vehicle health to 240 and ejecting me when the vehicle health goes below that value, as you can see at 2:13 and 3:33:

[ame]http://www.youtube.com/watch?v=SFiQdzWLY_4[/ame]

I have compiled it with Pawno.
Where is the problem and how to fix it?
Thanks.
Reply
#2

Try this [Untested]
pawn Код:
public OnVehicleDamageStatusUpdate(vehicleid, playerid)
{
    new veh, Float:health;
    veh = GetPlayerVehicleID(playerid);
    GetVehicleHealth(veh, health);
    if(health <= 240.0)
    {
    new Float:x, Float:y, Float:z;
        SetVehicleHealth(veh, 242.0); // You dont want the car to explode right?
    GetPlayerPos(playerid, x, y, z);
        SetPlayerPos(playerid, x, y, z+1);
    return 1;
     }
     return 1;
}
Or this one if the above one doesnt work for you everytime[Untested]
This one has a double check for veh health in it.
pawn Код:
public OnVehicleDamageStatusUpdate(vehicleid, playerid)
{
    new veh, Float:health;
    veh = GetPlayerVehicleID(playerid);
    GetVehicleHealth(veh, health);
    if(health <= 240.0)
    {
        new Float:x, Float:y, Float:z;
        SetVehicleHealth(veh, 242.0); // You dont want the car to explode right?
        GetPlayerPos(playerid, x, y, z);
        SetPlayerPos(playerid, x, y, z+1);
        if(health <= 240.0) // Double checking again! If you dont want the car to explode
        {
            SetVehicleHealth(veh, 242.0); // You dont want the car to explode right?
        }
        return 1;
     }
     return 1;
}
Reply
#3

Thanks for the reply, I will try your solutions and tell if they work.
Your first code seems to be similar to mine, but with the difference of setting the vehicle health to 242 instead of to 240, right?
Reply
#4

OnvehicleDamageStatusUpdate is called when visual damage is done to the vehicle, not when vehicle loses health.
Reply
#5

Quote:
Originally Posted by Ballu Miaa
Посмотреть сообщение
Try this [Untested]
pawn Код:
public OnVehicleDamageStatusUpdate(vehicleid, playerid)
{
    new veh, Float:health;
    veh = GetPlayerVehicleID(playerid);
    GetVehicleHealth(veh, health);
    if(health <= 240.0)
    {
    new Float:x, Float:y, Float:z;
        SetVehicleHealth(veh, 242.0); // You dont want the car to explode right?
    GetPlayerPos(playerid, x, y, z);
        SetPlayerPos(playerid, x, y, z+1);
    return 1;
     }
     return 1;
}
Or this one if the above one doesnt work for you everytime[Untested]
This one has a double check for veh health in it.
pawn Код:
public OnVehicleDamageStatusUpdate(vehicleid, playerid)
{
    new veh, Float:health;
    veh = GetPlayerVehicleID(playerid);
    GetVehicleHealth(veh, health);
    if(health <= 240.0)
    {
        new Float:x, Float:y, Float:z;
        SetVehicleHealth(veh, 242.0); // You dont want the car to explode right?
        GetPlayerPos(playerid, x, y, z);
        SetPlayerPos(playerid, x, y, z+1);
        if(health <= 240.0) // Double checking again! If you dont want the car to explode
        {
            SetVehicleHealth(veh, 242.0); // You dont want the car to explode right?
        }
        return 1;
     }
     return 1;
}
Sorry, but I have tried both and I still get the same bugs.

Quote:
Originally Posted by YJIET
Посмотреть сообщение
OnvehicleDamageStatusUpdate is called when visual damage is done to the vehicle, not when vehicle loses health.
Hmm you are right, and that may explain why a few times I haven't been ejected from the vehicle when its health was below 240. However, even if this callback is called only when visual damage is done, this still doesn't explain why I keep being ejected when getting in a new vehicle, knowing that my code has a conditional that executes the ejection ONLY IF the vehicle health is equal or lower than 240, and a new vehicle has 1k. Might this be a bug in the Pawno compiler itself? should I try with another compiler?
Reply
#6

Quote:
Originally Posted by Gryphus One
Посмотреть сообщение
Sorry, but I have tried both and I still get the same bugs.



Hmm you are right, and that may explain why a few times I haven't been ejected from the vehicle when its health was below 240. However, even if this callback is called only when visual damage is done, this still doesn't explain why I keep being ejected when getting in a new vehicle, knowing that my code has a conditional that executes the ejection ONLY IF the vehicle health is equal or lower than 240, and a new vehicle has 1k. Might this be a bug in the Pawno compiler itself? should I try with another compiler?
No changing the Compiler wont help. You need to change your callback in which your using the function.

Hope someone else has a solution to this.
Reply
#7

I'm starting to think that the bug has to do with SetPlayerPos, because yesterday I also tried to make another script for random spawning, using SetPlayerPos inside OnPlayerSpawn and I was always spawning at the coordinates set in AddPlayerClass inside OnGameModeInit.
Reply
#8

Quote:
Originally Posted by Gryphus One
Посмотреть сообщение
I'm starting to think that the bug has to do with SetPlayerPos, because yesterday I also tried to make another script for random spawning, using SetPlayerPos inside OnPlayerSpawn and I was always spawning at the coordinates set in AddPlayerClass inside OnGameModeInit.
AddPlayerClass has that spawn function which lets you spawn the player acc to the skin.

So copy the coordinates from your OnPlayerSpawn and edit the AddPlayerClass's positions. Will work!
Reply
#9

Simple fix:

This does not include vehicle health changes. Source: https://sampwiki.blast.hk/wiki/OnVehicleDamageStatusUpdate

You should make a timer for this.
Reply
#10

Thanks to the advice of YJIET, I have fixed the bug. YJIET told me that when I destroy a vehicle and it respawns, in the moment of getting in the newly respawned vehicle its health is still that of the previously destroyed one (even though the /dl command shows a vehicle health of 1000), and that health isn't updated to 1000 until some miliseconds after I get into the vehicle. Those miliseconds are enough for OnVehicleDamageStatusUpdate to be called, and that's why my ejection code is executed. So I have fixed it by adding a custom function, a global boolean variable and a timer. Here is it:

Код:
forward DriverVariable();
new bool:IsPlayerInVeh; // Boolean variable to tell if player is in a vehicle

public OnPlayerSpawn(playerid)
{
    IsPlayerInVeh = false;
    return 1;
}

public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    SetTimer("DriverVariable", 5000, false);
    return 1;
}

public DriverVariable()
{
    IsPlayerInVeh = true; // 5 seconds after entering a vehicle, the boolean variable is set to true
    return 1;
}

public OnPlayerExitVehicle(playerid, vehicleid)
{
    IsPlayerInVeh = false;
    return 1;
}

public OnVehicleDamageStatusUpdate(vehicleid, playerid)
{
    if (IsPlayerInVeh == true)
	{
	new veh, Float:health;
    	veh = GetPlayerVehicleID(playerid);
    	GetVehicleHealth(veh, health);
	if (health <= 240.0)
    	{
            SetVehicleHealth(veh, 240.0);
	    new Float:x, Float:y, Float:z;
	    GetPlayerPos(playerid, x, y, z);
            SetPlayerPos(playerid, x, y, z+1);
            IsPlayerInVeh = false; // After being ejected, the boolean is set to false
            return 1;
	}
	return 1;
    }
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)