AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved What does mean ampersands and dots before variable name? (https://forums.alliedmods.net/showthread.php?t=334239)

kww 09-09-2021 03:16

What does mean ampersands and dots before variable name?
 
E.g.
PHP Code:

native CsTeams:cs_get_user_team(index, &any:model CS_DONTCHANGE); 

or
PHP Code:

set_task(0.1, function, .flags "b"


HamletEagle 09-09-2021 06:00

Re: What does mean ampersands and dots before variable name?
 
. allows you to specify which values are used for which parameters.

For example, set_task header looks like this:
PHP Code:

set_task(Float:time, const function[], id 0, const any:parameter[] = ""len 0, const flags[] = ""repeat 0); 

A potential call would be:
PHP Code:

set_task(1.0"func"1234"something"10"b"

and this means that time is set to 1.0, function is "func", id is 1234 etc. Basically, parameters are filled in the order in which they are declared in the header.
But, maybe you want to fill them in a different order or explicitly state which value is for which parameter(useful in natives with a lot of parameters to make it clear what parameter is being set to what value). Then you can do:
PHP Code:

set_task(.time 1.0, .function = "func", .id 1234

or
PHP Code:

set_task(.id 1234, .time 1.0, .function = "func"

(notice I changed the order of parameters)

But, this alone is not terribly useful. The "true" usage of this is so you can skip some parameters you do not need while specifying a later parameter.
In your example you had:
PHP Code:

set_task(0.1"function", .flags "b"

time and function are normally specified(without ".") and then you have .flags = "b". Notice you skipped several parameters between "function" param and "flag" param. No values were specified for id, parameter, and len.
If you wanted to write this without the "."(dot notation) you would have to give all the intermediate parameters a value:
PHP Code:

set_task(0.1"function"0""0"b"

& for scalar values(simple variables, not arrays) means to pass a variable by reference(as opposed to passing by value when & is not used). When you pass by value a copy of the variable is passed to the function so if the function modifies that variable, the change will not be visible to the caller function.
When you pass by reference, informally, you pass the actual variable so if you change it inside the function, the change will be visible to the caller function.

PHP Code:

func(xy)
{
    
//x and y here are copies of the x and y from plugin_init
    
3
    y 
4
    
//from this point on, inside this function, x will be 3 and y 4
}

plugin_init()
{
    new 
1
    
new 2
    func
(xy)
    
//the changes made by func are not visible here
    //x and y are still 1 and 2


Now, the same code, but using pass by reference:

PHP Code:

func(&x, &y)
{
    
//x and y here are the same variables as the ones from plugin_init
    
3
    y 
4
    
//from this point on, inside this function, x will be 3 and y 4
}

plugin_init()
{
    new 
1
    
new 2
    func
(xy)
    
//the changes made by func are visible here
    //x and y are now 3 and 4


& is used when you want to change a parameter and have that change visible to the caller function. It is useful when you want to return move values from a function. One of them is returned with "return" and the others are "returned" by using reference parameters.
In your example, with cs_get_user_team, the model parameter can store/"return" the model id. It has to be a reference(&) in order to implement this functionality.

kww 09-09-2021 14:54

Re: What does mean ampersands and dots before variable name?
 
Not sure that i understood about ampersands but thank you so much

PHP Code:

plugin_init()
{
    new 
1
    
new 2

    
// I want to make sure. If i change them in funcA/B
    
funcA(x)
    
funcB(y)
    
// then here, after execution, x will be 3
    // and y still 2?
}

funcA(&x)
{
    
3
    
// even with
    
return PLUGIN_HANDLED
}

funcB(y)
{
    
4
    
// yes?
    
return PLUGIN_HANDLED


What is "reference parameters"? Sometimes i see "by reference", "reference parameters" and so on and can't figure out what it is

HamletEagle 09-09-2021 16:08

Re: What does mean ampersands and dots before variable name?
 
Yes. When using &, x is passed by reference so the modification is visible in plugin_init(x is now 3).
y is passed by value(no &) and will still be 2 in init.
Also, just to be clear, instead of plugin_init you can have any function. The general rule is that whem using & the modifications are visible to the caller function.

You could easily test this out by printing x and y to the console using server_print. Or make a command and print to chat.

Bacardi 09-10-2021 08:07

Re: What does mean ampersands and dots before variable name?
 
Nice @HamletEagle. I still see and learn new things here. That function .parameters

LearninG 09-10-2021 08:29

Re: What does mean ampersands and dots before variable name?
 
Quote:

Originally Posted by Bacardi (Post 2757391)
Nice @HamletEagle. I still see and learn new things here. That function .parameters

Yes , right. actually when i see posts of @HamletEagle , @Bugsy @Arkshine @Hawk @PM , @Emp , @BAILOPAN and some others... i say myself i am going to learn a new thing and definitely a correct one. i learn amxx like this.


All times are GMT -4. The time now is 02:32.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.