SA-MP Forums Archive
Variable stocking - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Variable stocking (/showthread.php?tid=307316)



Variable stocking - KreeDz - 30.12.2011

Hello, i`ll get straight to my problem.
So, i`m trying to get a variable to stock my number of cars, which is easy.

Main bits of the code:
Код:
#define CAR_AMMOUNT (carscounted+1)
new carscounted;
OnGameModeInit()
//cars//
carscounted = CountVehicles();
The problem is that, the variable gets stocked after the CAR_AMMOUNT is defined (carscounted being token as a "0").
So, how would i be able to stock the variable before the CAR_AMMOUNT is defined so "carscounted" will be token as the actual number of cars ?


Re: Variable stocking - Guest9328472398472 - 30.12.2011

I'm just saying out of ordinary, but the variable above the stock...idk


Re: Variable stocking - KreeDz - 30.12.2011

That's what i thought of, sadly, you can't count the vehicles before on gamemodeinit, as they are situated there. It will just return you a "0".


Re: Variable stocking - KreeDz - 30.12.2011

No one knows at all?


Re: Variable stocking - Gh05t_ - 30.12.2011

Quote:
Originally Posted by KreeDz
Посмотреть сообщение
The problem is that, the variable gets stocked after the CAR_AMMOUNT is defined (carscounted being token as a "0").
So, how would i be able to stock the variable before the CAR_AMMOUNT is defined so "carscounted" will be token as the actual number of cars ?
This isn't the issue.

pawn Код:
#define macro variable+1
new variable = 1;

main()
{
    printf("%d", macro);
}
pawn Код:
if (strcmp("/output", cmdtext, true, 10) == 0)
{
    new string[32];
    carscounted = CountVehicles();
   
    format(string, sizeof(string), "%d", carscounted);
    SendClientMessage(playerid, -1, string);
   
    //printf("%d", carscounted);
    return true;
}



Re: Variable stocking - KreeDz - 31.12.2011

Yeah, that would work perfectly for a command, the problem is that i`m using that macro throughout the script. So, it has to automatically count itself at ongamemodeinit. But as i said, it defines before counting taking it as a "zero".


Re: Variable stocking - Gh05t_ - 31.12.2011

Quote:
Originally Posted by KreeDz
Посмотреть сообщение
Yeah, that would work perfectly for a command, the problem is that i`m using that macro throughout the script. So, it has to automatically count itself at ongamemodeinit. But as i said, it defines before counting taking it as a "zero".
The purpose of the command was to output the value of carscounted. If your having issues with the outputted value (it being 0), then what you have stated is correct, BUT your CountVehicles(); function is the cause.


Re: Variable stocking - KreeDz - 31.12.2011

Actually, the output value is correct if i do printf("%d", carscounted); after the carscounted = CountVehicles();. And this is what makes me think that the value is defined incorrectly at the start of the script.
Код:
stock CountVehicles(){new cars;
for(new i = 0; i < 500; i++){
if(GetVehicleModel(i)!=0){
cars++;}}return cars;}



Re: Variable stocking - Gh05t_ - 31.12.2011

Quote:
Originally Posted by KreeDz
Посмотреть сообщение
Actually, the output value is correct if i do printf("%d", carscounted); after the carscounted = CountVehicles();. And this is what makes me think that the value is defined incorrectly at the start of the script.
Код:
stock CountVehicles(){new cars;
for(new i = 0; i < 500; i++){
if(GetVehicleModel(i)!=0){
cars++;}}return cars;}
You must call this function AFTER the information relating to it. Use my example below as reference.

pawn Код:
#define macro CountVehicles()+1

stock CountVehicles()
{
    new cars;
    for(new i = 0; i < 500; i++) if(GetVehicleModel(i)!=0) cars++;
    return cars;
}

public OnGameModeInit()
{
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60);
    printf("%d",CountVehicles());
    printf("%d",macro);
    return 1;
}
Код:
Output:
1
2



Re: Variable stocking - KreeDz - 31.12.2011

Alright, so, i have to move all the stuff that requires the macro after the ongamemodeinit. I`ll give it a try, it'll sure work as the macro return the right amount of cars after the cars are created. Thanks!