[Closed] What are "Names"?
Hi,
I’m writing a script that should react to some events in the scene so I tried to implement some event handlers with the “when” construct.
In order to remove those event handlers, you are able to assign an ID to each handler like this:
when transform $Obj1 change [b]id:#123[/b] do doSomething() -- assigns event handler to Obj1
when transform $Obj2 change[b] id:#123[/b] do doSomething() -- assigns event handler to Obj2
deleteAllChangeHandlers [b]id:#123 [/b] -- removes both event handlers
Now I want each instance of the script to create a new event handler ID to organize it’s own handlers. But that isn’t so easy because the id property is a name literal and not a string. So it is not possible to write
[b]theName[/b] = (random 1 1000) [b]as name[/b]
when transform $Obj1 change id:[b]theName [/b]do doSomething() -- assigns event handler to Obj1
To make it short, I’m looking for a possibility to convert strings (or integers…) to names, or another way to react to an object’s transformations.
Thanks,
Chris
((random 1 1000) as string) as name
Strings CAN be converted to Names and vice-versa.
Integers cannot. So you first convert to string, then to name.
Thank you,
I can assign a variable with a random generated name by
local theID = ((random 1 1000) as string) as name
But when I want to use the variable in the when construct like this:
when transform theObject change id:theID do doSomething()
then I get an Error like “syntax error at name, #name expected”. How can I use the name variable instead of a name literal in the when construct? Is that possible?
Thanks,
Christian
Syntax Error in MAXScript means that the error has been caught by the parser. In other words, you cannot use a variable containing a name in place of the name. What you can probably do is trick the parser by building a string with the command you want to execute, then use the EXECUTE method to parse that string and define the when construct.
theId = (random 1 1000) as string
execute ("when transform theObject change id:#"+theId + " do doSomething()")
Note that theObject must be a GLOBAL variable as execute operates in global scope only.
Same for doSomething() – it must be a global function.
I have no idea why you are trying to do this though.
If you want to monitor objects for transformation changes, you could have a general callback script registered to monitor for selection changes and registering a single when construct for the new selection right after removing the previous when construct that was monitoring the previous selection. This would mean that only one when construct would be defined at any given time, and it would only monitor the current selection.
Another way would be to register a single when construct for ALL OBJECTS that will ever be monitored, and have a general callback watch for new objects being added to the scene to unregister/register the when construct with the correct objects list.
In all these cases, you would need a single ID and a single when construct, plus up to one additional general callback.
Of course, you can do what you want, I am just trying to present some alternatives. I have never ever needed unique random IDs for callbacks myself.
theId = (random 1 1000) as string execute ("when transform theObject change id:#"+theId + " do doSomething()")
Nice trick But I want to have a clearer and more straightforward code at this place. And I want to understand how this event handlers work.
I was going though the MaxScript help and noticed that there are also general methods like callback.addScript or registerTimeCallback to insert own event handlers.
I could go though all objects in the scene and add a “when transform” handler which verifies if the transformed object is relevant. But how can I monitor if there are new objects in the scene to add an event handler?