INI stock causes "Unknown Command" message
#1

I've made a command to save tuning. It used to work quite decently, but after some changes, it stopped working altogether... Apparently, this bit causes problems.
Код:
stock SaveTuningToFile(playerid) {
	new INI:CarFile = INI_Open(CarPath(playerid));
	INI_SetTag(CarFile,"data");
	INI_WriteInt(CarFile,"Spoiler",PrivCarInfo[playerid][cspoiler]);
	INI_WriteInt(CarFile,"Fbumper",PrivCarInfo[playerid][cfbumper]);
	INI_WriteInt(CarFile,"Rbumper",PrivCarInfo[playerid][crbumper]);
	INI_WriteInt(CarFile,"Exhaust",PrivCarInfo[playerid][cexhaust]);
	INI_WriteInt(CarFile,"Bventr",PrivCarInfo[playerid][cbventr]);
	INI_WriteInt(CarFile,"Bventl",PrivCarInfo[playerid][cbventl]);
	INI_WriteInt(CarFile,"Bscoop",PrivCarInfo[playerid][cbscoop]);
	INI_WriteInt(CarFile,"Rscoop",PrivCarInfo[playerid][crscoop]);
	INI_WriteInt(CarFile,"Lskirt",PrivCarInfo[playerid][clskirt]);
	INI_WriteInt(CarFile,"Rskirt",PrivCarInfo[playerid][crskirt]);
	INI_WriteInt(CarFile,"Hydraulics",PrivCarInfo[playerid][chydraulics]);
	INI_WriteInt(CarFile,"Bass",PrivCarInfo[playerid][cbass]);
	INI_WriteInt(CarFile,"Rbbars",PrivCarInfo[playerid][crbbars]);
	INI_WriteInt(CarFile,"Fbbars",PrivCarInfo[playerid][cfbbars]);
	INI_WriteInt(CarFile,"Wheels",PrivCarInfo[playerid][cwheels]);
	INI_WriteInt(CarFile,"Lights",PrivCarInfo[playerid][clights]);
	INI_WriteInt(CarFile,"Paintjob",PrivCarInfo[playerid][cpaintjob]);
	INI_WriteInt(CarFile,"CarColor1",PrivCarInfo[playerid][cColor1]);
	INI_WriteInt(CarFile,"CarColor2",PrivCarInfo[playerid][cColor2]);
	INI_WriteInt(CarFile,"AirColor1",PrivCarInfo[playerid][aColor1]);
	INI_WriteInt(CarFile,"AirColor2",PrivCarInfo[playerid][aColor2]);
	INI_WriteInt(CarFile,"BoatColor1",PrivCarInfo[playerid][bColor1]);
	INI_WriteInt(CarFile,"BoatColor2",PrivCarInfo[playerid][bColor2]);
	INI_Close(CarFile);
}
CarPath:
Код:
#define CAR_PATH "Cars/%s.ini"

stock CarPath(playerid) {
    new string[128],playername[MAX_PLAYER_NAME];
    GetPlayerName(playerid,playername,sizeof(playername));
    format(string,sizeof(string),CAR_PATH,playername);
    return string;
}
The "Cars" folder is where it should be. Let me know if you need any additional info.
Reply
#2

It would be good if to see the actual command. Also, this seems to be an issue either with underflow/overflow or not returning or returning 0 within the command.
Reply
#3

I really, really doubt the command itself is of any importance here. Earlier I've put some Client Messages in it, before and after the "suspicious" lines, and it seemed that nothing was executed after calling SaveTuningToFile. But anyway, here it is:

Код:
CMD:savetuning(playerid,params[]) {
	new vehicleid=GetPlayerVehicleID(playerid);
	if(CooldownInfo[playerid][cSAVETUNING]>GetUnixTimePlus(0)) {
		new message[128];
		format(message,sizeof(message),"You will be able to use this command in %ds.",CooldownInfo[playerid][cSAVETUNING]-GetUnixTimePlus(0));
		SendClientMessage(playerid,COLOR_ERR_COOLDOWN,message);
	}
	else if(vehicleid==PrivCarInfo[playerid][carID]) {
                SaveTuningToFile(playerid);
		CooldownInfo[playerid][cSAVETUNING]=GetUnixTimePlus(10);
		SendClientMessage(playerid,COLOR_CMD_SUCCESS,"Tuning has been saved!");
	}
	else {
		SendClientMessage(playerid,COLOR_ERR_USAGE,"You're not in your private vehicle.");
	}
	return 1;
}
And here - https://pastebin.com/b3C8ksKS - is my data structure and the functions I use to temporarily save player's car components (until they save them with command), which doesn't seem to work either. I forgot to mention there, "LoadCar" gets called on player logging in. Underflow/overflow sounds possible, but what could be causing it?
Reply
#4

Quote:
Originally Posted by Nyitz
Посмотреть сообщение
Earlier I've put some Client Messages in it, before and after the "suspicious" lines, and it seemed that nothing was executed after calling SaveTuningToFile.
Right, In that case, try using print() within 'SaveTuningToFile' between lines, try to find where exactly in that function it fails. I suspect it is something to do with overflow/underflow in PrivCarInfo.

Try using crashdetect plugin to get more information of what's exactly going wrong.


I know this is irrelevant but the 'SaveTuning' could be considerably more optimised by only using one loop and if statements instead of however many loops you have there.

And why are you creating arrays of size of 1 like this one? new spoiler[20][0];? That is just wasteful and shouldn't be done, as you can easily do 'new spoiler[20];' which will do the same thing, except not wasting memory. if(componentid == spoiler[s][0]) would turn into if(componentid == spoiler[s]).
Reply
#5

Add a check that creates this file if it doesn't exists, this must be located right under the saving function.
Reply
#6

Quote:
Originally Posted by Logic_
Посмотреть сообщение
Add a check that creates this file if it doesn't exists, this must be located right under the saving function.
That is not the case with Y_INI, and if it was, it would crash the server instead of terminating the function.
Reply
#7

Quote:
I know this is irrelevant but the 'SaveTuning' could be considerably more optimised by only using one loop and if statements instead of however many loops you have there.

And why are you creating arrays of size of 1 like this one? new spoiler[20][0];? That is just wasteful and shouldn't be done, as you can easily do 'new spoiler[20];' which will do the same thing, except not wasting memory. if(componentid == spoiler[s][0]) would turn into if(componentid == spoiler[s]).


I know it's annoying to look at, but let's not focus on the small things just yet. Those loops are copied straight from the Wiki tutorial and I didn't bother to optimize them for my code so far (but I surely will). Thank you anyway.

Off-topic:
Quote:

That is just wasteful

Is it really? In both cases the array has 20 elements, so the size should be equal (but I'm aware that this "method" is still completely illogical ).
/Off-topic

I did the other things though and got something interesting. The stock stops working on line 'INI_Close(CarFile);'. Here's the debug info from crash detect: https://pastebin.com/itAqD7Rm

Where should I start looking for the out-of-bounds array? I figured it could be in those badly written loops.
Reply
#8

Quote:
Originally Posted by Nyitz
Посмотреть сообщение
Is it really? In both cases the array has 20 elements, so the size should be equal (but I'm aware that this "method" is still completely illogical ).
Afaik, it is wasteful, because you're creating an array of 2 dimensions, instead of 1, therefore using 64 bits of memory instead of 32.

Are you sure it is at INI_Close where it stops working? If it is, that's rather odd and shouldn't really happen. Are you using the latest Y_INI?

Код:
[17:02:55] [debug] Run time error 4: "Array index out of bounds"
[17:02:55] [debug]  Attempted to read/write array element at negative index -1
That tells me exactly what I was suspecting (underflow). Try printing the CarFile variable (before closing it) and see what it returns.
Код:
printf("CarFile - %i",_:Carfile);
Try commenting out parts of the script, try to narrow down where the issue might be.
Reply
#9

Quote:

Are you using the latest Y_INI?

Oops...

But it wasn't that either. I've made a lot of mistakes with this command, but I think the crucial one was forgetting to close the file after using it in other functions. Now it properly saves to file, I'm gonna test it more thoroughly but I think we can consider it fixed.

For anyone in the future running into the same problem, here's more complete list of things I've done:
1. Updated Y_INI include.
2. Installed Crash Detect plugin to determine whether the issues were caused by runtime error (they were).
3. Corrected some name inconsistencies (I've renamed "base" component to "bass" earlier, and didn't change it in some places in my code).
4. Optimized some parts of my tuning saving system (less code and smarter code makes it easier to find the problematic parts).
5. Made sure I've closed the file everywhere in the code.

Thank you ThA_Devil, I really appreciate your help. I'm glad we managed to track this issue down
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)