Help with arrays

Game Editor comments and discussion.

Help with arrays

Postby beginnerirvaz » Thu Sep 22, 2011 11:13 pm

hey
I'm having some trouble getting my head around arrays, I have 4 items my player collects. The items are all to restore hp to my player and each add different amounts of hp. At the moment I have separate actor variables all in player - draw actor - script
Item1=0; etc
I'm wondering if using an array would be more efficient as my game will have a lot of other items to collect and use that are related.
If somebody could advise and help how to set the array up that would be great.
Thanks
Irvaz
beginnerirvaz
 
Posts: 37
Joined: Fri Sep 09, 2011 2:02 am
Score: 0 Give a positive score

Re: Help with arrays

Postby skydereign » Thu Sep 22, 2011 11:31 pm

I would use arrays, it makes things easier, and it will be global. One thing about your game, is that you are using actor variables, when actor variables aren't neccesary. You should only use actor variables if you are dealing with clones, or multiple actors will be using their own version of the variable. So, since there is only one player, you have always been needing to do player.Item1, where it would be better to if it were global.

Anyway, declare an array like this.
Global Code
Code: Select all
int items[4];


As most things, it is indexed, 0-3, which means Item1 would be stored in items[0], Item2 in items[1], and so on. Other than declaring them, int arrays act exactly pretty similar to ints. There are a few tricks you can do with them as well. But if you want to add an item3, use this.
Code: Select all
items[2]++;


One thing is you can make a matching array, that holds the amount of hp the items will give.
Global Code
Code: Select all
int items[4];
int items_stats[4] = {10, 20, 30, 40};

So, when you are increasing hp, you can use this.
Code: Select all
if(items[2]>0)
{
    items[2]--;
    player.hp+=item_stats[2];
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help with arrays

Postby beginnerirvaz » Fri Sep 23, 2011 9:53 pm

Hey thanks for that, it's exactly what I needed. Can I ask is it possible to have sub arrays? So if I had an inventory with health items and special items, and say 5 items within each health and special. Is that possible?
Thanks
Irvaz
beginnerirvaz
 
Posts: 37
Joined: Fri Sep 09, 2011 2:02 am
Score: 0 Give a positive score

Re: Help with arrays

Postby skydereign » Sat Sep 24, 2011 12:30 am

You mean two dimensional arrays?
Global Code
Code: Select all
int items[2][5];

// typing item[0] will get you health array
// typing item[1] will get you special array

// typing [0][0] will get you the first health item
// typing [0][1] will get you the second health item
// and so on...
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help with arrays

Postby beginnerirvaz » Sat Sep 24, 2011 12:58 am

I think so... I've noticed a few posts about 2d arrays so I think it's best I give those a read. Thanks.
If I wanted to have a variable called -
Variable1 that equalled the average of my 4 items in array how would I do that?
Thanks
Irvaz
beginnerirvaz
 
Posts: 37
Joined: Fri Sep 09, 2011 2:02 am
Score: 0 Give a positive score

Re: Help with arrays

Postby skydereign » Sat Sep 24, 2011 1:05 am

You can do it with a for loop. This finds the average of the array and puts it in the double average.
Code: Select all
double average = 0;
int i;

for(i=0;i<4;i++)
{
    average+=items[i];
}
average=average/4;
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help with arrays

Postby beginnerirvaz » Sat Sep 24, 2011 1:25 am

I tried making my variable1=average; instead of variable1=100;
I'm guessing it's not as simple as that as this didn't work. I placed the average code in my players create actor along with the item array. I I have a count that I tried to use textNumber = average; as well. Could you advise where I'm going wrong? Thanks a lot
beginnerirvaz
 
Posts: 37
Joined: Fri Sep 09, 2011 2:02 am
Score: 0 Give a positive score

Re: Help with arrays

Postby skydereign » Sat Sep 24, 2011 1:34 am

Not sure what's happening. It works fine for me. One thing though is that I'm using a double for the average, where if you use an int, the precision will be lost. What exactly happens for you?
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help with arrays

Postby CoFFiN6 » Sat Sep 24, 2011 9:30 am

Hey I'm also learning from this post :mrgreen:
Don't run faster then the dragon, run faster than your friends :P
User avatar
CoFFiN6
 
Posts: 49
Joined: Sun Sep 11, 2011 8:17 pm
Location: South Africa
Score: 4 Give a positive score

Re: Help with arrays

Postby beginnerirvaz » Sat Sep 24, 2011 2:16 pm

I probably should of explained this better. Sorry.
I have an array with 4 health counts on screen, and a 5th count that I want to display an average of the 4. My array is in my players create actor and script and in my 5th count I tried to use
textNumber = average; but the count shows 0 even though the 4 counts from my array all start on 100 each so the average should be 100.
I also want to base the players death on the 5 th count so once it hits 0 it's destroyed.
Hope that makes sense.
Thanks
Irvaz
beginnerirvaz
 
Posts: 37
Joined: Fri Sep 09, 2011 2:02 am
Score: 0 Give a positive score

Re: Help with arrays

Postby skydereign » Sat Sep 24, 2011 10:42 pm

This should work. Stick it into a text actor and you'll need to remove the first line of code and switch array to your array. But left by itself the code will find the average of the first four numbers in the array, and set the text to it (remembering of course that it will return an int. You can insert your destroy code after that as well.
Code: Select all
int array[5] = {100, 100, 100, 100, 0};
int i;

for(i=0;i<4;i++)
{
    array[4]+=array[i];
}
array[4] = array[4]/4;
textNumber = array[4];
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest