• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Arrays to draw Text

L

LaLaurien

Guest
Hello everyone,

I am currently working on a Point and Click Adventure in GM2.
As I am still new to GML I am having quite a few problems with the coding language.
Some of you might know, that clicking on an item usually triggers a text in Point and Clicks.
At the moment I coded it this way:
The item (obj_item) has a mouse left clicked event:
instance_create_depth (x,y,depth, text_item)
The text_item object now has a draw event:
draw_text (x,y @"This is an item");
The object also has an alarm, which makes the text disappear afer a certain time.
Create event of text_item:
alarm[0] = 240;
Alarm event:
instance_destroy();

Now I have already added all items to the game, which are quite a lot.
And since I trigger an individual text item for each item, this causes me to have
hundreds of text objects. All just containing one difference:
the description in the string in the draw_text event.

Now i thought I could just make ONE object, which contains all explanations.
However, I have no idea how to code this logicially.
I thought about using an array, but I just now learned about their existence and
still dont quite understand how an array can help me here.
How does the item draw the individual text?
Doesnt a draw event always trigger as soon as the object is created?

Thank you for your help beforehand!
 

Xer0botXer0

Senpai
Think of a variable as a box, a container, to hold one item, of some data type..
Think of a 1D array as a row of variable boxes all connected together by the array name, each box contains an item as well. And you can easily find the cat in box 10 with arrays.

Think of a 2D array a row of boxes, with smaller boxes inside of them, you can easily find the sock in box 5s littlebox 3.

And the arrays just get bigger from there.

You could use a 1D array or a list, and just reference the first dimension of the array or the point on the list.
GML:
in obj_easier create event:
arr_descriptions[0] = "this is a box";
arr_descriptions[1] = "this is a potato";
arr_descriptions[2] = "etc";
that's the simplest way to create and populate a 1d array.

then you can do
draw_text (x,y, obj_easier.arr_descriptions[1]);
in the potato objects draw event within a click container.

I believe that's what you're asking for, although I don't see how it's much easier. It seems like the same amount of work to me.
But maybe nice to have all the descriptions in one place.
 
L

LaLaurien

Guest
Think of a variable as a box, a container, to hold one item, of some data type..
Think of a 1D array as a row of variable boxes all connected together by the array name, each box contains an item as well. And you can easily find the cat in box 10 with arrays.

Think of a 2D array a row of boxes, with smaller boxes inside of them, you can easily find the sock in box 5s littlebox 3.

And the arrays just get bigger from there.

You could use a 1D array or a list, and just reference the first dimension of the array or the point on the list.
GML:
in obj_easier create event:
arr_descriptions[0] = "this is a box";
arr_descriptions[1] = "this is a potato";
arr_descriptions[2] = "etc";
that's the simplest way to create and populate a 1d array.

then you can do
draw_text (x,y, obj_easier.arr_descriptions[1]);
in the potato objects draw event within a click container.

I believe that's what you're asking for, although I don't see how it's much easier. It seems like the same amount of work to me.
But maybe nice to have all the descriptions in one place.

Okay currently this is what I have
in the create event:
count = -1
texts = array_length_1d(texts);
texts[0] = "Its locked. Obviously"; //Car description

this would be the draw event:
draw_text (x,y, texts[count]);

but when I try it I get an Error when I want to start the Game:

Variable obj_AllTextObject.texts(100187, -2147483648) not set before reading it.
at gml_Object_obj_AllTextObject_Create_0 (line 3) - texts = array_length_1d(texts);
gml_Object_obj_AllTextObject_Create_0 (line 3)
 

TsukaYuriko

☄️
Forum Staff
Moderator
texts = array_length_1d(texts);
This line doesn't make much sense - you're setting texts to the length of texts - so, itself - but you haven't even created it yet. Hence the error, as that variable has not been set/created/declared before you read from it (in the call to array_length_1d).

Are you thinking that array_length_1d will create a 1d array? If so, that's incorrect - you'd have to use array_create for that.
 
P

ParodyKnaveBob

Guest
Howdy, LaLaurien, and welcome to the GMC. $:^ ]

Fwiw, an array (or a more specific data structure) might be your best bet, or since I expect you're lovingly placing each and every object's instance into the room editor, let me suggest object variables. In short, you define (let's say) a string variable, item_text, in the object, perhaps with a blank default value (or perhaps with an ugly "THERE IS NO SPOON." if that gets your attention better). Then, in your organic imagery in the room editor where you (have) place(d) all your many object instances, you can scratch that, replace the many objects with one object, make lots of instances of that one object, (all as you desire,) and then tell the instance you want to override the item_text with the appropriate message; you should also be able to set a separate sprite for each instance this way.

I hope this helps,
 
Top