View Full Version : Best way to force arbitrary code execution order? A -> B -> C
thatoneguy 03-20-2009, 06:00 PM If you have three functions
fn A = (print "Hello")
fn B = (print "Goodbye")
fn C = (print "World")
And I have a string. Let's say
ExecOrder = "ABC" or "CBA"
What's the best way to execute the functions in the defined order? Just loop through with an if else statement? Is there a better way? I guess it's not a bad way. But I haven't really done it before so I don't know if I've come up with the right solution.
for i = 1 to ExecOrder.count do
(
if ExecOrder.i == "A" then A
else if ExecOrder.i == "B" then B
else if ExecOrder.i == "C" then C
else ()
)
|
|
Ye good ol' optimizing thread, alright I'll chip in.
for exe in ExecOrder do execute exe
Don't know if you loop a lot of times, but above seems pretty clean and readable, provided the functions are in the same scope and the functions will be processed in a global scope.
you could also use for collect to get the output of the functions that ran.
-Johan
ZeBoxx2
03-20-2009, 06:19 PM
that's an odd little piece of code there...
regardless, I'd probably use something like this...
fn a = print "Hello"
fn b = print "World"
fn c = print "of MaxScript"
myStr = "bca"
for i = 1 to myStr.count do (
funcStr = myStr[i]
case funcStr of (
"a": ( a() )
"b": ( b() )
"c": ( c() )
)
)
Or, if the functions all take the same parameters, just a different handling...
fn a = print "Hello"
fn b = print "World"
fn c = print "of MaxScript"
myStr = "bca"
for i = 1 to myStr.count do (
funcStr = myStr[i]
theFunc = case funcStr of (
"a": ( a )
"b": ( b )
"c": ( c )
)
theFunc()
)
ZeBoxx2
03-20-2009, 06:23 PM
for exe in ExecOrder do execute exe
That would (almost*) work if the function names really are a/b/c and are defined as such.. ouch, though :\
( * can't map a string; wish we could! )
It could work with more complex examples using filterString, perhaps...
fn hello = print "hello"
fn world = print "world"
fn ofMaxscript = print "of MaxScript"
myStr = "world|ofMaxscript|hello"
for str in (filterString myStr "|") do ( execute (str + "()"))
still seems scary though
We are maxscripters we live by the fears of doing edgy stuff all day long, week after week. Stuff breaks we fix it.... I broke it, you fixed it... :P
I'm somehow a sucker for execute like functionality.. dunno why really...
-Johan
thatoneguy
03-20-2009, 06:48 PM
Hehe because the documentation says to be weary of using it except in special circumstances. We all think we are the need for 'special circumstances'. :D
thatoneguy
03-20-2009, 06:49 PM
Case. I like it. I always forget about case.
CGTalk Moderation
03-20-2009, 06:49 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2013, Jelsoft Enterprises Ltd.