playMaker

Author Topic: get gameObject ID  (Read 11886 times)

kiriri

  • Hero Member
  • *****
  • Posts: 506
get gameObject ID
« on: May 28, 2012, 09:14:29 AM »
useful if you want to assign any variable to a certain gameObject in a hashtable. Just read out the ID and use it as the key. (EDIT: works only for temporary stuff, since the InstanceIDs change each session)

Quote
using HutongGames.PlayMaker;
using UnityEngine;


[ActionCategory(ActionCategory.GameObject)]
[Tooltip("gets the instance ID of a gameObject")]
public class getGameObjectID : FsmStateAction
{
      [RequiredField]
      public FsmOwnerDefault gameObject;
      [UIHint(UIHint.Variable)]
      public FsmInt storeResult;

   // Code that runs on entering the state.

   public override void OnEnter()
   {
      var go = Fsm.GetOwnerDefaultTarget(gameObject);
      storeResult.Value = go.GetInstanceID();
      Finish();
   }


}
« Last Edit: May 28, 2012, 04:09:44 PM by kiriri »
Best,
Sven

Jos Yule

  • Playmaker Newbie
  • *
  • Posts: 33
Re: get gameObject ID
« Reply #1 on: May 28, 2012, 11:33:50 AM »
I'm a new user of Unity, but i'd just add a quick "might be a problem" FYI:

This probably isn't a good way to store data if you are serializing on shutdown/interruption on a mobile device. I don't know that IDs are deterministic from execution to execution.

Something to test anyway.

Peace
Jos

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: get gameObject ID
« Reply #2 on: May 28, 2012, 12:43:56 PM »
Hi,

 I think you are right, there would be problems if this id was to be used within sessions, it woul dbe fine during runtime ( created and accessed)

http://answers.unity3d.com/questions/8919/how-do-i-serialise-gameobject-references-between-e.html

I went for a hashtable key as a string because else, it would make too long actions UI and it would unworkable... I wish the action ui would be more flexible to allow for such thing, but for now I would suggest the following ( IF you want this to work between sessions of course):

 Maintain a look up table, that is an array of gameObjects you want to reference conveniently as integers, each integers being the index of that gameObject in this array ( so first gameObject would have an index/reference of "0")

Then you can create hashtables of any types and referencing that integer ( that you saved as string before).

 
Bye,

 Jean


kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: get gameObject ID
« Reply #3 on: May 28, 2012, 04:08:49 PM »
ah that's weird, I read somewhere that those IDs are what the editor uses under the hub to identify each object... I figured the engine itself would do that too. Too bad :D

Thanks for the help though, without your note I might have just blundered on and built upon a rotten foundation :S I'll try your way now.

Cheers!
Best,
Sven

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: get gameObject ID
« Reply #4 on: May 28, 2012, 05:39:31 PM »
ok, the final solution I now use is quite simple:
I define an artificial ID for each object by creating a manager object. This manager does nothing more than add 1 to an int and passes that on to the game object that invoked the manager. In short , the first gameObject is 1, the second is 2, and so on. By setting the int to something higher than 0, like 100,000, I have ample space to create my own IDs for some NPCs and fill them manually into the hashtable. This way any important character who is unique has got a very specific ID.
It's basically exactly what jeanfabre proposed, but there's one less problem to worry about: If I were to write all my game Objects to a list and then read out their index , it would all work out just fine... However, this renders me unable to delete any entry to the list, because that would change the index of all subsequent values. And if I can'r delete entries, the list will get longer and longer until looking through it to find the index of a certain gameObject might just hit the performance.

Any flaw in my thoughts? If not so, all of my problems should once again be solved without any complicated solutions.( as if that would ever happen :D )
Best,
Sven

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: get gameObject ID
« Reply #5 on: May 29, 2012, 02:00:20 AM »
Hi,

 No, good catch with this crud of gameObjects. I am not sure in the first place why you need to dereference gameObjects into integers. What is the use case? can you not simply use the gameObject reference itself to make tests? or is it because of the hashtable limitation I made that the key can only be a string?

Bye,

 Jean

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: get gameObject ID
« Reply #6 on: May 29, 2012, 06:29:22 AM »
the thing is, I want to create dynamic NPCs, meaning that their entire behaviour could change under special circumstances. So my default ''unimportant worker'' may trespass on a nobleman's territorry each morning while going to work. Each time the nobleman sees this, he gets this ID of that worker and uses it as a key in his own 'Reputation' hashtable. He then subtracts maybe 10 from the corresponding integer value and compares the result with his behaviour. If the integer is less than 0, he will attack the worker, if it's less than 50, he will get angry, and so on.
Normally I would have just used the gameObject as the key, but since I can only use a string, I have to make sure that each NPC is treated individually. If I were to just use the name as a string, the nobleman would after some time attack each and every worker because they've all got the same name. I haven't tried the path solution but I guess that works only for prefabs and would be a bother to edit by hand.
Best,
Sven

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: get gameObject ID
« Reply #7 on: May 29, 2012, 01:24:29 PM »
Hi,

ok, so I would suggest that since you are controlling and creating all them objects, that you simply rename them uniquely, that I think would solve your problem quite simply. I do have this sort of problematic all the time, and when I create, instanciate or clone a gameObject, I append a unique id to guarantee its uniqueness.


Bye,

 Jean

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: get gameObject ID
« Reply #8 on: May 29, 2012, 03:39:09 PM »
will do, thanks once again for the support :D A unique name may be a step longer, but it'd also be much easier to check in the editor, so I guess I'll lean towards generating a unique ID and adding it to the end of the name.

Incase someone else is having similar problems, here's how I finalized the ID system:
I noticed that unless one is carefull, some objects will get the same ID if they try to get their ID at the same time. To remedy this I've created a second FSM, which checks the state of the ID-manager FSM and passes a request of new gameObject only on, if the ID-manager FSM has reached an idle state. This might be slower, but better save than sorry  ;D
Best,
Sven

Jos Yule

  • Playmaker Newbie
  • *
  • Posts: 33
Re: get gameObject ID
« Reply #9 on: May 30, 2012, 10:32:30 AM »
You might want to look into using C#'s System.GUID class. I don't actually know if it is available to Unity, but if it is, that's what i'd use to generate IDs.

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: get gameObject ID
« Reply #10 on: May 31, 2012, 12:29:21 PM »
awesome, I wrote an action to generate them, I'll add it to the top in a sec. However, they really are a very fake way of creating IDs. Everything they do is create a random 32 character string. Since that string is incredebly long, it's very unlikely that one string might be put out twice.
This is a valid solution if one were to save them. It might be cooler though if I could actually get an ID that works like a permanent pointer (such as every other game engine uses), but this will have to do. Thanks for the tip!

I think I'll continue with the old solution though. This solution will probably work just fine, but I don't think I would be able to remember and find those long strings fast if I had to manually track them... and we all know stuff like that has to happen sooner or later. They are however extremely useful I gather.
« Last Edit: May 31, 2012, 12:40:23 PM by kiriri »
Best,
Sven