[Tutorial] ShowLabelToPlayer (Similar to LSRPs)
#1

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)

PHP код:
new LabelTimer[MAX_PLAYERS];
new 
Text3D:Label3D[MAX_PLAYERS]; 
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)

PHP код:
stock ShowLabelToPlayer(playeridtext[], colorFloat:xFloat:yFloat:ztimeFloat:distancevehicle INVALID_VEHICLE_ID)
{
    return 
1;

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

PHP код:
if(IsValidDynamic3DTextLabel(Label3D[playerid]))
{
    
KillTimer(LabelTimer[playerid]);
    
DestroyDynamic3DTextLabel(Label3D[playerid]);
}
Label3D[playerid] = CreateDynamic3DTextLabel(textcolorxyzdistance, .attachedvehicle vehicle, .worldid GetPlayerVirtualWorld(playerid), .interiorid GetPlayerInterior(playerid), .playerid playerid);
LabelTimer[playerid] = SetTimerEx("TerminateLabel", (1000 time), false"d"playerid); 
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

PHP код:
if(IsValidDynamic3DTextLabel(Label3D[playerid]))
{
    
KillTimer(LabelTimer[playerid]);
    
DestroyDynamic3DTextLabel(Label3D[playerid]);

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

PHP код:
forward TerminateLabel(playerid);
public 
TerminateLabel(playerid)
{
    
KillTimer(LabelTimer[playerid]);
    return 
DestroyDynamic3DTextLabel(Label3D[playerid]);

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

PHP код:
// Below the #includes
new LabelTimer[MAX_PLAYERS];
new 
Text3D:Label3D[MAX_PLAYERS];
public 
OnPlayerDisconnect(playeridreason)
{
    if(
IsValidDynamic3DTextLabel(Label3D[playerid]))
    {
        
KillTimer(LabelTimer[playerid]);
        
DestroyDynamic3DTextLabel(Label3D[playerid]);
    }
    return 
1;
}
stock ShowLabelToPlayer(playeridtext[], colorFloat:xFloat:yFloat:ztimeFloat:distancevehicle INVALID_VEHICLE_ID)
{
    if(
IsValidDynamic3DTextLabel(Label3D[playerid]))
    {
        
KillTimer(LabelTimer[playerid]);
        
DestroyDynamic3DTextLabel(Label3D[playerid]);
    }
    
Label3D[playerid] = CreateDynamic3DTextLabel(textcolorxyzdistance, .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]);

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.

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(playeridparams[])
{
    new 
Float:xFloat:yFloat:z;
    
GetPlayerPos(playeridxyz);
    switch(
Specific[playerid])
    {
        case 
false:
        {
            
Specific[playerid] = true;
            
ShowLabelToPlayer(playerid"This is a test command, you have set a var to one."0x33AA33FFxyz510.0);
        }
        case 
true:
        {
            
Specific[playerid] = false;
            
ShowLabelToPlayer(playerid"This is a test command, you have set a var to false."0xFF6347FFxyz510.0);
        }
    }
    return 
1;
}
public 
OnFilterScriptInit()
{
    
// Sample vehicles.
    
LSPDVehicles[0] = AddStaticVehicleEx(5961602.5122, -1680.36635.463090.120001, -1);
    
LSPDVehicles[1] = AddStaticVehicleEx(5961602.4829, -1684.06095.463090.120001, -1);
    
LSPDVehicles[2] = AddStaticVehicleEx(5961602.5535, -1688.04385.463090.120001, -1);
    
LSPDVehicles[3] = AddStaticVehicleEx(5961602.6227, -1692.10345.463090.120001, -1);
    
LSPDVehicles[4] = AddStaticVehicleEx(5961602.4873, -1696.20585.463090.120001, -1);
    
LSPDVehicles[5] = AddStaticVehicleEx(5961602.5120, -1700.28615.463090.120001, -1);
    return 
1;
}
public 
OnPlayerConnect(playerid)
{
    
// Sets Specific var to ZERO.
    
Specific[playerid]  = false;
    return 
1;
}
public 
OnPlayerDisconnect(playeridreason)
{
    if(
IsValidDynamic3DTextLabel(Label3D[playerid]))
    {
        
KillTimer(LabelTimer[playerid]);
        
DestroyDynamic3DTextLabel(Label3D[playerid]);
    }
    return 
1;
}
public 
OnPlayerEnterVehicle(playeridvehicleidispassenger)
{
    for(new 
isizeof(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."0xFF6347FF0.00.00.0510.0vehicleid);
                
// 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(playeridtext[], colorFloat:xFloat:yFloat:ztimeFloat:distancevehicle INVALID_VEHICLE_ID)
{
    if(
IsValidDynamic3DTextLabel(Label3D[playerid]))
    {
        
KillTimer(LabelTimer[playerid]);
        
DestroyDynamic3DTextLabel(Label3D[playerid]);
    }
    
Label3D[playerid] = CreateDynamic3DTextLabel(textcolorxyzdistance, .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]);

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.
Reply
#2

Pictures? For those who seem curious but don't know what you're talking about.
Reply
#3

Quote:
Originally Posted by ISmokezU
Посмотреть сообщение
Pictures? For those who seem curious but don't know what you're talking about.
Sure, give me a second.

Edit: Updated.
Reply
#4

Was a tutorial really needed? 1 star
Reply
#5

gsgsgsgsgsgs
Reply
#6

It's too easy to do that but my friend your explanation is what deserve a + rep.
Reply
#7

You just had to find the most unoptimized way to do this, there's functions for this already.

https://sampwiki.blast.hk/wiki/CreatePlayer3DTextLabel
Reply
#8

Quote:
Originally Posted by Meller
Посмотреть сообщение
You just had to find the most unoptimized way to do this, there's functions for this already.

https://sampwiki.blast.hk/wiki/CreatePlayer3DTextLabel
Never knew that this function exists, the first thing that came out of my mind is to use the playerid function that is provided on CreateDynamic3DTextLabel.
Reply
#9

if I am not wrong Streamer works by player based stuff. like if you do CreateDynamicObject it will use CreatePlayerObject actually, so it won't stream the object which is only near you to all players and / or the label near you to all players. so you've just did it right by using stramer and adjusting playerid parameter in it!

well done, +rep when I become able to give lol [ been reping so much recently. ]


( small note: there's no createplayervehicle in samp and that's why streamer avoided adding vehicle stream. )
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)