AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Variable life time? (https://forums.alliedmods.net/showthread.php?t=85830)

Flasher 02-16-2009 19:11

Variable life time?
 
Hi. I have a question.
What is a variable life time?
For this case:
PHP Code:

public client_putinserver(plrid)
{
    new 
param[1];
    
param[0] = plrid;
    
set_task(30.0"showInfo"BASE_TASK_ID plridparam1);
    return 
PLUGIN_CONTINUE;
}

public 
showInfo(param[])
{
    new 
plrid param[0];
    
//....


How long this task can be delayed to function to get an alive variable? For me this is working, but I doesn't know can I depend on this or not.
Or this is a bad code practise?
Because here: http://www.amxmodx.org/funcwiki.php?go=func&id=253
phatency wrote:
PHP Code:

new args[1]; 
public 
test1(id

    
args[0] = id
    
set_task(60.0,"MyFunction",0,args,1,"a",5); 


//args is defined outside function 
//because it might get deleted before MyFunction is called, 
//if it were defined inside test1. 


Vet 02-17-2009 00:23

Re: Variable life time?
 
Typically, the scope of a variable is only as wide as the routine its defined in. Thus, the life of your variables is only valid as long as each routine is executing. As soon as the sub/function is exited, the variables are released. A variable defined globally (not within a sub/function, like args in the second example)), lives and is visible throughout the entire plugin.
In the example you cited, I'd assume the author was referring the the value of the variable being deleted rather than the variable itself.
In your code, you don't really need to sent a parameter. You could simplely obtain the plrid again by subtracting out the BASE_TASK_ID

Bugsy 02-17-2009 00:54

Re: Variable life time?
 
Quote:

Originally Posted by Flasher (Post 762852)
Hi. I have a question.
What is a variable life time?
For this case:
PHP Code:

public client_putinserver(plrid)
{
    new 
param[1];
    
param[0] = plrid;
    
set_task(30.0"showInfo"BASE_TASK_ID plridparam1);
    return 
PLUGIN_CONTINUE;
}

public 
showInfo(param[])
{
    new 
plrid param[0];
    
//....


How long this task can be delayed to function to get an alive variable? For me this is working, but I doesn't know can I depend on this or not.
Or this is a bad code practise?

The variable will be passed without issue but you need to do additional checking in showinfo to confirm the player is still connected after 30 seconds.

ie.
- id 21 connects
- set_task called with id 21 as a param
- 10 seconds later id 21 disconnects
- 20 seconds later, showInfo(21) gets called from set_task but player no longer connected. any functions called on id 21 will error without first checking is_user_connected()

Flasher 02-17-2009 02:25

Re: Variable life time?
 
Thanks Vet and Bugsy!

Quote:

Originally Posted by Vet (Post 762995)
In the example you cited, I'd assume the author was referring the the value of the variable being deleted rather than the variable itself.

Variable discussed is an array and if it is destroyed, I think value should be lost.
Quote:

Originally Posted by Vet (Post 762995)
In your code, you don't really need to sent a parameter. You could simplely obtain the plrid again by subtracting out the BASE_TASK_ID

Thanks, I thinked about such way as a backup.

Quote:

Originally Posted by Vet (Post 762995)
Typically, the scope of a variable is only as wide as the routine its defined in. Thus, the life of your variables is only valid as long as each routine is executing. As soon as the sub/function is exited, the variables are released. A variable defined globally (not within a sub/function, like args in the second example)), lives and is visible throughout the entire plugin.

Quote:

Originally Posted by Bugsy (Post 763005)
The variable will be passed without issue

So can we came to one opinion about variable lifetime?

Quote:

Originally Posted by Bugsy (Post 763005)
but you need to do additional checking in showinfo to confirm the player is still connected after 30 seconds.
ie.
- id 21 connects
- set_task called with id 21 as a param
- 10 seconds later id 21 disconnects
- 20 seconds later, showInfo(21) gets called from set_task but player no longer connected. any functions called on id 21 will error without first checking is_user_connected()

Thanks for this valuable notice.
BTW If we have
PHP Code:

public client_disconnect(plrid)
{
    
remove_task(BASE_TASK_ID plrid);
    return 
PLUGIN_CONTINUE;


will this be enough to skip checking is_user_connected()?

Arkshine 02-17-2009 03:11

Re: Variable life time?
 
It should.


All times are GMT -4. The time now is 17:04.

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