solved - using a function to designate object for attribute change

not sure how to phrase the question. Essentially, I want a function to determine the current object being affected by an attribute change, but returning the function is invalid as a variable to do so - does anyone know how to make this work, or do I need to do if;then for all possible objects?

In example, the function, let's call it CurrentNpc works like this:

if (ListContains(ScopeReachable(), npc1)) {
  return (npc1)
}

And the attribute change, theoretically, would work like this:

CurrentNpc().attribute = changedattribute

But using CurrentNpc() doesn't work like that, it just throws up an error like this:

Error running script: Invalid variable name 'CurrentNpc().attribute' in 'CurrentNpc().attribute= changedattribute'

Ideally I'd want the function to select the npc, and then the attribute change to affect whichever npc has been chosen by the function, without having to do it individually for each possible npc to be chosen. If it matters, I've tested the function both returning a string containing the name of the object, and returning the object itself, neither works. Cheers for any help.


Gng

Your question is not fully clear for me, but I think it's that you have many NPCs in your world and you want to see if there's one in the player's room that they can interact with:
first I would create an attribute to see if the object is NPC:
In the attribute tab of the object, create the attribute 'NPC', set it to boolean, then set it to true.
then use this code:

non = FilterByAttribute (ScopeReachable(), "NPC", true)
while (ListCount (non) > 0) {
  x = PickOneObject (non)
x.attribute = changedattribute

}

if each NPC has different functions, you can again create the unique attribute for each of them and use FilterByAttribute function to find and designate your object for attribute change.


Yeah, "object.attribute" syntax expects the object name to be a literal.

There are two ways to do this. The first is by using a temporary variable:

npc = CurrentNpc()
npc.attribute = changedattribute

The slightly more efficient option (but I find it less intuitive) is to use the set operator explicitly:

set (CurrentNpc(), "attribute", changedattribute)

set takes three parameters: The object, the name of the attribute, and the value to set.


mrangel, using a temporary variable is exactly what I needed, very helpful as always thank you!


(The set option is also the one you need to use if you have a function returning the name of an attribute, or want the same function to be usable on several different attributes… most commonly comes up in RPG type systems where you want functions to modify the player's statistics, and might have 4-8 statistic attributes all using the same formula)


Log in to post a reply.

Support

Forums