GML Creating variables for copied instances

R

RisingKane

Guest
Hi everyone!

Let's say I have an object in my room. Then, through some way, I copy this object, creating a new instance of it. I can keep copying it if I want, ending up with multiple instances of that object. What I would like to know is, if it's possible to address a new variable to the copied instance, as soon as it's copied from the original object, and then, through some input text box, modify the value stored in that variable. Also, is there some way to make this work for each copied instance? The final result would be multiple instances of the same object with one variable storing different values for each copied instance.

Thanks!
 
B

Badger

Guest
Have you tried using the "with" function?
like -
Code:
with(newInstance)
{
 newVariable=somethingTotallyUseful
}
 
R

RisingKane

Guest
Have you tried using the "with" function?
like -
Code:
with(newInstance)
{
 newVariable=somethingTotallyUseful
}
Yes, I tried doing that. It works for the first copied instance, but as soon as I copy more, I lose control of them. I'm trying to add the copied instance's IDs to an array or a ds_list, but it's not working. Thanks for your response Badger!
 
B

Badger

Guest
Have you tried using the instance_find function?
You can add id's with that like this -
Code:
var num=instance_number(yourObject);
var i;
for(i=0;i<num;i++;)
{
  var inst=instance_find(yourObject,i);
  with(inst)
  {
  //your code
  }
}
I havent tested that, but if I remember correctly thats how u do it
 
Last edited by a moderator:

Perseus

Not Medusa
Forum Staff
Moderator
It works for the first copied instance, but as soon as I copy more, I lose control of them.
How are you copying the instances? Please post all the relevant code, since that information is not enough to understand what's going on.
 

JackTurbo

Member
When you say copying, do you mean creating? If so just keep a variable with what ever is the newest created object in it as you create them.

Code:
latestInstance = instance_create(x, y, obj);

latestInstance.variable = "what ever you want";
 

FrostyCat

Redemption Seeker
When you say copying, do you mean creating? If so just keep a variable with what ever is the newest created object in it as you create them.

Code:
latestInstance = instance_create(x, y, obj);

latestInstance.variable = "what ever you want";
There is such thing as copying an instance, see instance_copy(). It also returns an instance ID.

Judging from the way the opening post is written, the root of the problem is --- yet again --- ignoring the difference between instances and objects. At least a dozen related topics appear on the Q&A every week because mainstream GML education fails to teach novices the difference.

Have you tried using the instance_find function?
You can add id's with that like this -
Code:
var num=instance_number(yourObject);
var i;
for(i=0;i<num;i++;)
{
  var inst=instance_find(yourObject,i);
  with(inst)
  {
  //your code
  }
}
I havent tested that, but if I remember correctly thats how u do it
It's not what people who know their stuff do. Your instance_find() loop is redundant and slow.
Code:
with (yourObject) {
  //...
}
See this post for why you should never write another loop like that again.
 
Top