Jump to content

Finding simObjects by name, from script.


Recommended Posts

So, I recently wrote an engine function to do this, but felt awfully stupid because there *has* to be a way to do this already, right??


DefineEngineFunction(findSimObjectByName, S32, (const char *objName), , "")
SimObject *simObj = NULL;
simObj = Sim::findObject(objName);

if (simObj)
	return simObj->getId();
	return 0;


I looked around in simSet and SimGroup but couldn't find anything. In case this is actually lacking in the engine, could someone give me a better idea of where it should live? I know where I put it is the wrong place, that's all I know. :-)

Link to comment
Share on other sites

In that case:

%name = "SomeName";
%objID = findSimObjectByName (%name);


would be functionally equivalent to:

%name = "SomeName";
%objID = isObject(%name) ? %name.getId() : 0;


or, if you want to restrict it to being a member of a particular SimSet.

%name = "SomeName";
%objID = SomeSet.isMember(%name) ? %name.getId() : 0;

Is there a particular case where a dedicated function would be preferable?

Link to comment
Share on other sites

Yeah, @OTHGMars is correct. Technically the name ITSELF is a reference in script. Unlike in C++ where you need to actually talk to the console to find an object by name, the name itself is a reference to the object. If you have the object, and the object exists, you're done.

If your object is called TheThing in the level, then if you know the object's name (TheThing) then accessing it is as simple as using it's name TheThing.variable = 5;

If you're unsure if it exists yet, then you can use isObject(TheThing) to validate that an object with that name exists first.

Having said that, There could indeed be use in a function that lets you search partial names and get back an object, or a list of objects. That could be pretty handy.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...