1 registered members (TipmyPip),
679
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
[Solved] Displaying an array of strings.
#456791
12/10/15 02:17
12/10/15 02:17
|
Joined: Nov 2014
Posts: 24 ME
Hawthourne
OP
Newbie
|
OP
Newbie
Joined: Nov 2014
Posts: 24
ME
|
Greetings, I have been exploring arrays a bit in the context of making an inventory. It seems to me that a relatively simple way to do this is by building a string array and then displaying it, most likely with a text box. The gamestudio manual, however, is fairly scarce on its treatment of String arrays, just making some mention of how they are similar to variable arrays.
With this in mind, I tried to directly define an array using STRING* filler[100]. The script is accepts this notation, but when I try to access an entry of it like I would a variable array (arrayname[entrynumber]) I get a launch error. The only message I am given is a ^before the name of the array entry I was trying to pull.
Am I not able to directly define an array like this, rather I have to manually enter in the various string into a text and use that as the array? My current project is displaying a series of variables, so I was going to translate each entry of the variable array over to an entry of the string array. Is there a better path to take?
Bonus: Also, I placed a vec_set(textname.blue,vector(100,100,100)); into my main function to change the text's color, but it remained white. Any ideas what is up with that?
Thanks for any assistance.
Last edited by Hawthourne; 12/10/15 22:58.
|
|
|
Re: Displaying an array of strings.
[Re: Hawthourne]
#456793
12/10/15 04:51
12/10/15 04:51
|
Malice
Unregistered
|
Malice
Unregistered
|
God wishes lite-c work in this c++ way
STRING* filler[100]
TEXT* OBJECT TO DISPLAY STRINGS <--- See manual
usually provide links but the bottles got me...
fucntion this()
{
int arrayi[100];
.... syntex fills array
int i=0;
for(i=0;i<100;i++)
{
str_for_num(filler[i],arrayi[i]
}
}
I am Mal Peace love and fun programming
Last edited by Malice; 12/10/15 04:54.
|
|
|
Re: Displaying an array of strings.
[Re: ]
#456794
12/10/15 04:52
12/10/15 04:52
|
Malice
Unregistered
|
Malice
Unregistered
|
Rest of your post is lost to my drunkenness... Thnk night
|
|
|
Re: Displaying an array of strings.
[Re: ]
#456797
12/10/15 06:31
12/10/15 06:31
|
Joined: Apr 2015
Posts: 20 Vietnam
Florastamine
Newbie
|
Newbie
Joined: Apr 2015
Posts: 20
Vietnam
|
You have to replace STRING *filler[] with a pointer to pointer:
#include <acknex.h>
#define N 10
int main(void)
{
STRING **array = (STRING *) sys_malloc(sizeof(STRING) * N);
int i = 0;
for(; i < N; i++)
array[i] = str_create(str_printf(NULL, "This is string #%i", i));
for(i = 0; i < N; i++)
printf("%s", _chr(array[i]));
return 0;
}
Another way would be using TEXT. Also, I placed a vec_set(textname.blue,vector(100,100,100)); into my main function to change the text's color, but it remained white. Any ideas what is up with that? You have to "tell" Acknex that the text object can be colored, by passing an additional "LIGHT" flag:
#include <acknex.h>
TEXT *text =
{
strings = 1;
string("Hello!");
font = "Arial#25b";
flags = SHOW | LIGHT;
}
int main(void)
{
random_seed(0);
while( !key_esc )
{
vec_set(text.blue,vector(random(255.0), random(255.0), random(255.0)));
wait( -0.5 );
}
return 0;
}
Last edited by Florastamine; 12/10/15 06:32. Reason: wrong tag >.<
|
|
|
Re: Displaying an array of strings.
[Re: Florastamine]
#456800
12/10/15 08:02
12/10/15 08:02
|
Joined: Jan 2006
Posts: 968
EpsiloN
User
|
User
Joined: Jan 2006
Posts: 968
|
You will display this text, so why not use a text directly... Define a text and follow the manual examples:
TEXT* inventory_text =
{
layer = 1;
pos_x = 10;
pos_y = 10;
strings = 100;
flags = LIGHT | SHOW;
}
Now, to access individual strings from this text use:
(inventory_text.pstring)[0]
And I suggest never try to modify this directly. Always use str_cpy or you can get in a lot of trouble without understanding where your error is
str_cpy((inventory_text.pstring)[0],"Short sword");
...
str_cpy((inventory_text.pstring)[1],axeName_str);
You have to replace STRING *filler[] with a pointer to pointer: Your answer is great, but... If he's new to this, I doubt he'll understand what a pointer to pointer is I'm not new, but I just scratched the surface of C++, so even I get confused sometimes...
|
|
|
Re: Displaying an array of strings.
[Re: Hawthourne]
#456818
12/11/15 05:52
12/11/15 05:52
|
Malice
Unregistered
|
Malice
Unregistered
|
I also loled at Malice's reply, I am quite ridiculous, for sure! If he's new to this, I doubt he'll understand what a pointer to pointer is laugh I'm not new, but I just scratched the surface of C++, so even I get confused sometimes... In this usage, it's bit complex for more reasons then pointer to a pointer. STRING **array = (STRING *) sys_malloc(sizeof(STRING) * N); However the idea of pointer to pointer is not difficult. As In player=my; when written clean in lite-c, is simply a pointer-to-a-pointer, not a copy of the object. An address to a address, but more a copy of a memory address. But perhaps, my understanding is just poor, in this matter and C++. Mal
Last edited by Malice; 12/11/15 05:53.
|
|
|
Re: Displaying an array of strings.
[Re: Florastamine]
#456830
12/11/15 16:26
12/11/15 16:26
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
STRING **array = (STRING *) sys_malloc(sizeof(STRING) * N);
This code line is wrong. It allocates N times the length of a STRING struct! It should be:
STRING **array = (STRING**) sys_malloc(sizeof(STRING*) * N);
that allocates N times the length of a pointer to a STRING struct. Salud!
|
|
|
Re: Displaying an array of strings.
[Re: txesmi]
#456837
12/12/15 04:18
12/12/15 04:18
|
Joined: Apr 2015
Posts: 20 Vietnam
Florastamine
Newbie
|
Newbie
Joined: Apr 2015
Posts: 20
Vietnam
|
STRING **array = (STRING *) sys_malloc(sizeof(STRING) * N);
This code line is wrong. It allocates N times the length of a STRING struct! It should be:
STRING **array = (STRING**) sys_malloc(sizeof(STRING*) * N);
that allocates N times the length of a pointer to a STRING struct. Salud! Yeah, I forgot about the extra asterisk! Because it's a pointer to pointer, you should allocate memory for the pointers. Thanks for pointing it out!
Last edited by Florastamine; 12/12/15 04:19.
|
|
|
Re: Displaying an array of strings.
[Re: Florastamine]
#458825
04/01/16 07:16
04/01/16 07:16
|
Joined: Jun 2010
Posts: 212
tagimbul
Member
|
Member
Joined: Jun 2010
Posts: 212
|
is this correct?
STRING **array = (STRING**) sys_malloc(sizeof(STRING*) * N);
...
array[1] = "hello";
can i use array with [] ?
|
|
|
|