Raised This Month: $51 Target: $400
 12% 

Solved What does mean ampersands and dots before variable name?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
kww
Senior Member
Join Date: Feb 2021
Location: Russia
Old 09-09-2021 , 03:16   What does mean ampersands and dots before variable name?
Reply With Quote #1

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"
__________________
Now working on: Side Weapons (Very lazy, tbh)
Avatar source: https://bit.ly/3BAk19g
Discord: kww#9951

Last edited by kww; 09-09-2021 at 14:53.
kww is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 09-09-2021 , 06:00   Re: What does mean ampersands and dots before variable name?
Reply With Quote #2

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

Last edited by HamletEagle; 09-09-2021 at 06:05.
HamletEagle is offline
kww
Senior Member
Join Date: Feb 2021
Location: Russia
Old 09-09-2021 , 14:54   Re: What does mean ampersands and dots before variable name?
Reply With Quote #3

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
__________________
Now working on: Side Weapons (Very lazy, tbh)
Avatar source: https://bit.ly/3BAk19g
Discord: kww#9951

Last edited by kww; 09-09-2021 at 15:09.
kww is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 09-09-2021 , 16:08   Re: What does mean ampersands and dots before variable name?
Reply With Quote #4

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

Last edited by HamletEagle; 09-09-2021 at 16:11.
HamletEagle is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 09-10-2021 , 08:07   Re: What does mean ampersands and dots before variable name?
Reply With Quote #5

Nice @HamletEagle. I still see and learn new things here. That function .parameters
Bacardi is offline
LearninG
Senior Member
Join Date: Apr 2019
Location: Iran
Old 09-10-2021 , 08:29   Re: What does mean ampersands and dots before variable name?
Reply With Quote #6

Quote:
Originally Posted by Bacardi View Post
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.
LearninG is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:20.


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