Retrieving an Object Name By Its Alias

Shadecerule
28 Apr 2020, 12:42

Is there a way to retrieve an object name with its alias (or another attribute)?

For instance, I have objects House1 and House2, but they'll each have an alias like "400 70th Street". In my current script, I only know the address (alias) of the house I want to target, so I need to find the house object that has the matching address (alias).


mrangel
28 Apr 2020, 13:11

If there's only 2 possibilities, then you'd want something like:

if (House1.alias = "400 70th Street") {
  house = House1
}
else {
  house = House2
}
// Then you can do stuff with the variable "house"

That's just checking which of them has that alias - and assumes that it will always be one of them.


That's a pretty weird thing to ask for; but that's how you'd go about finding which of a list of objects has a certain alias.

If there's more than 2 objects to check (for example, if you're creating clones), you can loop over all objects in the game until you find one with a certain alias:

houses = FilterByAttribute (AllObjects(), "alias", "400 70th Street")

That will give you a list of all objects that have that alias; and you can then extract one from the list.

If you want to find a visible (to the player) object with a certain alias, it would be:

houses = FilterByAttribute (ScopeVisible(), "alias", "400 70th Street")

Additional note: There are very few situations where looking for an object by alias is a good idea. I've answered the question on how to do it; but there is probably a better way to do it. For example, if dealing with cloned objects, there is an attribute called prototype which contains a reference to the original object, and is faster to check.


Shadecerule
28 Apr 2020, 13:38

Well, I could alternatively use an object list, but it needs to be a permanent list that allows me to add and remove objects. I know they exist but I don't see a way to add an object list to a room/object as an attribute in the UI.

(Also, these objects aren't clones, but some will need to be temporarily removed from the list and re-added later.)


mrangel
28 Apr 2020, 13:47

I think that's probably a weakness in the UI. I'm used to using the web editor, so can't add attributes in the UI; so I have to make do with a script that creates them at the start of the game.