Passing a variable by reference using SetTimerEx
#1

So I'm trying to pass a variable by reference to a function using SetTimerEx and I'm just getting a messy result.
I cannot find where I fucked up and I searched about this and didn't find a thing, Help?

PHP код:
new TEST 5;
forward decrease(&n);
public 
decrease(&n)
{
    
printf("%i"n);
}
public 
OnGameModeInit()
{
    
SetTimerEx("decrease"1000true"i"TEST);
    return 
1;

Console:
PHP код:
[22:19:2033554432
[22:19:2133554432
[22:19:2233554432
[22:19:2333554432
[22:19:2433554432
[22:19:2633554432
[22:19:2733554432
[22:19:2833554432
[22:19:2933554432
[22:19:3033554432
[22:19:3133554432
[22:19:3233554432
[22:19:3333554432 
Reply
#2

PHP код:
new TEST 5;
forward decrease();
public 
decrease()
{
    
TEST--;
    
printf("%i"TEST);
    return 
1;
}
public 
OnGameModeInit()
{
    
SetTimer("decrease"1000true);
    return 
1;

Is this what you want to do? I might misunderstood.

Edit:
Or maybe like this:
PHP код:
new TEST 5;
forward decrease(amount);
public 
decrease(amount)
{
    
TEST -= amount;
    
printf("%i"TEST);
    return 
1;
}
public 
OnGameModeInit()
{
    new 
amount 1;
    
SetTimerEx("decrease"1000true"i"amount);
    return 
1;

Reply
#3

format a string before printing it to get the right value.

PHP код:
new TEST 5
forward decrease(&n); 
public 
decrease(&n

    new 
str[128];
    
format(strsizeof(str), "%i"n);
    
printf(str); 

public 
OnGameModeInit() 

    
SetTimerEx("decrease"1000true"i"TEST); 
    return 
1

Reply
#4

Remove & mongi
Reply
#5

Quote:
Originally Posted by jasperschellekens
Посмотреть сообщение
format a string before printing it to get the right value.

PHP код:
new TEST 5
forward decrease(&n); 
public 
decrease(&n

    new 
str[128];
    
format(strsizeof(str), "%i"n);
    
printf(str); 

public 
OnGameModeInit() 

    
SetTimerEx("decrease"1000true"i"TEST); 
    return 
1

Still the same, It doesn't even print the correct value.



@kovac

Later on I'll have more variables which I would like to start decreasing 1 till they reach 0 so I want to pass the var. which i'm going to decrease to the function.
Doing it manually works
PHP код:
decrease(TEST); 
but I need it via a timer.
Reply
#6

Public functions that have arguments which are passed by reference expect a variable (or array) as input, not a number.

If you do

Код:
decrease(5);
the compiler will give you an error:

Quote:

error 035: argument type mismatch (argument 1)

And that's basically what SetTimerEx does - the timer does NOT pass the "TEST" variable like you expected it to do, it attempts to pass its value (5) which does not work.
Reply
#7

You can if you pass the correct address, for example by using ref from amx_memory
PHP код:
SetTimerEx("decrease"1000true"i"ref(TEST)); 
SetTimerEx is just limited to global / static variables, local variables would be out of scope
CallLocalFunction works also with local ones
Reply
#8

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
You can if you pass the correct address, for example by using ref from amx_memory
PHP код:
SetTimerEx("decrease"1000true"i"ref(TEST)); 
SetTimerEx is just limited to global / static variables, local variables would be out of scope
CallLocalFunction works also with local ones
Passing the address to the function using ref fixed it, I guess the function kept taking "TEST"'s value and considered it as an address which just messed up.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)