Omnimaga
Calculator Community => TI Calculators => Calculator C => Topic started by: epic7 on December 24, 2012, 10:02:06 pm
-
I'm experiencing some weird error... and my best guess would be that these two arrays are messing up each other.
So I have two portions of code.
This one:
sprintf(recipes[18].name, "Stock ");
recipes[18].power = 0;
And this one
int invmenu[] ={352, 352, 352, 352, 352, 352, 352, 352, 352, 352, //352 == bedrock
352, COAL, IRON, GOLD, PLATINUM, SAPPHIRE, EMERALD, RUBY, DIAMOND, 352,
352, 0, 0, 0, 0, 0, 0, 0, 0, 352, //0 == stone
352, 0, 0, AIR, 0, 0, 0, 0, 0, 352,
352, 0, 0, AIR, 0, 0, 0, 0, 0, 352,
352, 0, 0, AIR, 0, 0, 0, 0, 0, 352,
352, 352, 352, 352, 352, 352, 352, 352, 352, 352};
I then display the invmenu array (which contains tilemap data), and then output values from the recipe array.
for(ly = 0; ly <= 7; ly++)
{
for(lx = 0; lx <= 10; lx++)
{
tile.x = lx*32;
tile.y = ly*32+16;
tile.offset_x = invmenu[ly*10+lx];
if(tile.offset_x == AIR)
{
//stuff that replaces air with the "STOCK" tile
}
drawImageSubrect(&tile, s);
tile.offset_y = 0;
}
}
sprintf(GUI, "%sDrill", recipes[18].name);
drawStr(133, 116, GUI, 1, 1, green, s);
The result is this:
(https://dl.dropbox.com/u/93019118/recipes1.png)
The invmenu[] data is fine, but the data from recipes[] is messed up.
Now when I put the portion of code with invmenu[] before the part with recipes[], it displays this:
(https://dl.dropbox.com/u/93019118/inv1.png)
So this time, the recipes[] data outputs fine, but invmenu[] is messed up.
So it seems that one array messes up the other...
When the part with invmenu[] comes first, only recipes[] works.
When the part with recipes[] comes first, only invmenu[] works :P
Commenting out the code between the assigning and the output doesn't get rid of this weird effect...
-
for(ly = 0; ly <= 7; ly++)
{
for(lx = 0; lx <= 10; lx++)
The last run would be: ly = 7, lx = 10.
invmenu[ly*10+lx] = invmenu[80].
In Java you would get an ArrayOutOfBoundsException :D
-
Yep, shouldn't that be for(lx = 0; lx < 10; lx++) ? :P
-
Doesn't fix any problems, but nice catch nevertheless :P
-
Try to allocate them dynamically (malloc).
Does it make any differences if you allocate invmenu before recipes and vice versa?
-
How do I do that with arrays?
Sorry, I dove into making this game without that much C knowledge :P
-
You can declare a pointer variable, then assign the return value from malloc to that variable.
int* inventory=malloc(70*sizeof(int));
-
Finally got around to trying this :P
I put invmenu first, and then stored it into a pointer var, and that worked :D