Page 1 of 1

array value setting

PostPosted: Sat Jan 19, 2013 4:27 pm
by Soullem
Is there anyway to write a switch statement for all parts of an array at once? like can I do something like
Code: Select all
switch (pMOVE1[any real number])
{
//set values of attack
}

instead of having to do each one separately like

Code: Select all
switch (pMOVE1[0])
{
//set values
}
switch (pMOVE1[1])
{
}
//......

that would be impossible for my current project.
Also since I have four different attacks per monster how would I setup a global two part array where the values correspond to pMOVE[monsternumber][attacknumber].
thanks.

Re: array value setting

PostPosted: Sat Jan 19, 2013 6:31 pm
by skydereign
A switch statement can only take one value at a time. But, you can put a switch statement in a loop.
Code: Select all
int i;
int array[10];

for(i=0;i<10;i++)
{
    switch(array[i])
    {
        default:
        break;
    }
}

Re: array value setting

PostPosted: Sat Jan 19, 2013 6:53 pm
by Soullem
oh wow, that is actually pretty smart, and very simple. Thanks for the wonderfully easy genius idea. Also how would I make one array have two parts? So instead of pATK1[i], pATK2[i] i can do pATK[j][i]. But it has to be able to be accessed globally?

Thank you so much!

Re: array value setting

PostPosted: Sat Jan 19, 2013 6:57 pm
by skydereign
You mean a multidimensional array? The idea is also simple, you write it as an array of arrays.
Code: Select all
int array[5][2];

Looping through an array like that is pretty simple as well. Just put a loop within a loop.

Re: array value setting

PostPosted: Sat Jan 19, 2013 7:33 pm
by Soullem
and then I can access the array from anywhere? wow, that would seem to be too easy.

Re: array value setting

PostPosted: Sat Jan 19, 2013 7:35 pm
by skydereign
Soullem wrote:and then I can access the array from anywhere? wow, that would seem to be too easy.

Do you understand scope? If you declared the integer within an event, it would only exist within the event. If you declared it within a function, it would only exist in that function. Now if you declare it in global code, then it will exist globally. That is pretty much all there is to it. It being an array doesn't change that it is a variable.

Re: array value setting

PostPosted: Sat Jan 19, 2013 11:22 pm
by Soullem
No I hadn't heard of that but it makes perfect sense. Thanks a lot for the new information, now I can continue to make progress on my game.