Weird behavior of #if, invalid instruction
#1

So, I have this example script:
pawn Code:
#include <a_samp>

#if !defined GetVehicleColor
new vehiclecolor[MAX_VEHICLES*2 char];
stock GetVehicleColor(vehicleid, &color1, &color2)
{
    color1 = vehiclecolor{vehicleid};
    color2 = vehiclecolor{vehicleid+1};
}
stock SaveVehicleColor(vehicleid, color1, color2)
{
    vehiclecolor{vehicleid} = color1;
    vehiclecolor{vehicleid+1} = color2;
}
#else
stock SaveVehicleColor(vehicleid, color1, color2)
{
    #pragma unused vehicleid, color1, color2
}
#endif

main()
{
    new c1,c2;
    print("Start");
    GetVehicleColor(0, c1, c2);
    printf("%d %d", c1, c2);
}
It's supposed to check if a default GetVehicleColor is supplied (like from YSF), and if not, add its own implementation (which needs external color saving). To me, this code seems perfectly valid. So why the hell is this shown in the console when I run the script (crashdetect):
Code:
Start
[debug] Run time error 6: "Invalid instruction"
[debug]  Unknown opcode 0x0 at address 0x00000928
[debug] AMX backtrace:
[debug] #0 00000928 in main () from <unknown>
Script[gamemodes/../test.amx]: Run time error 6: "Invalid instruction"
Number of vehicle models: 0
Some things to note:
  • The #include is needed only for print and printf, not for GetVehicleColor.
  • When I access "vehiclecolor" in "main", is says 'undefined symbol "vehiclecolor"'.
  • If I put #error DEFINED under #if, it correctly outputs the error.
  • If I put #error NOTDEFINED under #else, it also outputs the error.
  • If I add "hello[]" in the parameter list of the second SaveVehicleColor, it outputs "function heading differs from prototype" on that line.
This completely baffles me, is the compiler compiling both blocks, even when there is #else?
Reply
#2

pawn Code:
#include <a_samp>

#if !defined GetVehicleColor
new vehiclecolor[MAX_VEHICLES*2 char];
stock GetVehicleColor(vehicleid, &color1, &color2)
{
    color1 = vehiclecolor{vehicleid};
    color2 = vehiclecolor{vehicleid+1};
}
stock SaveVehicleColor(vehicleid, color1, color2)
{
    vehiclecolor{vehicleid} = color1;
    vehiclecolor{vehicleid+1} = color2;
}
#else
stock SaveVehicleColor(vehicleid, color1, color2)
{
    #pragma unused vehicleid, color1, color2
}
#endif

main()
{
    new c1,c2;
    print("Start");
    GetVehicleColor(0, c1, c2);
    printf("%d %d", c1, c2);
}
The errors are valid because you haven't coded it properly.

You can't access vehiclecolor array because of this
pawn Code:
#if !defined GetVehicleColor
Declare it globaly so that you can still use it even when you have a pre-defined GetVehicleColor.

And why would you even write this:
pawn Code:
stock SaveVehicleColor(vehicleid, color1, color2)
{
    #pragma unused vehicleid, color1, color2
}
If you have unused params, thats just empty!

pawn Code:
main()
{
}
isn't made for those purpose! This seems to work:
pawn Code:
#include <a_samp>

new vehiclecolor[MAX_VEHICLES*2 char] = 0;

stock GetVehicleColor(vehicleid, &color1, &color2)
{
    color1 = vehiclecolor{vehicleid};
    color2 = vehiclecolor{vehicleid+1};
}
stock SaveVehicleColor(vehicleid, color1, color2)
{
    vehiclecolor{vehicleid} = color1;
    vehiclecolor{vehicleid+1} = color2;
}

main(){}

public OnGameModeInit()
{
    new c1,c2;
    print("Start");
    GetVehicleColor(0, c1, c2);
    printf("%d %d", c1, c2);
    return 1;
}
Reply
#3

Your code doesn't work if the GetVehicleColor function is already defined. That was the purpose of my code. The point is, why can I access GetVehicleColor but not vehiclecolor, even if they are defined in the same place?
Reply
#4

Solved it this way:
pawn Code:
#if !defined GetVehicleColor
static vehiclecolor[MAX_VEHICLES][2 char];
stock GetVehicleColorDefault(vehicleid, &color1, &color2)
{
    color1 = vehiclecolor[vehicleid]{0};
    color1 = vehiclecolor[vehicleid]{1};
}
stock SaveVehicleColor(vehicleid, color1, color2)
{
    ChangeVehicleColor(vehicleid, color1, color2);
    vehiclecolor[vehicleid]{0} = color1;
    vehiclecolor[vehicleid]{1} = color2;
}
#define GetVehicleColor GetVehicleColorDefault
#else
stock SaveVehicleColor(vehicleid, color1, color2)
{
    ChangeVehicleColor(vehicleid, color1, color2);
}
#endif
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)