PDA

View Full Version : sending a command to a function?


Hobbs
11-03-2009, 10:16 PM
I find myself using the command "for obj in $selection do" alot in the listener, is there a way to turn that into a function, just for speed?

For example, would something like this be possible?

fn fois command =
(
for obj in $selection do command
)

fois (obj.pos = [0,0,0])


i know if i try this now, it returns obj undefined..obviously because obj is not defined.

Thanks

ehulser
11-03-2009, 11:38 PM
that in itself wouldnt work...

I would recommend just writing out the loop personally - the overhead of writing that out is pretty minimal, but if you really wanted to go the method way, the two things you could do are:


fn fois command = ( for obj in $selection do (command obj) )

function someaction obj = ( obj.pos = [0,0,0] )
function someotheraction obj = (obj.pos = [10,10,10] )

fois someaction
fois someotheraction


or if thats even too much, do it as an execute


fn fois command = (
for o in $selection do (
global obj = o
execute command
)
global obj = undefined
)

fois "obj.pos = [0,0,0]"


I still wouldn't really recommend either way though...

Hobbs
11-04-2009, 04:22 PM
The bottom one was more of what i was looking for since my command would change...I was going more for a "strategy pattern" style.

Oh well, thanks.

ehulser
11-05-2009, 07:13 PM
just learned about this today for another problem, but thought of this post...

you should probably use a mapped function, seems like what its designed for:


mapped function commandA obj = ( obj.pos = [0,0,0] )
mapped function commandB obj = ( obj.pos = [10,10,10] )

commandA $selection
commandB $selection

CGTalk Moderation
11-05-2009, 07:13 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.