Getting id from obj_instance.var

ZeDuval

Member
Hey guys. Second time I would really need this. I can't possibly come up with anything new I could try.

If I do this:
Code:
instance_id_ref = instance_create(0,0,obj_whatever);
instance_id_ref.some_variable = 123;
Is it somehow possible to get the id of the instance that "owns" the variable some_variable if I only have 'instance_id_ref.some_variable'. Any way to "split" this again to get the value before the dot?

edit: In my obj.Method asset I use a technique where I actually store the value of the variable AND the id of the instance using an array, but this won't work with the stuff I'm working on right now.

edit2: Ok, I will probably get it to work somehow using the array approach, but I really want to know this. I hope someone knows! :D
 
Last edited:

FrostyCat

Redemption Seeker
Why does it have to be an array? Can't you use maps instead?

If you genuinely have no other choice, use a with block to search through the instances.
Code:
var inst_with_ref = noone,
    target_ref = 123;
with (obj_whatever) {
  if (some_variable == target_ref) {
    inst_with_ref = id;
    break;
  }
}
 

jazzzar

Member
You created the instance (instance_id_ref) you can get it's id by simply doing :
Code:
instance_id_ref.id
I hope that's what you mean
 
It doesn't make any sense to me. Why don't you just store the instance id, from which you can always get "some_variable" at any time if you need it?

If all you have is the value of "some_variable" and you need to for some reason get the instance id out of that, and I can't really see a reason why this would be necessary, you could encode the instance id into the value of "some_variable". For instance, if you know the real value of "some_variable" will always be a smallish number (less than 1000), you could do this:

instance_id_ref.some_variable = instance_id_ref * 1000 + 123;

you could then extract the instance id out of it by using this:

some_id = some_variable div 1000;

and extract the real value:

some_value = some_variable - some_id * 1000;
 

Weird Dragon

Wizard
GMC Elder
I am not sure I understand your question.
If you have 'instance_id_ref.some_variable' then the value of the part before the dot 'instance_id_ref' will be instance id.

***
EDIT: I can see that there are already other replies which weren't there when I replied (I didn't update the screen before replying, and some time went from the time I read the question and until I replied).
 
Last edited:

ZeDuval

Member
Hello again and sorry for the late reply. At first, a general answer to the question "Why the **** do you ask for this stuff?" since you all seem rather confused about it.
I'm building a lot of extensions/functions at the moment and the goal is always to build them with a few requirements in mind.

- The usage should be as intuitive as possible
- The dirty stuff should happen behind the curtain
- The functionality should be achieved with blocking as few as possible argument"slots"
- Get the job done with as few as possible of the following things: controler-objects, global vars, ds_maps/lists,grids or anything that could lead to memory leaks

That doesn't mean that I never use anything of those but if there is an alternative...

Let's take my obj.Method extension as an example. The reference to a method is actually something like hero.Move, mirroring real methods from javascript for example. This alone should be enough as an argument to a function call. That's what I wanted to achieve. Since there seems to be no way of getting the id of the var-owning instance, I save the reference to id and user_event/script to the var. It works, sure. But if there would have been a secret and hidden function I missed to find, it would have been even better.

Thank you guys anyway because I learned something new here from some of your posts! I mark this thread as solved since you delivered some possible alternatives to my method!
 
Top