Need some answers.
#1

Hi SA-MP Community,

I need some answers to these questions.

Q1). Explain me format and string.(I don't want the wiki link)

Q2).If a want to code something like, a player sits in a car and he mustn't do drive-by. So should code it in OnPlayerUpdate or OnPlayerStateChange. Explain why?(This was an example question)


That's all these are my questions.

Awaiting your reply...
Reply
#2

There's no such thing as a string in pawn, it's just an array of " X * cell" and you can assign each cell one character.

In order to assign each cell with a character people designed format. Using format like this:

pawn Код:
new str[64];
format(str, 64, "Hello");
is SORT OF equal to
pawn Код:
for(new i = 0; i < 64; i++)
{
    if(i<arraysize)
    {
        string[i] = array[i];
    }
    else
    {
        string[i] == 0;
        break;
    }
}
While format would be an equivalant of:
pawn Код:
format(string, loopmax, array);

For your other question, you should use OnPlayerStateChange because OnPlayerUpdate is called WHENEVER something changes for the user which is pretty much ALL THE TIME resulting in the function literally being called almost every TICK. OnPlayerStateChange ONLY gets called when the STATE changes of the player.


EDIT:

And I forgot to mention that this obviously simplifies the insertion of other strings or data into an array e.g.

pawn Код:
new number, new Float:floats, new str[128], new hello[32];

format(hello, 32, "Hello");

number = 1;
floats = 2.0;

format(str, 128, "Hello: %s Number: %d Float: %f", hello, number, floats);
EDIT2:

pawn Код:
%s = string
%d = double (same as integer, theres no difference for pawn)
%i = integer (aka number)
%f = float (aka floating point number aka 1.123456)
Just so you understand the example above!


Hope it helps,
Regards.
Reply
#3

That is pretty confusing, and I know all about strings and format(), so I think Faisal_khan is going to go when he looks at that. I did.

Here's a clear explanation of format (which is what he wants to know - not how it works internally..):

pawn Код:
format(string, length, "text", replacements);
The string is the string you want to 'format', you must declare it BEFORE using format():
pawn Код:
new string[128];
The length is best to be ignored, just put 'sizeof(string)'.
pawn Код:
format(string, sizeof(string), "text", replacements);
In the text is where we put our message. If we want to insert things such as numbers we need to specify where it should be inserted with 'specifiers'. A list of these can be found on the wiki. Here's a practical example:

pawn Код:
new name = "MP2";
new id = 69;
new Float:health = 100.0;

new string[128];
format(string, sizeof(string), "%s (ID: %i) has %0.2f health.", name, id, health);
SendClientMessage(playerid, -1, string);
Should output "MP2 (ID: 69) has 100.0 health". %0.2f means it will show only two decimal places.

The ordering is obviously important.
Reply
#4

Thanks explained great..
Reply
#5

Thanks Extremo and MP2 for explaining it.

Q1). Is the no. 128 here is the no. of characters in the name?
pawn Код:
new string[128];
I mean the limit of the characters in the name.


Q2). Why we use this?
pawn Код:
sizeof(string)
Reply
#6

Even though I pretty much feel like I explained the same as MP2 I'd like to add something important!

pawn Код:
new variablename[somesize];

// remember to use the VARIABLENAME in sizeof not always sizeof(string) otherwise you WILL get errors at some point.

format(variablename, sizeof(variablename), "Some text with %s specifiers", stuff to replace the specifiers with in order of specifiers);

// Also keep in mind that a variablename may not be used twice in the same segment, a segment is this:

{
    // code
}

// This also applies to global variables!

new globalvariable;

// segment
{
    // cannot use globalvariable
}

Obviously if there is inner segments

{
    new var;
    {
        // cannot use var
    }
}
I hope this prevents issues.


EDIT:

To your last question: sizeof(variablename) returns the cells

pawn Код:
new variablename[cells];
Each cell can hold a character/number

so if you have a text like "Hello" you need 5 cells, keep in mind though that in programming everything starts with ZERO.

pawn Код:
new hello[5];
format(hello, sizeof(hello)/* returns 5 */, "Hello");

// Now, the above looks like this:
string[0]='H';
string[1]='e';
string[2]='l';
string[3]='l';
string[4]='o';
string[5]='\0'; // this tells the script that the string has ended and is a internal thing, so simply keep in mind that everything starts with zero but don't take it into account when you define the cells! aka: I need 5 cells for hello, 0,1,2,3,4 but since we don't take into account the zero when declaring, we declare 1,2,3,4,5 so 5 cells. There NEEDS to be ONE CELL for the NULL terminator!

EDIT2:

To avoid confusion, when I say "cannot use XX" I don't mean you can't reference to the variable like this

pawn Код:
new var = 2;
{
    printf("Debug: %d", var);
}
The above is FINE, what I mean is you cannot do this:
pawn Код:
new var = 2;
{
    new var = 3;
}
// Warning: var shadows a variable at a preceding level
If you have any open questions feel free to ask!

Regards.
Reply
#7

Here:
pawn Код:
new variablename[somesize];

// remember to use the VARIABLENAME in sizeof not always sizeof(string) otherwise you WILL get errors at some point.
That's an obvious statement. So you mean we can use anything in place of string? Like:
pawn Код:
new faisal[32];
And
pawn Код:
format(faisal, sizeof(faisal), "Some text with %s specifiers", stuff to replace the specifiers with in order of specifiers);
And what if we don't write sizeof and in place of that we write faisal[32]?

And

Q1). What are pvars?
Reply
#8

Yes that's what I pretty much mean. You can write this:

pawn Код:
format(faisal, 32, "Text", specifierreplacements);
That works just fine, though if you don't remember the cells or they change at a later point because you do this:

pawn Код:
new faisal[54];
format(faisal, 32, "Text", specifierreplacements); // now even tho you changed the size this one didn't adapt
You can face these issues.

To your question about PVars, they are variables stored in sa-mp and not in your script. So you can access them from any script at anytime.

Sort of like this:

Gamemode
pawn Код:
public OnPlayerConnect(playerid)
{
    SetPVarInt(playerid, "Varname", 1);
}
Filterscript
pawn Код:
public OnPlayerConnect(playerid)
{
    printf("Debug: %d", GetPVarInt(playerid, "Varname")); // will print Debug: 1
}
It's slower then regular variables but can be accessed by any script. Plus you don't need to define a size like you would with an array:

pawn Код:
new bool:IsSpawned[MAX_PLAYERS];
In the above scenario you had to tell the script that it should make the array long enough to hold a variable for EVERY player, now imagine though you have something like this:
pawn Код:
new bool:IsAdmin[MAX_PLAYERS];
Obviously not every player will be admin, so it may be pointless in storing false for everyone else right? So instead you can do this:
pawn Код:
SetPVarInt(playerid, "Admin", 1);
Now whenever you use this:
pawn Код:
GetPVarInt(playerid, "Admin");
It'll return 1 if you've set it for the player and EVEN IF IT IS NOT SET it will return 0.

I hope I am making sense here, if something is confusing to you don't hesistate to ask again and I'll try explaining it again!

Regards.
Reply
#9

The Pvars part was very easy to understand thanks dude.

Can you answer my above question(above your 2nd post).
Reply
#10

Quote:
Originally Posted by Faisal_khan
Посмотреть сообщение
Q2). Why we use this?
pawn Код:
sizeof(string)
If you want to change the length of the string, for example from 64 to 128, you have to change both

pawn Код:
new string[64];
format(string, 64, "Text Here");
pawn Код:
new string[128];
format(string, 128, "Text Here");
but if you use sizeof, you have to change it in 1 place only

pawn Код:
new string[64];
format(string, sizeof(string), "Text Here");
pawn Код:
new string[128];
format(string, sizeof(string), "Text Here");
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)