AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Transfer variables from one function to another (https://forums.alliedmods.net/showthread.php?t=131605)

SpeeDeeR 07-06-2010 21:53

Transfer variables from one function to another
 
I think this question is kinda dumb and maybe its already asked somewhere.
I came across this problem many times before and because of my lack of knowledge i couldnt go over it.So I'm asking now how is it possible.
PHP Code:

public asd()
{
if(
smth == 1)
{
a++
}
public 
adf()
{
if(
== 2// line X
{
...


Of course on line X it will bring up error for undefined symbol and if i define it in adf() it will not have the same value as asd().
If i define it out of the both functions, again it will ot have the same value.

Alucard^ 07-06-2010 23:01

Re: Transfer variables from one function to another
 
First of all...

PHP Code:

if(2

-->

PHP Code:

if(== 2

And same for smth = 1.

The whole code should be something like this:

PHP Code:

new a;

public 
asd()
{
    if(
smth == 1)
    {
        
a++;
    }
}

public 
adf()
{
    if(
== 2)
    {
        
// something
    



just create "a" variable, as global.

Bugsy 07-06-2010 23:09

Re: Transfer variables from one function to another
 
I'm not 100% sure what your question is but maybe this will help.

Passing a variable by-reference. You will see "Var1=4" in console.
PHP Code:

register_concmd"test" "TestFunc" );

public 
TestFuncid )
{
    
Func1();
}

public 
Func1()
{
    new 
Var1;
    
    
Func2Var1 );
    
Func2Var1 );
    
Func2Var1 );
    
Func2Var1 );
    
    
server_print"Var1=%d" Var1 );
}

public 
Func2( &TheVar )
{
    
TheVar++;


Using a global variable. You will see "Var1=4" on the first command, "Var1=8" on the second and so on, each incremented by 4.
PHP Code:

//Place this outside of all functions (below your includes or whatever)
new Var1;

register_concmd"test" "TestFunc" );

public 
TestFuncid )
{
    
Func1();
}

public 
Func1()
{
    
Func2();
    
Func2();
    
Func2();
    
Func2();
    
    
server_print"Var1=%d" Var1 );
}

public 
Func2()
{
    
Var1++;



SpeeDeeR 07-07-2010 05:40

Re: Transfer variables from one function to another
 
The thing that I'm asking is how to pass values from a function to another.Like in my example i want to pass the value of variable 'a' in asd() to the variable 'a' in adf().I just want 'a' in asd() to be equal to 'a' in adf().

Owyn 07-07-2010 05:45

Re: Transfer variables from one function to another
 
PHP Code:

public asd()
{
new 
a
if(smth == 1)
{
a++
adf(a)
}
public 
adf(a)
{
if(
== 2// line X
{
...



SpeeDeeR 07-07-2010 05:59

Re: Transfer variables from one function to another
 
Thank you @Owyn.Now when that is cleared I'm asking again how to do it but this time for multiple values passed in a single function.

Owyn 07-07-2010 06:05

Re: Transfer variables from one function to another
 
PHP Code:

public asd()
{
new 
ab
if(smth == 1)
{
a++
b++
adf(a,b)
}
public 
adf(a,b)
{
if(
== 2// line X
{
...
}  
if(
== 2// line Y
{
...


</span></span>

SpeeDeeR 07-07-2010 06:06

Re: Transfer variables from one function to another
 
omfg thank you again.That clears up eveything.


All times are GMT -4. The time now is 07:10.

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