Page 1 of 1

Strings in arrays

PostPosted: Sat Jul 27, 2013 2:07 pm
by Jukeri
I am trying to make a reporting system in our game, that would take random phrases from different arrays setup to store situational responses and reporting (such as loss of something, frontline advance, etc.) and possibly modify parts of them by changing single words from the base phrases so that it doesn't end up being monotone. Some stuff such as names etc would also be stored in these arrays.

I'm having a problem with arrays and strings; I've tried a coupple of methods I googled and dug up from the forum but none seem to work or print out anything. I guess I'm missing out something important in handling string arrays in C and GEscript.

I've tried making the arrays
Code: Select all
const char *cmdnames;[20]
cmdnames[0] = "Lieutenant Powell";
cmdnames[1] = "Sergeant Mason";

And
Code: Select all
char cmdnames[20][15];
strcpy(cmdnames[0], "Lieutenant Powell";
strcpy(cmdnames[1], "Sergeant Mason";


What is a good way to store arrays/print them out later in GEscript? Also, how can I do a console-like printing output (so that it adds a newline then prints a new row of text after the previous row.)

Re: Strings in arrays

PostPosted: Sun Jul 28, 2013 8:33 am
by skydereign
Jukeri wrote:I've tried making the arrays
Code: Select all
const char *cmdnames;[20]
cmdnames[0] = "Lieutenant Powell";
cmdnames[1] = "Sergeant Mason";

And
Code: Select all
char cmdnames[20][15];
strcpy(cmdnames[0], "Lieutenant Powell";
strcpy(cmdnames[1], "Sergeant Mason";

You are setting string arrays properly assuming you meant this by your second code.
Code: Select all
char cmdnames[20][15];
strcpy(cmdnames[0], "Lieutenant Powell)";
strcpy(cmdnames[1], "Sergeant Mason");

Now that variable cmdnames will only exist within the script that code is in. If you want to have it accessible elsewhere you need to declare it as a global variable.
Jukeri wrote:What is a good way to store arrays/print them out later in GEscript? Also, how can I do a console-like printing output (so that it adds a newline then prints a new row of text after the previous row.)

You can display text in gE using text actors. To create a text actor, give a normal actor a font file, and some default text (click the [Text] button in the actor control panel dialog). Then you can use strcpy, or other string functions on that actor's text variable. Due to the nature of the text variable, namely its limit of 255 characters, if you want to create a console like print mechanism I would suggest spawning clones that represent each new line of text.

Re: Strings in arrays

PostPosted: Sun Jul 28, 2013 12:22 pm
by Jukeri
Alright, sounds good, but how do I then use that string stored in an array? It just empties the target string/text object if I try sprintf or strcpy or other command