Passing a variable by reference using SetTimerEx -
mongi - 06.05.2018
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", 1000, true, "i", TEST);
return 1;
}
Console:
PHP код:
[22:19:20] 33554432
[22:19:21] 33554432
[22:19:22] 33554432
[22:19:23] 33554432
[22:19:24] 33554432
[22:19:26] 33554432
[22:19:27] 33554432
[22:19:28] 33554432
[22:19:29] 33554432
[22:19:30] 33554432
[22:19:31] 33554432
[22:19:32] 33554432
[22:19:33] 33554432
Re: Passing a variable by reference using SetTimerEx -
kovac - 06.05.2018
PHP код:
new TEST = 5;
forward decrease();
public decrease()
{
TEST--;
printf("%i", TEST);
return 1;
}
public OnGameModeInit()
{
SetTimer("decrease", 1000, true);
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", 1000, true, "i", amount);
return 1;
}
Re: Passing a variable by reference using SetTimerEx -
jasperschellekens - 06.05.2018
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(str, sizeof(str), "%i", n);
printf(str);
}
public OnGameModeInit()
{
SetTimerEx("decrease", 1000, true, "i", TEST);
return 1;
}
Re: Passing a variable by reference using SetTimerEx -
Jefff - 06.05.2018
Remove & mongi
Re: Passing a variable by reference using SetTimerEx -
mongi - 06.05.2018
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(str, sizeof(str), "%i", n);
printf(str);
}
public OnGameModeInit()
{
SetTimerEx("decrease", 1000, true, "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
but I need it via a timer.
Re: Passing a variable by reference using SetTimerEx -
NaS - 07.05.2018
Public functions that have arguments which are passed by reference expect a variable (or array) as input, not a number.
If you do
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.
Re: Passing a variable by reference using SetTimerEx -
Nero_3D - 07.05.2018
You can if you pass the correct address, for example by using ref from amx_memory
PHP код:
SetTimerEx("decrease", 1000, true, "i", ref(TEST));
SetTimerEx is just limited to global / static variables, local variables would be out of scope
CallLocalFunction works also with local ones
Re: Passing a variable by reference using SetTimerEx -
mongi - 07.05.2018
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", 1000, true, "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.