Question with function
#1

I have found this:
pawn Код:
new var[20];

public OnPlayerConnect(playerid)
{
     format(var,sizeof(var),"Hello");
     OnPlayerStartGame(playerid,var);
     return 1;
}

forward OnPlayerStartGame(playerid,text[])
{
     printf("Test 1: %s",text);
     format(var,sizeof(var),"Bye");
     printf("Test 2:%s",text);
     return 1;
}
Test 1 shows Hello, but Test 2 - Bye.

Is it supposed to be? I thought 'var' will be stored as 'text' and while i dont change 'text' to something else it will be always 'Hello'.
Reply
#2

Its normal because var its global variable and you formating 2texts.
Reply
#3

pawn Код:
forward OnPlayerStartGame(playerid,text[]);
public OnPlayerStartGame(playerid,text[])
{
     printf("Test 1: %s",text);
     format(var,sizeof(var),"Bye");
     printf("Test 2:%s",var);
     return 1;
}
Reply
#4

Interesting, I didn't know that.
This is probably because the function just calls the variable 'var' always when the 'text' variable gets called. Might be compared with a shortcut (Shortcut to a variable? Interesting?!).
Can't explain this otherwise.

Jeffry
Reply
#5

Quote:
Originally Posted by Raimis_R
Посмотреть сообщение
Its normal because var its global variable and you formating 2texts.
He didn't changed the value of the global variable 'var', he changed the local one 'text'.
Why the global variable has changed?
The reason is because all varialbles which have [](have dimensions) are acting same as pointers. So, you can chage their value via other function if they are in parameters of that function.
Same thing happens in Dev C aswell.

EDIT: It doesn't even need to be global variable if you want to change it. See example below.
pawn Код:
main()
{
     new var[10];
     var="Hey";
     printf("%s",var);// this should print 'Hey'
     Function(var);
     printf("%s",var);// this should print 'Hi'
}
Function(var[])
{
     var="Hello";
}
Reply
#6

This was interesting to me too. Because i thought that var will be saved as text. But it seems that it gets called when reading every new line.

I did this:

pawn Код:
new owner[MAX_PLAYER_NAME];
format(owner,sizeof(owner),"%s",HInfo[id][HOwner]);
OnPlayerBuyHouse(playerid,id,owner);
Reply
#7

Oh, i forgot to ask 1 more question. Why sometimes functions gets called in a different way?

in pawno:

pawn Код:
new skin = GetPlayerSkin(playerid);
SetPlayerSkin(playerid,0);
In game sometimes SetPlayerSkin gets called first and GetPlayerSkin second. So it doesn't save skin.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)