Need some answers.
#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


Messages In This Thread
Need some answers. - by Faisal_khan - 09.06.2012, 06:13
AW: Need some answers. - by Extremo - 09.06.2012, 06:22
Re: Need some answers. - by MP2 - 09.06.2012, 06:38
Re: Need some answers. - by Rudy_ - 09.06.2012, 07:13
Re: Need some answers. - by Faisal_khan - 09.06.2012, 07:27
AW: Need some answers. - by Extremo - 09.06.2012, 07:28
Re: Need some answers. - by Faisal_khan - 09.06.2012, 07:36
AW: Need some answers. - by Extremo - 09.06.2012, 07:46
Re: Need some answers. - by Faisal_khan - 09.06.2012, 07:50
Re: Need some answers. - by MadeMan - 09.06.2012, 07:51

Forum Jump:


Users browsing this thread: 3 Guest(s)