Stock Function - Simple - Yes Or No Answer
#1

Hey,

I have decided to be slightly lazy in my script and speed up my commands by scripting a stock function and i am just wondering if it would work, basically the gamemode i am currently working on is a Cops And Robbers. And of course i cannot have cops, CIA or medics robbing places.

Here if the function:

pawn Код:
stock IsACop(playerid)
{
    if(gTeam[playerid] == TEAM_COP || gTeam[playerid] == TEAM_CIA || gTeam[playerid] == TEAM_MEDIC)
    {
        SendClientMessage(playerid,COLOR_RED,"Law Enforcement Officers Cannot Rob Places");
    }
}
As you can see very simple but if someone could confirm it works that would be great thanks.

I only ask this as i am along way off complete and not thinking of adding commands at the moment.
Reply
#2

Not really.. it doesn't return anything.

If you don't want a 'function IsACop must return a value' warning, this will be the right code:

pawn Код:
stock IsACop(playerid)
{
    if(gTeam[playerid] == TEAM_COP || gTeam[playerid] == TEAM_CIA || gTeam[playerid] == TEAM_MEDIC)
    {
        SendClientMessage(playerid,COLOR_RED,"Law Enforcement Officers Cannot Rob Places");
    }
    return 1;
}
Everything else is fine. I have a similar function in my script (with the gTeam crap too), so yeah, the code above WILL work, but your current IsACop should be replaced with mine to avoid any problems, or just put

pawn Код:
return 1;
Before the very last } in the function.
Reply
#3

Thanks, Rep+, And yeah i see alot of roleplay scripts use something similar.
Reply
#4

Quote:
Originally Posted by xMichaelx
Посмотреть сообщение
Thanks, Rep+, And yeah i see alot of roleplay scripts use something similar, how would i do is a cop car?
pawn Код:
stock IsACopCar(vehicleid)
{
    switch(vehicleid):
    {
        case 599: true;
        case 569: true;
    }
    return 1;
}
And just continue like that?
Nope.

Return is to return a value, for example:

pawn Код:
stock Return46()
{
    return 46;
}

main()
{
    // this will print 46 since we returned 46
    // if we returned 69, it would print 69.
    printf("I am %d years old.", Return46());
}
Or..

pawn Код:
if(Return46() == 45)
    // we're returning 46, then we're checking if it's 45, but it's not
    print("46 is not 45");
So, look at this code:

pawn Код:
stock IsACopCar(vehicleid)
{
    switch(vehicleid):
    {
        case 599: true;
        case 569: true;
    }
    return 1;
}
There are a bit of things wrong with that code, but no problem, let's fix them.
It will return 1, even if it's NOT a cop car, here's a fixed version.

pawn Код:
stock IsACopCar(vehicleid)
{
    // You put a : at the end of the 0 on the code below, don't do it.
    // Also, you have to check the model, not the vehicle ID.
    // Because if you do switch(vehicleid), and ID 599 or ID 569
    // doesn't exist, the code won't work.
    switch(GetVehicleModel(vehicleid))
    {
        case 599: return 1;
        case 569: return 1;
    }
    return 0;
}
Let's explain the code.
When the vehicle's model is 599 or 569, it will return 1, but if it's not, it will return 0.

pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if(strcmp(cmdtext, "/iscopcar", true) == 0)
    {
         if(!IsPlayerInAnyVehicle(playerid)) return 1;
         if(IsACopCar(GetPlayerVehicleID(playerid)) == 1) // checks if the player's vehicle is model 599 or 569. Notice the 1 here? That's what was returned.
             print("Yep"); // IsACopCar returned 1, so the player is in a cop car!
         else if(IsACopCar(GetPlayerVehicleID(playerid)) == 0) // if the player's vehicle is not 599 or 569. See the 0? That's what was returned, since we're not in a cop car.
             print("Nope"); // IsACopCar returned 0, since we're not in a cop car!
         return 1;
    }
    return 0;
}
Some parts are hard to explain; sorry for that.
Reply
#5

Wow thanks for the detailed reply and yeah i kind of fucked up the switch(vehicleid), I'm using the following code:
pawn Код:
stock IsACopCar(vehicleid)
{
    switch(vehicleid)
    {
        case 523, 427, 490, 528, 596, 598, 597, 599, 601, 432, 433: return 1;
    }
    return 1;
}
stock IsAPublicCar(vehicleid)
{
    switch(vehicleid)
    {
        case 416, 407, 544: return 1;
    }
    return 1;
}
Then just defining it under OnPlayerEnterVehicle as an 'if' statement so if the player is team civil sets that players wanted to 4 or 3 depending on cop or public. Infact i feel quite stupid really considering i used an IsAPlane function on my anti-cheat proberly just lack of sleep, thanks again for all your help.

Best regards,

-Michael.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)