What is better
#1

Hello,
Well I know script, create some functions, but I have a question about it.
is better to script like this:
Код:
///////1st example
new Msg[128];
function1()
{
     format(Msg, 128, "Some Text");
     SendClientMessageToAll(0x00FF00FF, Msg);
}
function2()
{
     format(Msg, 128, "Some Text");
     SendClientMessageToAll(0x00FF00FF, Msg);
}
function3()
{
     format(Msg, 128, "Some Text");
     SendClientMessageToAll(0x00FF00FF, Msg);
}
or like this:
Код:
///////2nd example
function1()
{
     new Msg[128];
     format(Msg, 128, "Some Text");
     SendClientMessageToAll(0x00FF00FF, Msg);
}
function2()
{
     new Msg[128];
     format(Msg, 128, "Some Text");
     SendClientMessageToAll(0x00FF00FF, Msg);
}
function3()
{
     new Msg[128];
     format(Msg, 128, "Some Text");
     SendClientMessageToAll(0x00FF00FF, Msg);
}
I started to optimize my gamemode so I asking will this help (to reduce memory useage and so on) or won't help?
Reply
#2

In that specific example, both versions are equally horrible. Functions can have parameters. Use them.
Global variables are evil. Avoid whenever possible.
Reply
#3

Global variables can be conflicted, never use if you're going to change them.
So the second case is better.

Quote:
Originally Posted by Vince
Посмотреть сообщение
In that specific example, both versions are equally horrible. Functions can have parameters. Use them.
Global variables are evil. Avoid whenever possible.
Functions can have parameters.

It's not always required (like in the code posted) a variable as function parameter, and it would be the same thing except that you would be able to give the content in the arguments when you call it.
Reply
#4

2nd example is better

but with different string names.
Reply
#5

Quote:
Originally Posted by Vince
Посмотреть сообщение
In that specific example, both versions are equally horrible. Functions can have parameters. Use them.
Global variables are evil. Avoid whenever possible.
ok, than how about this:

Код:
///////1st example
new Msg[128];
public OnPlayerConnect(playerid)
{
     format(Msg, 128, "Some Text");
     SendClientMessageToAll(0x00FF00FF, Msg);
     return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
     format(Msg, 128, "Some Text");
     SendClientMessageToAll(0x00FF00FF, Msg);
     return 1;
}
and some other callbacks
or this:


Код:
///////2nd example
public OnPlayerConnect(playerid)
{
     new Msg[128];
     format(Msg, 128, "Some Text");
     SendClientMessageToAll(0x00FF00FF, Msg);
     return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
     new Msg[128];
     format(Msg, 128, "Some Text");
     SendClientMessageToAll(0x00FF00FF, Msg);
     return 1;
}
and some other callbacks with same variables inside: new Msg[128]...
Reply
#6

Anyone?
Reply
#7

First.
Reply
#8

Third:
pawn Код:
public OnPlayerConnect(playerid)
{
     SendClientMessageToAll(0x00FF00FF, "Some Text");
     return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
     SendClientMessageToAll(0x00FF00FF, "Some Text");
     return 1;
}
Reply
#9

Quote:
Originally Posted by Maro06
Посмотреть сообщение
First.
are you sure? Or can anyone other confirm that, because I've large gamemode I don't want to make something that will bug wil gamemode

Edit:
Quote:
Originally Posted by SickAttack
Посмотреть сообщение
Third:
pawn Код:
public OnPlayerConnect(playerid)
{
     SendClientMessageToAll(0x00FF00FF, "Some Text");
     return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
     SendClientMessageToAll(0x00FF00FF, "Some Text");
     return 1;
}
but if I use
Код:
    format(Msg, 128, "Player name %s VIP %s", Name, VIPNivoName[Level]);
and more like this
Reply
#10

Quote:
Originally Posted by AntonioCroatia
Посмотреть сообщение
are you sure? Or can anyone other confirm that, because I've large gamemode I don't want to make something that will bug wil gamemode

Edit:


but if I use
Код:
    format(Msg, 128, "Player name %s VIP %s", Name, VIPNivoName[Level]);
and more like this
I don't see what the problem is here, just keep the cells to an estimated length and don't make it a global variable.
pawn Код:
public OnPlayerConnect(playerid)
{
    new string[50];
    format(string, sizeof(string), "Player name %s VIP %s", Name, VIPNivoName[Level]);
    SendClientMessageToAll(0x00FF00FF, string);
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)