SA-MP Forums Archive
Question with function - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Question with function (/showthread.php?tid=269969)



Question with function - BuLLeT[LTU] - 18.07.2011

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'.


Re: Question with function - Raimis_R - 18.07.2011

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


Re: Question with function - swieberdevos - 18.07.2011

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;
}



Re: Question with function - Jeffry - 18.07.2011

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


Re: Question with function - Roko_foko - 18.07.2011

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";
}



Re: Question with function - BuLLeT[LTU] - 18.07.2011

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);



Re: Question with function - BuLLeT[LTU] - 18.07.2011

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.