is there anything unnecessary in this script?
#1

hey, i started scripting a week ago and i need some feedback. i think i got the hang of variables but i'm not sure.
do you think this script below could be improved/shortened in any way?
pawn Код:
public OnPlayerSpawn(playerid)
{
    if(PlayerIsInJizzy[playerid] == 1)
    {
        PlayerIsInLobby[playerid] = 0;
        PlayerIsInMaddogg[playerid] = 0;
        SetPlayerInterior(playerid, 3);
        new Random = random(sizeof(JizzySpawns));
        SetPlayerPos(playerid, JizzySpawns[Random][0], JizzySpawns[Random][1], JizzySpawns[Random][2]);
        SetPlayerFacingAngle(playerid, JizzySpawns[Random][3]);
    }
    if(PlayerIsInMaddogg[playerid] == 1)
    {
        PlayerIsInJizzy[playerid] = 0;
        PlayerIsInLobby[playerid] = 0;
        SetPlayerInterior(playerid, 5);
        new Random = random(sizeof(MaddoggSpawns));
        SetPlayerPos(playerid, MaddoggSpawns[Random][0], MaddoggSpawns[Random][1], MaddoggSpawns[Random][2]);
        SetPlayerFacingAngle(playerid, MaddoggSpawns[Random][3]);
    }
    if(PlayerIsInLobby[playerid] == 1)
    {
        SetPlayerHealth(playerid, Float:0x7F800000 );
    }
    else
    {
        SetPlayerHealth(playerid, 100.0);
        SetPlayerArmour(playerid, 50.0);
    }
    return 1;
}
Reply
#2

Nope. Except the fact that you can:

Код:
if(PlayerIsInJizzy[playerid])
Instead of:

Код:
if(PlayerIsInJizzy[playerid] == 1)
But only if the variable is declared as a boolean.

Then you should stop the running code inside the statements with a return so you don't get two actions performed, I can see that there are probabilities that you receive two things, and if your if statement has only one line of code, there is no need to add braces, you can:

Код:
if(PlayerIsInLobby[playerid])
    return SetPlayerHealth(playerid, Float:0x7F800000);
Instead of:

Код:
if(PlayerIsInLobby[playerid] == 1)
{
    SetPlayerHealth(playerid, Float:0x7F800000 );
}
Reply
#3

Yes. You can use 1 variable only instead of 3 variables.

Value 0 is Lobby
Value 1 is Jizzy
Value 2 is Maddog

pawn Код:
enum
{
    IN_LOBBY,

    IN_JIZZY,
    IN_MADDOG
};

new Player_DM[MAX_PLAYERS];

public OnPlayerSpawn(playerid)
{
    switch (Player_DM[playerid])
    {
        case IN_LOBBY:
        {
            SetPlayerHealth(playerid, Float: 0x7F800000);
        }
        case IN_JIZZY:
        {
            new Random = random(sizeof(JizzySpawns));

            SetPlayerInterior(playerid, 3);
            SetPlayerPos(playerid, JizzySpawns[Random][0], JizzySpawns[Random][1], JizzySpawns[Random][2]);
            SetPlayerFacingAngle(playerid, JizzySpawns[Random][3]);

            SetPlayerHealth(playerid, 100.0);
            SetPlayerArmour(playerid, 50.0);
        }
        case IN_MADDOG:
        {
            new Random = random(sizeof(MaddoggSpawns));

            SetPlayerInterior(playerid, 5);
            SetPlayerPos(playerid, MaddoggSpawns[Random][0], MaddoggSpawns[Random][1], MaddoggSpawns[Random][2]);
            SetPlayerFacingAngle(playerid, MaddoggSpawns[Random][3]);

            SetPlayerHealth(playerid, 100.0);
            SetPlayerArmour(playerid, 50.0);
        }
    }
    return 1;
}
Reply
#4

It works both ways you are missing code.

Код:
      SetPlayerPos(playerid, MaddoggSpawns[Random][0], MaddoggSpawns[Random][1], MaddoggSpawns[Random][2]);
        SetPlayerFacingAngle(playerid, MaddoggSpawns[Random][3]);
https://sampwiki.blast.hk/wiki/SetCameraBehindPlayer

Always set camera behind after setting pos/facing angle.

You should also NEVER use SetPlayerPos() in OnPlayerSpawn() use SetPlayerSpawnInfo() before they spawn!
Reply
#5

Quote:
Originally Posted by TheToretto
Посмотреть сообщение
and if your if statement has only one line of code, there is no need to add braces, you can:

Код:
if(PlayerIsInLobby[playerid])
    return SetPlayerHealth(playerid, Float:0x7F800000);
Instead of:

Код:
if(PlayerIsInLobby[playerid] == 1)
{
    SetPlayerHealth(playerid, Float:0x7F800000 );
}
why? what's the difference?
Reply
#6

Quote:
Originally Posted by ConnorW
Посмотреть сообщение
why? what's the difference?
There is no difference, beside the return added, two unnecessary braces as the title says
Reply
#7

Quote:
Originally Posted by TheToretto
Посмотреть сообщение
There is no difference, beside the return added, two unnecessary braces as the title says
Unnecessary? How so, just because you're lazy to do them doesn't mean you should recommend it to people, there's nothing wrong doing {} and a clean code.
Reply
#8

Quote:
Originally Posted by ConnorW
Посмотреть сообщение
Unnecessary? How so, just because you're lazy to do them doesn't mean you should recommend it to people, there's nothing wrong doing {} and a clean code.
Well, I would say it looks better not using them if you have just one function however it is good practice to use them if you expect to add more code later then it is already set to do so.
Reply
#9

Quote:
Originally Posted by Pottus
Посмотреть сообщение
It works both ways you are missing code.

Код:
      SetPlayerPos(playerid, MaddoggSpawns[Random][0], MaddoggSpawns[Random][1], MaddoggSpawns[Random][2]);
        SetPlayerFacingAngle(playerid, MaddoggSpawns[Random][3]);
https://sampwiki.blast.hk/wiki/SetCameraBehindPlayer

Always set camera behind after setting pos/facing angle.

You should also NEVER use SetPlayerPos() in OnPlayerSpawn() use SetPlayerSpawnInfo() before they spawn!
Why would SetPlayerSpawnInfo be better if I may ask??
Reply
#10

Quote:
Originally Posted by masterart
Посмотреть сообщение
Why would SetPlayerSpawnInfo be better if I may ask??
SetPlayerPos() is not really made to set the player position when they spawn yes it works but that it a poor use of this function.

What ends up happening is you are making determinations of where the player should spawn after they have already spawned. That is counter intuitive when you can make that determination any number of times before they spawn. So if you know where they SHOULD spawn before they spawn why not have it set up already?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)