GMS 2.3+ Same object, different messages

Elicarus

Member
Hello, in the game I'm creating, the player goes in a bedroom where a lot of papers are pinned on the wall, and everyone has a different message written on it.
In order to do that, I only created 4 objects, for the 4 sprites (4 different types of papers pinned, to have some variation), but i want each one of the paper to show a different message. I am wondering if there is a way to have different messages and sprite ofr the instances in the room, but all linked to only one object (or 4, if it is impossible to change one sprite without changing them all), or if I have to create an different object for each paper to make all of them say something different.
Looking for an answer 😅
Thank you very much!
 

samspade

Member
You can definitely have different instances of the same object say different things. How depends upon a couple other factors. For example, if you place these objects in the room editor, then the simplest way is to have an object variable (created with the variable tab, not in the create event) and to then set that to be the message you want directly in the room editor.

If you are doing it through code, then you would still have a variable, although this time probably in the create event, that you set immediately after creating the instance. For example:

GML:
with (instance_create_layer(x, y, layer, obj_message)) {
    my_message = //some message;
}
 

Elicarus

Member
It looks like what i want, but can you explain this code please? On the documentation they say the last parameter has to be an object, is 'obj_message' an object? And what does the "with" do? Does it specify that the following code will only apply to the instance? I feel like i understand what you say but i can't think of a way to use it context. Thank you :)
 

samspade

Member
It looks like what i want, but can you explain this code please? On the documentation they say the last parameter has to be an object, is 'obj_message' an object? And what does the "with" do? Does it specify that the following code will only apply to the instance? I feel like i understand what you say but i can't think of a way to use it context. Thank you :)
I just made up an object name, so obj_message would be whatever you named the object. Instance_create_layer returns the instance id of instance. With switches the scope of the code to the scope of the instance assuming you provided it with an instance id (if you provide it an object id it will loop through all objects applying that code to all of them. So the code I provided creates an instance of obj_message and then switches the scope to that instances and sets a variable in that instance to be whatever you provide.

There are a lot of concepts packed in there, so it might be worth looking into a couple of these topics in a more general way as well.
 

ParodyKnaveBob

The Laughing Rogue
Howdy, Elicarus, and welcome to the GMC. $:^ J

"What samspade said." You can totally do what you want with multiple instances of one single object resource. (Objects are just blueprints or recipes; instances are the chairs or cakes.) To further some input, you read how the instance create function worked, great, but did you read about the with construction? Fyi, many people on the forum will offer example code or even pseudocode using made-up resource names; you'll do well to read obj_, spr_, rm_, etc. names abstractly -- and sometimes with a grain of salt. $;^ ]

Likewise, depending how the game is set up, you can definitely vary the sprite assigned to each paper on the wall. If you only see them once, you could randomize it with choose(), or you can give the human touch and set each one's sprite in the room editor via instance creation code, which would also look consistent if you can re-enter the bedroom.

I hope this helps,
 

Elicarus

Member
There are a lot of concepts packed in there, so it might be worth looking into a couple of these topics in a more general way as well.
I already did some programming, but it was in python, which is very far from gms from what i can see; so I can understand almost everything you said, but still I have a question : if that line of code creates an instance, does it create only one instance? Because i want ~20 papers pinned on the wall, will i have to do that "with" line 20 times, one for each? and if so, how can i put the instance in the room at the place i want it to be?
 

Elicarus

Member
Howdy, Elicarus, and welcome to the GMC. $:^ J

"What samspade said." You can totally do what you want with multiple instances of one single object resource. (Objects are just blueprints or recipes; instances are the chairs or cakes.) To further some input, you read how the instance create function worked, great, but did you read about the with construction? Fyi, many people on the forum will offer example code or even pseudocode using made-up resource names; you'll do well to read obj_, spr_, rm_, etc. names abstractly -- and sometimes with a grain of salt. $;^ ]

Likewise, depending how the game is set up, you can definitely vary the sprite assigned to each paper on the wall. If you only see them once, you could randomize it with choose(), or you can give the human touch and set each one's sprite in the room editor via instance creation code, which would also look consistent if you can re-enter the bedroom.

I hope this helps,
I knew about the made names, but i didn't understand why "message" was in that made up name 😅
What I want is a way to assign to each one of the instances a string, and have a way to know which instance the player is in front of, in order to have coherence : one paper = one text
I will read that link, thank you!

Update : When i click on an instance from the room editor, there is a "creation code" button, i think it's what i'm looking for !
Update 2.0 : That creation code isnt helping : the variables from there cant be used in the script
 
Last edited:

samspade

Member
I knew about the made names, but i didn't understand why "message" was in that made up name 😅
What I want is a way to assign to each one of the instances a string, and have a way to know which instance the player is in front of, in order to have coherence : one paper = one text
I will read that link, thank you!

Update : When i click on an instance from the room editor, there is a "creation code" button, i think it's what i'm looking for !
I would not use creation code. I would use the object variables instead. They are nice because you can edit them in the IDE, set defaults, inherit them, and so on.
 

ParodyKnaveBob

The Laughing Rogue
Yeah, sorry, my advice comes from breathing whiffs of GMS2 scents as I pass by to my table where I eat nothing but GM Legacy. $:^ ] From the manual (which I just read thanks to Mr. ♠):

you can also modify these variables for individual instances that have been placed in the room.
I had no idea that object variables opened the door to room-editor instance variables, too. Wow, that's cool stuff. Makes me really wish I could feasibly get GMS2.
 
Last edited:

Fielmann

Member
if that line of code creates an instance, does it create only one instance?
Yes.
Because i want ~20 papers pinned on the wall, will i have to do that "with" line 20 times, one for each?
You use a loop. Something like this:

GML:
messages = ["first message", "second message", "third message", "and so on"];

var length = array_length(messages);

for (var i = 0; i < length; ++i) {

    with (instance_create_layer(x, y, layer, obj_message)) {

        my_message = messages[i];

    }

}
how can i put the instance in the room at the place i want it to be?
By modifying the first two arguments of the instance_create_layer() function. For example this will put them in a row, 20px from each other:
GML:
instance_create_layer(20*i, y, layer, obj_message)

This will put them in random places:
GML:
instance_create_layer(random_range(0, room_width), random_range(0, room_height), layer, obj_message)

This will put them in places the coordinates of which you have saved in arrays:
GML:
instance_create_layer(xcoordinates[i], ycoordinates[i], layer, obj_message)
 
If you are working mostly in the room editor, you could declare their values right in there.
You would have the advantage of a visual feedback of what it looks like without needing to run your game, and you can adjust it all in there.
Not really suited for big stuff, but perfect for a couple of posters or menu buttons, for example!
You ultimately would only need ONE object (obj_poster) and ONE sprite (spr_poster), but really, at this stage, however you want to do it is fine as long as it works and gets you hooked on game dev!

This is how you set your variables in the editor.
GameMaker Studio 2 Manual (yoyogames.com)
 

Elicarus

Member
I still don't understand where my message variable should be. In the code of the instance? In the create event of the object? Object variables don't seem different to variables i could create in the "create" event. Is there any "hashing tables", in GMS? or just any way i could make separate cases for when a player comes near to different instances of the same object? I'm sorry, but I can't see how i could use object variables, nor the "with", in that way...
 

Elicarus

Member
Yes.
You use a loop. Something like this:

GML:
messages = ["first message", "second message", "third message", "and so on"];

var length = array_length(messages);

for (var i = 0; i < length; ++i) {

    with (instance_create_layer(x, y, layer, obj_message)) {

        my_message = messages[i];

    }

}

By modifying the first two arguments of the instance_create_layer() function. For example this will put them in a row, 20px from each other:
GML:
instance_create_layer(20*i, y, layer, obj_message)

This will put them in random places:
GML:
instance_create_layer(random_range(0, room_width), random_range(0, room_height), layer, obj_message)

This will put them in places the coordinates of which you have saved in arrays:
GML:
instance_create_layer(xcoordinates[i], ycoordinates[i], layer, obj_message)
Okay, this seems very useful! But i have to questions : where do I put this code? and what kind of object is "object_message" supposed to represent? The original paper object?
 
I still don't understand where my message variable should be. In the code of the instance? In the create event of the object? Object variables don't seem different to variables i could create in the "create" event. Is there any "hashing tables", in GMS? or just any way i could make separate cases for when a player comes near to different instances of the same object? I'm sorry, but I can't see how i could use object variables, nor the "with", in that way...
Sorry, I used my current project and not a fresh one!
If you see, I create a new varible to the object called poster_label, and then I assign the value "Motorherad" for the first instance and "Megadeth" to the second. It's that simple.
 

Fielmann

Member
where do I put this code?
This code should run when you want the instances to be created. So for example if you want the instances to be created when the player presses a button, you would put it in the button's Mouse Left Pressed event. Or if you want the instances to be there right from the start, you can put them in the Create event of a controller object.

what kind of object is "object_message" supposed to represent? The original paper object?
Yes.
 
Top