The Magic of Arrays

You must understand the Game Editor concepts, before post here.

The Magic of Arrays

Postby GeoGia » Wed Nov 04, 2009 1:48 am

Hi everybody :D

I'm new to these here parts, but I like the attitude of this community, you really seem to care about games, like I do :) I haven't had much of a chance to use GE, but I was reading some forum posts, and Feral's tutorial about arrays inspired me. I started to think about what you could do with nothing but arrays... some of this might not apply to GE, and I'm sure some of you are already miles past this point, but hopefully it'll at least get some people thinkin' :)





Arrays are useful for organizing information when you have a large number of objects. For example, you could use them to keep track of health for all the monsters on a level:

char monsterhealth[20]

Now whenever you want to modify an individual monster's health, just do this:

monsterhealth[12]-=4

Now let's say we're working on an even more complicated game. We want to keep track of a whole range of stats for each of those 20 monsters. We could use arrays for this:

char monsteratk[20]
char monsterdef[20]
char monsteragi[20]

but as you can see this is getting a little disorganized. Let's use a multi-dimensional array instead

char monsterstats[20,4]

20 monsters, with 4 stats each.

Now we can actually do something quite clever to keep track of what's what.

#define health=1
#define atk=2
#define def=3 'When you use #define, the compiler doesn't use any
#define agi=4 'memory, it just replaces these with hard code

Now when we want to modify them:

monsterstats[19,health]-=6

Monster number 19 loses 6 health. Simple, eh?

What if we wanted to have different types of monsters, each with their own set of stats? Well, let's make an array where we define the stats of each type so we can look them up when we need them

char monster_stats[4,4]

That's 4 types of monsters with 4 stats each, and we can set them up like this:

#define goblin=1

monster_stats[goblin,health]=10
monster_stats[goblin,atk]=3
monster_stats[goblin,def]=4
monster_stats[goblin,agi]=1 '(goblins are slow)

Isn't that cool? Just like a class!

Now we need an array like our first one, so we can control individual monsters

int monster_control[20,7]

The extra cells are for:

#define type=5
#define xcrd=6 'Likely to go above 255, hence the int
#define ycrd=7

The type and coordinates can be manipulated from the map editor, just keep track of what monster number you're on (not sure about this for GE)

Now when we're setting up a level, we just copy the values from "stats" into "control" depending on the type

for monstnum=1 to 20
_type=monster_control[monstnum,type]
if _type<>0
for _stat=1 to 4
monster_control[monstnum,_stat]=monster_stats[_type,_stat]
next
next

Now we have a nice array loaded full of bloodthirsty monsters ready to fight! Enjoy!

GeoGia

Note that in practice, arrays start at 0, so for 20 cells you actually type 19. I just find it easier to understand this way

All of the arrays should be unsigned, except the control, which has to account for negative health, I just left them out for simplicity
You don't know what you can do unless you've already done it
GeoGia
 
Posts: 19
Joined: Wed Nov 04, 2009 1:30 am
Score: 2 Give a positive score

Re: The Magic of Arrays

Postby makslane » Wed Nov 04, 2009 1:58 am

Thanks and welcome to the Game Editor forum!
Game Editor is an open source game creator software that's wants to pay it's developers to keep evolving.
If you like Game Editor, make a review!
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Re: The Magic of Arrays

Postby GeoGia » Wed Nov 04, 2009 7:05 pm

Thanks, glad to be here :)
You don't know what you can do unless you've already done it
GeoGia
 
Posts: 19
Joined: Wed Nov 04, 2009 1:30 am
Score: 2 Give a positive score

Re: The Magic of Arrays

Postby Hblade » Mon Apr 05, 2010 7:38 pm

Yeah hey nice tutorial! Thanks allot :D I posted one too in General but mine's not as advanced as yours :D
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest