need help with OnPlayerUpdate -
danielpalade - 24.04.2015
Well I'm working on my Trucker Job but I need to make it so when a player attaches a trailer, it sends him a message as a test. I made that part successfully. But then I had to make it so when a player unattaches his trailer he gets another test message. That part was successfully made too. But how do I make it so when a player attaches a trailer again, the function gets reset and starts again? Becouse now whenever I deattach my trailer and attach it again, it won't send me the test message.
This is my code.
Код:
public OnPlayerUpdate(playerid)
{
new vehicleid = GetPlayerVehicleID(playerid);
if(IsTrailerAttachedToVehicle(vehicleid) && AttachedTrailer1[playerid])
{
SendClientMessage(playerid, COLOR_MAXZONE, "Test");
AttachedTrailer1[playerid] = 0;
TrailerIsAttached[playerid] = 0;
}
else if (AttachedTrailer1[playerid]) AttachedTrailer1[playerid] = -1;
if(TrailerIsNotAttached[playerid] && !IsTrailerAttachedToVehicle(vehicleid) && IsPlayerInAnyVehicle(playerid) && TrailerIsAttached[playerid] == 0)
{
SendClientMessage(playerid, COLOR_MAXZONE, "Nu mai esti test.");
TrailerIsNotAttached[playerid] = 0;
TrailerIsAttached[playerid] = -1;
}
else if(TrailerIsNotAttached[playerid]) TrailerIsNotAttached[playerid] = -1;
return 1;
}
Re: need help with OnPlayerUpdate -
Abagail - 24.04.2015
Something such as this should suffice, perhaps?
pawn Код:
#include <a_samp>
#include <foreach>
#define TRAILER_STATE_DETACHED 0
#define TRAILER_STATE_ATTACHED 1
new VehicleUpdateTimer;
new Attached[MAX_VEHICLES];
public OnFilterScriptInit()
{
VehicleUpdateTimer = SetTimer("OnVehicleUpdate", 1000, true);
return 1;
}
public OnFilterScriptExit() return KillTimer(VehicleUpdateTimer);
forward OnTrailerStateChange(trailerid, newstate, oldstate, ext=0, playerid=INVALID_PLAYER_ID);
forward VehicleUpdateTimer();
public VehicleUpdateTimer() {
foreach(new i: Vehicle) {
if(IsTrailerAttachedToVehicle(i) && !Attached_[i])) {
Attached[i] = GetVehicleTrailer(i);
CallLocalFunction("OnTrailerStateChange", "dd", i, GetVehicleTrailer(i), TRAILER_STATE_ATTACHED, TRAILER_STATE_DETACHED, i, GetVehicleDriver(i));
}
else if(!IsTrailerAttachedToVehicle(i) && Attached_[i])) {
CallLocalFunction("OnTrailerStateChange", "dd", i, Attached[i], TRAILER_STATE_DETACHED, TRAILER_STATE_ATTACHED, i, GetVehicleDriver(i)));
Attached[i] = 0;
}
}
return 1;
}
static GetVehicleDriver(vehicleid) {
foreach(new i: Player)
{
if(IsPlayerInAnyVehicle(i) && GetPlayerVehicleID(i) == vehicleid && GetPlayerState(playerid) == PLAYER_STATE_DRIVER) {
return i;
}
}
return INVALID_PLAYER_ID;
}
Then you can do:
pawn Код:
public OnTrailerStateChange(vehicleid, newstate, oldstate, ext, playerid)
{
if(newstate == TRAILER_STATE_ATTACHED) {
SendClientMessage(playerid, COLOR_WHITE, "Test");
}
else
SendClientMessage(playerid, COLOR_WHITE, "Nu mai esti test.");
}
return 1;
}
You can also just use the callback to set the variable. If this works as it should, it'll be called when a trailer gets attached and detached from a vehicle(similar to other state change callbacks).
ext - The vehicle ID the trailer is interacting with
playerid - The (if applicable) player operating the vehicle interacting with the trailer(aka the ext parameter).
Though I haven't tested this; it should work.
Re: need help with OnPlayerUpdate -
park4bmx - 24.04.2015
considering the new update there is a much better way of doing this then checking it every update.
or even by a timer ! you should consider that.
pawn Код:
public OnPlayerUpdate(playerid)
{
new vehicleid = GetPlayerVehicleID(playerid);
TrailerIsAttached[playerid] = IsTrailerAttachedToVehicle(vehicleid)
if(IsTrailerAttachedToVehicle(vehicleid)==1)//trailer attached
{
SendClientMessage(playerid, COLOR_MAXZONE, "Test");
}else if(IsPlayerInAnyVehicle(playerid) && TrailerIsAttached[playerid] == 1) return SendClientMessage(playerid, COLOR_MAXZONE, "Nu mai esti test.");
return 1;
}