weird issue with arrays?

Talk about making games.

weird issue with arrays?

Postby Fojam » Sun May 06, 2012 7:44 pm

i am having a really weird problem. for some reason, whenever i try to access what an array has stored, it just ends the code completely. for example, i have this:
Code: Select all
loadVars("temp", "temp");

sprintf(helper.text, "%i%i%i%i", winners[1],winners[2],winners[3],winners[4]);

amountChar=charAmount;
amountStock=stockAmount;
charNo=selectedChar;
charNo2=selectedChar2;
charNo3=selectedChar3;
charNo4=selectedChar4;
modeGame=gameMode;
typeGame=gameType;
limitTime=timeLimit;

switch(amountChar)
{
    case 4:
    CreateActor("player", "none", "(none)", "(none)", -160, -20, true);
    CreateActor("player", "none", "(none)", "(none)", -50, -20, true);
    CreateActor("player", "none", "(none)", "(none)", 50, -20, true);
    CreateActor("player", "none", "(none)", "(none)", 160, -20, true);
    break;
 
    case 3:
    CreateActor("player", "none", "(none)", "(none)", -160, -20, true);
    CreateActor("player", "none", "(none)", "(none)", -50, -20, true);
    CreateActor("player", "none", "(none)", "(none)", 50, -20, true);
    DestroyActor("playerIndicator.3");
    DestroyActor("rank.3");
    break;
 
    case 2:
    CreateActor("player", "none", "(none)", "(none)", -160, -20, true);
    CreateActor("player", "none", "(none)", "(none)", -50, -20, true);
    DestroyActor("playerIndicator.3");
    DestroyActor("rank.3");
    DestroyActor("playerIndicator.2");
    DestroyActor("rank.2");
    break;
}

if(winners[0]==1)
{
    Actor*rank3Actor=getclone("rank.3");
    strcpy(rank3Actor->text, "3rd Place");
}

charAmount=2;
stockAmount=3;
selectedChar=0;
selectedChar2=0;
selectedChar3=0;
selectedChar4=0;
gameMode=0;
gameType=0;
timeLimit=2;

saveVars("temp", "temp");


for some reason, since i added the sprintf thing, the code comes to the sprintf line, and doesnt execute it or anything after it. I dont know why its doing this but this only happens when i try to access array values.

Is this happening to anyone else?
CLICK TO GIVE ME POINTS

My Latest Projects:
Super Smash Bros: viewtopic.php?f=6&t=12307 PLEASE help by making sprites!
User avatar
Fojam
 
Posts: 513
Joined: Thu Mar 19, 2009 10:02 pm
Location: under your bed!!!
Score: 69 Give a positive score

Re: weird issue with arrays?

Postby skydereign » Sun May 06, 2012 7:47 pm

I assume the array has a size of 4, in which case your problem is that you are indexing them wrong. Array indexes start from 0. So change 1-4 in your code to 0-3. Accessing outside of the array bounds does cause the game to exit.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: weird issue with arrays?

Postby Fojam » Sun May 06, 2012 7:49 pm

wait so if I have nothing stored in winners[0], it will just stop the game altogether?
CLICK TO GIVE ME POINTS

My Latest Projects:
Super Smash Bros: viewtopic.php?f=6&t=12307 PLEASE help by making sprites!
User avatar
Fojam
 
Posts: 513
Joined: Thu Mar 19, 2009 10:02 pm
Location: under your bed!!!
Score: 69 Give a positive score

Re: weird issue with arrays?

Postby Fojam » Sun May 06, 2012 7:50 pm

wait nevermind i get it. ill just change the size to 5, because Im using winners[0]
CLICK TO GIVE ME POINTS

My Latest Projects:
Super Smash Bros: viewtopic.php?f=6&t=12307 PLEASE help by making sprites!
User avatar
Fojam
 
Posts: 513
Joined: Thu Mar 19, 2009 10:02 pm
Location: under your bed!!!
Score: 69 Give a positive score

Re: weird issue with arrays?

Postby skydereign » Sun May 06, 2012 7:52 pm

This is the line that is probably causing it to crash.
Code: Select all
sprintf(helper.text, "%i%i%i%i", winners[1],winners[2],winners[3],winners[4]);

It should be this (since your array is size 4).
Code: Select all
sprintf(helper.text, "%i%i%i%i", winners[0],winners[1],winners[2],winners[3]);
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: weird issue with arrays?

Postby Fojam » Sun May 06, 2012 7:56 pm

i know arrays start at 0, i just thought then when i defined the size as 4, it would make values for 0-4 not 0-3

it should have an error that tells you when youre outside an array
CLICK TO GIVE ME POINTS

My Latest Projects:
Super Smash Bros: viewtopic.php?f=6&t=12307 PLEASE help by making sprites!
User avatar
Fojam
 
Posts: 513
Joined: Thu Mar 19, 2009 10:02 pm
Location: under your bed!!!
Score: 69 Give a positive score

Re: weird issue with arrays?

Postby skydereign » Sun May 06, 2012 8:00 pm

Oh, I must have read that wrong. It normally does tell you you accessed memory outside the array bounds. But, I have noticed sometimes it doesn't. Not really sure why that happens.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest