SA-MP Forums Archive
Weird behavior of #if, invalid instruction - 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: Weird behavior of #if, invalid instruction (/showthread.php?tid=574225)



Weird behavior of #if, invalid instruction - IllidanS4 - 14.05.2015

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:This completely baffles me, is the compiler compiling both blocks, even when there is #else?


Re: Weird behavior of #if, invalid instruction - Gammix - 15.05.2015

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;
}



Re: Weird behavior of #if, invalid instruction - IllidanS4 - 15.05.2015

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?


Re: Weird behavior of #if, invalid instruction - IllidanS4 - 30.05.2015

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