[Help] Newbie absolutely stumped, sending a message when train arrives.
#1

Hey everyone.

I've been trying to do this for a couple of hours, I always try everything I can think of before asking for help - until I get a headache.

Usually the answer is really simple and I'll slap myself for not being able to see it.

I have created an NPC that drives a train around various locations, which is working great.

However I want to make it so that when the train/NPC arrives at certain points a message is sent.

I added this into the script, no errors or warnings but it doesnt work:

Код:
     
new Float:vehx, Float:vehy, Float:vehz;
          GetVehiclePos(Train, vehx, vehy, vehz);
          if (vehx == 1778.8184 && vehy == -1953.8077 && vehz == 14.8756)
		   {
		    SendClientMessageToAll(COLOR_RED, "The train has arrived at Unity Station");
		    }
It's either completely wrong, or in the wrong place in the script. I've used the search and found someone else asking a similar question, however a post or two later they said "SOLVED" but I saw no explanation.

Help is appreciated, thanks in advance!

Sorry if it's a really dumb question - I wouldn't ask if I hadn't tried every idea my little brain is capable of concocting.
Reply
#2

Where are you calling that code?
Reply
#3

Quote:
Originally Posted by Bicentric
Посмотреть сообщение
Where are you calling that code?
I had it in OnGameModeInit - pretty sure its the wrong place, I just cant think where it should go.
Reply
#4

pawn Код:
public OnGameModeInit()
{
    SetTimer("TrainCheck",1000,1);
    return 1;
}

forward TrainCheck();

public TrainCheck()
{
    new Float:vehx, Float:vehy, Float:vehz;
    GetVehiclePos(Train, vehx, vehy, vehz);
    if (vehx == 1778.8184 && vehy == -1953.8077 && vehz == 14.8756)
    {
        SendClientMessageToAll(COLOR_RED, "The train has arrived at Unity Station");
    }
    return 1;
}
Reply
#5

You can't compare floats like that due to the way they are saved. Sometimes the position may be 1778.8185, rendering your check totally useless. You could probably use GetVehicleDistanceFromPoint.
Reply
#6

Quote:
Originally Posted by tyler12
Посмотреть сообщение
pawn Код:
public OnGameModeInit()
{
    SetTimer("TrainCheck",1000,1);
    return 1;
}

forward TrainCheck();

public TrainCheck()
{
    new Float:vehx, Float:vehy, Float:vehz;
    GetVehiclePos(Train, vehx, vehy, vehz);
    if (vehx == 1778.8184 && vehy == -1953.8077 && vehz == 14.8756)
    {
        SendClientMessageToAll(COLOR_RED, "The train has arrived at Unity Station");
    }
    return 1;
}
Thankyou. I thought about the use of a timer, because otherwise it wouldnt know when to check if the train is there..

Quote:
Originally Posted by Vince
Посмотреть сообщение
You can't compare floats like that due to the way they are saved. Sometimes the position may be 1778.8185, rendering your check totally useless. You could probably use GetVehicleDistanceFromPoint.
Hmmm, I understand - I'll try the method posted first and if I have trouble still I'll try that instead.

EDIT: Yeah I think you are right Vince, as still no message is being shown, and I've put all the code in the right place as i followed the structure of another timer that I know works. I'll try using GetVehicleDistanceFromPoint instead.
Reply
#7

Thankyou to both of you, I have it working.

Quote:

public TrainCheck()
{
new Float:fDistance = GetVehicleDistanceFromPoint(MyFirstNPCVehicle, 1778.8184, -1953.8077, 14.8756);
if(fDistance <= 10)
{
SendClientMessageToAll(COLOR_RED, "The train has arrived at Unity Station");
}
return 1;
}

Now I just need to make the timer longer than 1 second because it spams the chat the whole time the train is there. xD

+Repped
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)