Page 1 of 1

Finding simObjects by name, from script.

Posted: Tue Nov 28, 2017 6:15 pm
by chriscalef
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??

Code: Select all

DefineEngineFunction(findSimObjectByName, S32, (const char *objName), , "") { SimObject *simObj = NULL; simObj = Sim::findObject(objName); if (simObj) return simObj->getId(); else 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. :-)

Re: Finding simObjects by name, from script.

Posted: Wed Nov 29, 2017 12:27 am
by Jason Campbell
What about findObjectByInternalName()? Every SimObject would have to have an internal name but it can search SimGroups and SimSets. Think it is game.cs where this would go.

http://docs.garagegames.com/torque-3d/r ... imSet.html

Re: Finding simObjects by name, from script.

Posted: Wed Nov 29, 2017 4:25 am
by chriscalef
Well, yeah I've been using that for a while, but it just seems silly not to have a script way to find any object in the scene by its declared name. I wonder if simManager.cpp would be a reasonable place to put it, if it actually isn't already somewhere.

Re: Finding simObjects by name, from script.

Posted: Wed Nov 29, 2017 11:02 am
by OTHGMars
In that case:

Code: Select all

%name = "SomeName"; %objID = findSimObjectByName (%name);
would be functionally equivalent to:

Code: Select all

%name = "SomeName"; %objID = isObject(%name) ? %name.getId() : 0;
or, if you want to restrict it to being a member of a particular SimSet.

Code: Select all

%name = "SomeName"; %objID = SomeSet.isMember(%name) ? %name.getId() : 0;
Is there a particular case where a dedicated function would be preferable?

Re: Finding simObjects by name, from script.

Posted: Wed Nov 29, 2017 3:49 pm
by JeffR
Yeah, @
User avatar
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.

Re: Finding simObjects by name, from script.

Posted: Thu Nov 30, 2017 3:33 am
by chriscalef
Ah, right, duh! That makes perfect sense. Thank you @
User avatar
OTHGMars
@
User avatar
JeffR

Re: Finding simObjects by name, from script.

Posted: Sat Dec 02, 2017 2:52 am
by JeffR
Any time :)