GML Different Dialogue for Different Instances of the Same Object (RPG) SOLVED

Koruu

Member
For brevity's sake, I have a a player object, a textbox object, and an interact object. I would like to make each instance of obj_interact contain it's own dialogue ( text[1] = "unique dialogue"; ). I have the obj_textbox set up to read obj_interact's text variable, but then I tried setting the variable in the creation code of each instance, and now I'm lost. Is there an easy fix, or am I going to need a new object for each interaction?
 

FrostyCat

Member
Learn the difference between objects and instances.
NEVER access a single instance by object ID if multiple instances of the object exist. This includes attempts to reference or set object.variable (which is inconsistent across exports) and using with (object) to apply actions to it (this encompasses all instances of the object instead of just the one you want). Verbally, "Dog's colour" makes sense with one dog, but not with multiple dogs.
Created or copied instance. instance_create() (GMS 1.x and legacy) / instance_create_layer() and instance_create_depth() (GMS 2.x) and instance_copy() return the ID of the created or copied instance. NEVER use instance_nearest() to establish this relationship --- something else could be closer by.

Subsidiary instance(s). Same as created or copied instance. If the subsidiary instance needs to reference its creator, the creator should assign its instance ID to an instance variable in the subsidiary instance. Conversely, if the creator needs to reference its subsidiary (or subsidiaries), it must store the return value of instance_create() (GMS 1.x and legacy) / instance_create_layer() or instance_create_depth() (GMS 2.x) immediately. NEVER use instance_nearest() to establish or maintain this relationship --- being the closest does NOT imply being the most relevant, especially in close quarters.
If obj_interact is responsible for generating obj_textbox, then you should make it leave its instance ID in the created instance:
Code:
with (instance_create_layer(x, y, layer, obj_textbox)) {
  owner = other.id;
}
If some other source is responsible for generating obj_textbox, find the appropriate function for determining the correct instance ID (e.g. instance_place(x, y, obj_interact) for a colliding instance), and leave that instance ID in the created obj_textbox instance the same way.

In either case, obj_textbox should only attempt to reference owner.text from now on, not obj_interact.text.
 

Koruu

Member
Learn the difference between objects and instances.


If obj_interact is responsible for generating obj_textbox, then you should make it leave its instance ID in the created instance:
Code:
with (instance_create_layer(x, y, layer, obj_textbox)) {
  owner = other.id;
}
If some other source is responsible for generating obj_textbox, find the appropriate function for determining the correct instance ID (e.g. instance_place(x, y, obj_interact) for a colliding instance), and leave that instance ID in the created obj_textbox instance the same way.

In either case, obj_textbox should only attempt to reference owner.text from now on, not obj_interact.text.
What exactly do you mean by "other?"
 

TsukaYuriko

☄️
Forum Staff
Moderator
Literally other, as in the keyword other, which refers to the instance that invoked the with statement from within its body in this case.
 

TheouAegis

Member
It's just for that example. When you use

with instance_create_layer()

It changes the focus of the code to the instance that was created. The variable owner in that coat is just a variable that you create. Since it is within the scope of the with block, that means the variable owner will be an instance variable for that particular instance that was created. And in that example, value of owner is the ID of the instant that created the new instance.
 
Top