21.08.2017, 03:18
Have you ever always wondered on how do LSRP set their label and attach them to vehicles for a sort of period of time whenever a player attempts to steal a faction vehicle (not authorized)?
Well I have the answer for you, This tutorial will teach you how to do those (+ additional, You can also pop this label in specific coordinates if the label isn't meant to be attached to a vehicle)
I wanted to share this knowledge/code of mine, I will try explaining the codes the best way I can.
For those who have no idea of what I am talking about;
[Part One] Setting everything up
Before proceeding with the tutorial, I must remind you that we are using Incognito's Streamer Plugin for this script. Why? Well, I find using Streamer a lot easier rather than using the traditional old 3D Text Label, You can also do this on the normal 3D Text Label, however, I suggest that you should use the Streamer (as this is a lot easier).
You can download Incognito's Streamer by clicking me
[Part Two] Coding the function
Place this at the top of your script (below include, it can be anywhere BUT it should be placed before the ShowLabelToPlayer function otherwise you will get undefined errors after compiling the script)
LabelTimer will be used later to assign itself to a player timer while Label3D is used for 3D Text Labels.
Moving forward....
We will begin on creating our function by typing this at the bottom of our script (It can be placed anywhere HOWEVER it cannot be inside of callbacks/any stock-functions)
Let's break it down explaining each parameter placed over the function:
* playerid
The player who will be viewing the 3D Text Label
* text
The string, the context/message (text) of the label (E.G.: You cannot enter this vehicle)
* color
The color that will be set in the context/message (Use -1 for color white, you can also use 0xFFFFFFFF)
Click me for more information about colors.
* Float: x
The X offset for the label (Left or Right)
* Float: y
The Y offset for the label (North or South)
* Float: z
The Z offset for the label (Up and Down)
* time
Time set when the label will disappear/expire
* Float: distance
The maximum distance where players can view the label
* vehicle
This parameter can be optional and not included when you are using the function (set to INVALID_VEHICLE_ID by default), if the paramater is not INVALID_VEHICLE_ID it will be attached to a vehicle specified by the script (E.G.; vehicle = 1, it will attach to vehicleID #1)
[Part Three] Adding the codes inside the function
As you can see from above we are first checking if Label3D is already assigned to an existing 3D Text Label (IsValidDynamic3DTextLabel), If there is an existing label then we will have to delete it (DestroyDynamic3DTextLabel) and kill the timer assigned to LabelTimer (no matter if it's running or not).
Moving down, if there is no label assigned to Label3D then we can proceed on assigning it to a new label.
As you can see from the code we have skipped some paramaters on CreateDynamic3DTextLabel jumping straight away to attachedvehicle after the CreateDynamic3DTextLabel's parameter Float: drawdistance, leaving those skipped paramaters to use their own assigned default values. The label gets created on the player's virtual world & interior and it is only visible to them (therefore why it's playerid = playerid) no one else can see it.
While we are creating the Label after the function is called, we will also run a timer. This timer will serve a purpose when it gets called it will destroy the label and kill itself. We have named the timer TerminateLabel, we have set the interval to (time * 1000), Why (time * 1000) you ask? 1000 mileseconds is equivalent to 1 second and we do not use seconds when setting up timers but we use mileseconds instead. We are multiplying 1000 over the parameter time so the result will be on mileseconds (E.G. 5 * 1000 = 5000, 5000 is equal to 5 seconds) - We have also set the repeating parameter to false since we wouldn't be repeatedly using the timer for any other stuff but rather than use it on destroying a label which is a ONE TIME thing only.
Don't forget to add this inside the OnPlayerDisconnect callback
After adding those codes above you should be good.
Make sure to add return 1; before the enclosing parenthesis to avoid the "should return a value" warning.
[Part Four] Final Touch - Adding the Timer
We are now declaring a new public callback which is named TerminateLabel, the parameter is playerid (which is self-explanatory).
Inside the callback is where we placed the KillTimer, it kills the LabelTimer to prevent itself from running repeatedly/removes-deattach LabelTimer from being assigned to that timer. Below the KillTimer function is the DestroyDynamic3DTextLabel which is self-explanatory by its own name, destroying the Label3D text label after the timer has been called.
Over all this is what the function & the timer should look like after you have coded them
You can now use the function on popping it up/attaching it to restricted vehicles when player tries to enter them.
[SAMPLE] Script
Remainder - The x/y/z parameter depends if it gets attached to a vehicle or not, if its not attached to a vehicle. It will be created to that specific coordinate, if its attached then the X/Y/Z will be the position/location/coordinate-parts of the vehicle.
You are free to share/critcize your opinion about my tutorial, If you have seen any mistakes from my side on explaining a part of this tutorial just let me know down below the post.
Thank you & god bless you, Have a nice day everybody.
Well I have the answer for you, This tutorial will teach you how to do those (+ additional, You can also pop this label in specific coordinates if the label isn't meant to be attached to a vehicle)
I wanted to share this knowledge/code of mine, I will try explaining the codes the best way I can.
For those who have no idea of what I am talking about;
[Part One] Setting everything up
Before proceeding with the tutorial, I must remind you that we are using Incognito's Streamer Plugin for this script. Why? Well, I find using Streamer a lot easier rather than using the traditional old 3D Text Label, You can also do this on the normal 3D Text Label, however, I suggest that you should use the Streamer (as this is a lot easier).
You can download Incognito's Streamer by clicking me
[Part Two] Coding the function
Place this at the top of your script (below include, it can be anywhere BUT it should be placed before the ShowLabelToPlayer function otherwise you will get undefined errors after compiling the script)
PHP код:
new LabelTimer[MAX_PLAYERS];
new Text3D:Label3D[MAX_PLAYERS];
Moving forward....
We will begin on creating our function by typing this at the bottom of our script (It can be placed anywhere HOWEVER it cannot be inside of callbacks/any stock-functions)
PHP код:
stock ShowLabelToPlayer(playerid, text[], color, Float:x, Float:y, Float:z, time, Float:distance, vehicle = INVALID_VEHICLE_ID)
{
return 1;
}
* playerid
The player who will be viewing the 3D Text Label
* text
The string, the context/message (text) of the label (E.G.: You cannot enter this vehicle)
* color
The color that will be set in the context/message (Use -1 for color white, you can also use 0xFFFFFFFF)
Click me for more information about colors.
* Float: x
The X offset for the label (Left or Right)
* Float: y
The Y offset for the label (North or South)
* Float: z
The Z offset for the label (Up and Down)
* time
Time set when the label will disappear/expire
* Float: distance
The maximum distance where players can view the label
* vehicle
This parameter can be optional and not included when you are using the function (set to INVALID_VEHICLE_ID by default), if the paramater is not INVALID_VEHICLE_ID it will be attached to a vehicle specified by the script (E.G.; vehicle = 1, it will attach to vehicleID #1)
[Part Three] Adding the codes inside the function
PHP код:
if(IsValidDynamic3DTextLabel(Label3D[playerid]))
{
KillTimer(LabelTimer[playerid]);
DestroyDynamic3DTextLabel(Label3D[playerid]);
}
Label3D[playerid] = CreateDynamic3DTextLabel(text, color, x, y, z, distance, .attachedvehicle = vehicle, .worldid = GetPlayerVirtualWorld(playerid), .interiorid = GetPlayerInterior(playerid), .playerid = playerid);
LabelTimer[playerid] = SetTimerEx("TerminateLabel", (1000 * time), false, "d", playerid);
Moving down, if there is no label assigned to Label3D then we can proceed on assigning it to a new label.
As you can see from the code we have skipped some paramaters on CreateDynamic3DTextLabel jumping straight away to attachedvehicle after the CreateDynamic3DTextLabel's parameter Float: drawdistance, leaving those skipped paramaters to use their own assigned default values. The label gets created on the player's virtual world & interior and it is only visible to them (therefore why it's playerid = playerid) no one else can see it.
While we are creating the Label after the function is called, we will also run a timer. This timer will serve a purpose when it gets called it will destroy the label and kill itself. We have named the timer TerminateLabel, we have set the interval to (time * 1000), Why (time * 1000) you ask? 1000 mileseconds is equivalent to 1 second and we do not use seconds when setting up timers but we use mileseconds instead. We are multiplying 1000 over the parameter time so the result will be on mileseconds (E.G. 5 * 1000 = 5000, 5000 is equal to 5 seconds) - We have also set the repeating parameter to false since we wouldn't be repeatedly using the timer for any other stuff but rather than use it on destroying a label which is a ONE TIME thing only.
Don't forget to add this inside the OnPlayerDisconnect callback
PHP код:
if(IsValidDynamic3DTextLabel(Label3D[playerid]))
{
KillTimer(LabelTimer[playerid]);
DestroyDynamic3DTextLabel(Label3D[playerid]);
}
Make sure to add return 1; before the enclosing parenthesis to avoid the "should return a value" warning.
[Part Four] Final Touch - Adding the Timer
PHP код:
forward TerminateLabel(playerid);
public TerminateLabel(playerid)
{
KillTimer(LabelTimer[playerid]);
return DestroyDynamic3DTextLabel(Label3D[playerid]);
}
Inside the callback is where we placed the KillTimer, it kills the LabelTimer to prevent itself from running repeatedly/removes-deattach LabelTimer from being assigned to that timer. Below the KillTimer function is the DestroyDynamic3DTextLabel which is self-explanatory by its own name, destroying the Label3D text label after the timer has been called.
Over all this is what the function & the timer should look like after you have coded them
PHP код:
// Below the #includes
new LabelTimer[MAX_PLAYERS];
new Text3D:Label3D[MAX_PLAYERS];
public OnPlayerDisconnect(playerid, reason)
{
if(IsValidDynamic3DTextLabel(Label3D[playerid]))
{
KillTimer(LabelTimer[playerid]);
DestroyDynamic3DTextLabel(Label3D[playerid]);
}
return 1;
}
stock ShowLabelToPlayer(playerid, text[], color, Float:x, Float:y, Float:z, time, Float:distance, vehicle = INVALID_VEHICLE_ID)
{
if(IsValidDynamic3DTextLabel(Label3D[playerid]))
{
KillTimer(LabelTimer[playerid]);
DestroyDynamic3DTextLabel(Label3D[playerid]);
}
Label3D[playerid] = CreateDynamic3DTextLabel(text, color, x, y, z, distance, .attachedvehicle = vehicle, .worldid = GetPlayerVirtualWorld(playerid), .interiorid = GetPlayerInterior(playerid), .playerid = playerid);
LabelTimer[playerid] = SetTimerEx("TerminateLabel", (1000 * time), false, "d", playerid);
return 1;
}
forward TerminateLabel(playerid);
public TerminateLabel(playerid)
{
KillTimer(LabelTimer[playerid]);
return DestroyDynamic3DTextLabel(Label3D[playerid]);
}
[SAMPLE] Script
Remainder - The x/y/z parameter depends if it gets attached to a vehicle or not, if its not attached to a vehicle. It will be created to that specific coordinate, if its attached then the X/Y/Z will be the position/location/coordinate-parts of the vehicle.
PHP код:
#include <a_samp>
#include <zcmd>
#include <streamer>
new Specific[MAX_PLAYERS];
new LabelTimer[MAX_PLAYERS];
new Text3D:Label3D[MAX_PLAYERS];
new LSPDVehicles[6];
// A test command that shows ShowLabelToPlayer working without the vehicle attachment.
// Sets the var Specific to ZERO/ONE (creates a label to the player's position)
CMD:test(playerid, params[])
{
new Float:x, Float:y, Float:z;
GetPlayerPos(playerid, x, y, z);
switch(Specific[playerid])
{
case false:
{
Specific[playerid] = true;
ShowLabelToPlayer(playerid, "This is a test command, you have set a var to one.", 0x33AA33FF, x, y, z, 5, 10.0);
}
case true:
{
Specific[playerid] = false;
ShowLabelToPlayer(playerid, "This is a test command, you have set a var to false.", 0xFF6347FF, x, y, z, 5, 10.0);
}
}
return 1;
}
public OnFilterScriptInit()
{
// Sample vehicles.
LSPDVehicles[0] = AddStaticVehicleEx(596, 1602.5122, -1680.3663, 5.4630, 90.1200, 0, 1, -1);
LSPDVehicles[1] = AddStaticVehicleEx(596, 1602.4829, -1684.0609, 5.4630, 90.1200, 0, 1, -1);
LSPDVehicles[2] = AddStaticVehicleEx(596, 1602.5535, -1688.0438, 5.4630, 90.1200, 0, 1, -1);
LSPDVehicles[3] = AddStaticVehicleEx(596, 1602.6227, -1692.1034, 5.4630, 90.1200, 0, 1, -1);
LSPDVehicles[4] = AddStaticVehicleEx(596, 1602.4873, -1696.2058, 5.4630, 90.1200, 0, 1, -1);
LSPDVehicles[5] = AddStaticVehicleEx(596, 1602.5120, -1700.2861, 5.4630, 90.1200, 0, 1, -1);
return 1;
}
public OnPlayerConnect(playerid)
{
// Sets Specific var to ZERO.
Specific[playerid] = false;
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
if(IsValidDynamic3DTextLabel(Label3D[playerid]))
{
KillTimer(LabelTimer[playerid]);
DestroyDynamic3DTextLabel(Label3D[playerid]);
}
return 1;
}
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
for(new i; i < sizeof(LSPDVehicles); i++)
{
if(vehicleid == LSPDVehicles[i])
{
// If the var for Specific is set to ZERO then the player cannot enter.
if(!Specific[playerid])
{
// Claars animation after the player attempted to enter the vehicle.
ClearAnimations(playerid);
return ShowLabelToPlayer(playerid, "You are not a LSPD, cannot enter this vehicle.", 0xFF6347FF, 0.0, 0.0, 0.0, 5, 10.0, vehicleid);
// Uses the function, Attachs a label to the player's entered vehicle with the context "You are not a LSPD, cannot enter this vehicle."
// Color - Lightred, The time when the label will disappear/expire is 5 seconds (The label can be seen within 10 meters radius only)
}
}
}
return 1;
}
stock ShowLabelToPlayer(playerid, text[], color, Float:x, Float:y, Float:z, time, Float:distance, vehicle = INVALID_VEHICLE_ID)
{
if(IsValidDynamic3DTextLabel(Label3D[playerid]))
{
KillTimer(LabelTimer[playerid]);
DestroyDynamic3DTextLabel(Label3D[playerid]);
}
Label3D[playerid] = CreateDynamic3DTextLabel(text, color, x, y, z, distance, .attachedvehicle = vehicle, .worldid = GetPlayerVirtualWorld(playerid), .interiorid = GetPlayerInterior(playerid), .playerid = playerid);
LabelTimer[playerid] = SetTimerEx("TerminateLabel", (1000 * time), false, "d", playerid);
return 1;
}
forward TerminateLabel(playerid);
public TerminateLabel(playerid)
{
KillTimer(LabelTimer[playerid]);
return DestroyDynamic3DTextLabel(Label3D[playerid]);
}
Thank you & god bless you, Have a nice day everybody.