Multidimensional Arrays

Game Editor comments and discussion.

Multidimensional Arrays

Postby CrackedP0t » Thu Jan 16, 2014 12:01 am

I'd like to use multidimensional arrays for NPC dialog in my RPG engine. Is this possible, and if so, how? If not, any other ideas for branching NPC dialog?
/人 ◕‿‿◕ 人\
Fang Pictures, my game development company:
http://fangpictures.comuf.com
User avatar
CrackedP0t
 
Posts: 157
Joined: Mon Dec 30, 2013 5:49 pm
Location: Crested Butte, CO (The US of A)
Score: 8 Give a positive score

Re: Multidimensional Arrays

Postby DarkParadox » Thu Jan 16, 2014 3:47 am

It's very possible to do this. There are other options, but a method like this could be the simplest for you.
To create the array you would want, we'd make a two-dimensional CHAR (letter/character) array with the following code:
Code: Select all
char rpg_text[5][256];


Essentially, this code a list of five groups (or lines of text) of 256 characters (which is the max for a single group of CHARs).
You would have to figure out some way to assign text to these lines, of course. Whether through normal script, text files, etc. I'll show you how you might do this in code:
Code: Select all
char rpg_text[5][256];
strcpy(rpg_text[0], "My first line of text");
strcpy(rpg_text[1], "My second line of text");
// and so on ...


Each of the lines of text with the function "strcpy" assign a string of text to one of the ones in our rpg_text array. You may have noticed the first strcpy starts with "rpg_text[0]". If you didn't know already, arrays start at 0 instead of one, so an array with the size of 5 would be from 0 to 4.

To grab these lines of text and display them, we might use a special variable made in the gui, say something named "current_line" as an integer, and we would also have a text actor to display it. The code we would write would look something like this:
Code: Select all
// Assigns the text of the text actor to the line
// number that current_line is at.
strcpy(my_text_actor.text, rpg_text[current_line]);


We would also have to keep track of some method to increase the current line we're at, so maybe in a mouse down event on a "next" button, or a "enter" key down event. But, we can't have the current_line variable go over the amount of lines in our text, so we'd have to check that each time the event happened.
Code: Select all
// It's at 4, remember the size starts a 0
if (current_line < 4)
{
  current_line = current_line + 1;
}


Then you have a basic RPG text system, extremely basic. You could also do choices, maybe. Say, check the number current_line is at and display options, and set the current_line variable based on which choice was made.
In the end, this exact example won't work on a large scale. You'd have to make variables for every line of text the actors say in the game, keep track of dozens upon dozens of variables, and it would just be an awful mess. For something simple though, this might work fine.
To show you something more complex than this, however, would make me write the whole engine myself!
User avatar
DarkParadox
 
Posts: 457
Joined: Mon Jan 08, 2007 11:32 pm
Location: USA, Florida.
Score: 84 Give a positive score

Re: Multidimensional Arrays

Postby CrackedP0t » Fri Jan 17, 2014 2:56 am

Thanks!

What would this do?
Code: Select all
char foo[16][16][16];
/人 ◕‿‿◕ 人\
Fang Pictures, my game development company:
http://fangpictures.comuf.com
User avatar
CrackedP0t
 
Posts: 157
Joined: Mon Dec 30, 2013 5:49 pm
Location: Crested Butte, CO (The US of A)
Score: 8 Give a positive score

Re: Multidimensional Arrays

Postby DarkParadox » Fri Jan 17, 2014 3:40 am

It's a perfectly valid code for what we call a 3D array. You can expand arrays like that as far as you want, but it would get really confusing after a few.

A 2D array could be considered a couple things, like a "list of lists", or a 2D map of objects.
Code: Select all
* Visual Representation of foo[3][3] *
[0] [1] [2]
 V   V   V
[0] [0] [0]
[1] [1] [1]
[2] [2] [2]

A 3D array simply takes this another step to a "list of lists of lists", or a 3D cube of objects. You could think of a simple 3D array, say "foo[3][3][3]" like a Rubiks Cube. A 3D cube seperated into sections, each containing their own piece of data. It'd be good to, for instance, store the data of an image. You can define the size of the image with the first two array sizes ("foo[64][64]" = 64x64 image), and then the 3rd dimension could be a 3-size array containing the red, green, and blue values of each pixel ("foo[64][64][3]").

When you start getting into high-depth arrays like that, though, you'd be better off learning how to use structs to make your code easier to read and write.
User avatar
DarkParadox
 
Posts: 457
Joined: Mon Jan 08, 2007 11:32 pm
Location: USA, Florida.
Score: 84 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest