strcpy and Multi Dimensional char arrays

Talk about making games.

strcpy and Multi Dimensional char arrays

Postby livens » Tue Feb 14, 2012 11:17 pm

OK,

SO im to the point in my game I need to develop a dialog system for the main character to talk to other actors. I wanted to be able to store all of a actors available dialog in one variable, and easily loop through them with an integer counter.. so I picked a multi dimensional char array :) Also I want the char array to be global.

So I declare the car array in global code:

Code: Select all
char SJdialogList[10][256];


Then in a create actor script where all of my other variables are initialized Im having problems initializing all of the array elements at once, like in this sample code I pulled from Bee-Ant's Dialog Engine demo:

Code: Select all
char DialogList[20][256]= //20 is the max amount of dialog, 256 is the letters max amount
{
    "Hello, my name is Bee-Ant",
    "This is a dialog demo",
    "It will generate random dialog to the screen",
    "Check in the Global code -> Choose -> function\nto see the function to generate the scrolling\ntext effect",
    "Check in the Global code -> Choose -> misc\nto see this dialog list",
    "What's your name?",
    "Oh...\nI have lived here for almost 999 years",
    "Hello, my name is Idk",
    "I'm busy right now...\nA lot of work to do...\nCome again later...",
    "Coding is interesting...\nDo you feel the same thing?",
};

^^^
Thanks Bee-Ant!

Now I could just do each element individually:

Code: Select all
strcpy(SJDialogList[0],
"Doctor, we all seem a bit paler\nthan usual. We should find a nice warm,\n\
sunny planet with nice beaches.");

strcpy(SJDialogList[1],
"Line 2");
...


But can you use strcpy to load up all of them at once, something like:

Code: Select all
strcpy(SJDialogList,"Line 1", "Line 2", "Line 3");


The issue with the individual element solution is having to keep track of the element numbers if things get rearranged.

Thanks,

livens
livens
 
Posts: 13
Joined: Thu Feb 09, 2012 1:34 am
Score: 0 Give a positive score

Re: strcpy and Multi Dimensional char arrays

Postby skydereign » Tue Feb 14, 2012 11:48 pm

Well strcpy can't do that, but you could write a function to use strcpy to do that. Or just have a function that inserts a new line of text into an array of strings you pass it via an index. Something like this.
Global Code
Code: Select all
#define MAX_STRING 256
#define MAX_LINES 10

void
insert_dialog (char strings[][MAX_STRING], char* line, int * count)
{
    if(*count<10)
    {
        strcpy(strings[*count], line);
        (*count)++;
    }
}


That way you can write this in the create actor event.
Code: Select all
int index;
insert_dialog(SJdialogList, "line 1", &index);
insert_dialog(SJdialogList, "line 2", &index);
insert_dialog(SJdialogList, "line 3", &index);
insert_dialog(SJdialogList, "line 4", &index);
// and so on

All this really bypasses is the need to know the exact place of the string you are inserting.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: strcpy and Multi Dimensional char arrays

Postby livens » Wed Feb 15, 2012 3:19 am

Thanks skydereign,

After working through a few ways I trashed the idea. I wanted to keep track of which actor was speaking during the dialog. Next to the dialog window there will be a small window with a portrait of the actor who is speaking. So I went with a struct array that holds the dialog text and an integer that refers to an actor.

Code: Select all
int const thedoctor=0;
int const sarahjane=1;

struct dialogStruct
{
    char dia[256];
    int act;
} sarahJaneDialog[10];


then to initialize:

Code: Select all
strcpy(sarahJaneDialog[0].dia,
"Doctor, we all seem a bit paler\n\
than usual. We should find a nice warm,\n\
sunny planet with lots of beaches.");
sarahJaneDialog[0].act=sarahjane;

strcpy(sarahJaneDialog[1].dia,
"Why, Ive never felt better! Im\n\
at the peak of physical fitness");
sarahJaneDialog[1].act=thedoctor;

strcpy(sarahJaneDialog[2].dia,
"Doctor...");
sarahJaneDialog[2].act=sarahjane;


After that I can use an integer counter to go through each "page" of the struct, and a switch statement to pick which actors image to use. I think the visual aspect of seeing the characters picture is better that reading their names at the beginning of each line of text.
livens
 
Posts: 13
Joined: Thu Feb 09, 2012 1:34 am
Score: 0 Give a positive score

Re: strcpy and Multi Dimensional char arrays

Postby Bee-Ant » Sun Feb 19, 2012 3:26 am

Nice improvements! :D
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest