PDA

View Full Version : Incrementing referenced variables


stuh505
10-19-2005, 01:10 AM
Although max script uses pass by reference by default, when you change the data at a reference it automatically creates a new variable to change...instead of changing the data you were originally referencing.

Is there a clean way to get around this? A lot of times I am passing a value to a function that I specifically want that function to change...if I am just doing this with 1 variable at a time than I could easily have the function return the result, but I need to be able to be changing many variables in one function, and returning an array with all the new values is plain ugly!

There seems to be at least one workaround, which is to package all the values that will change into an array, send this array to the function, because as a compound variable maxscript does not do its "smart stuff"...then after returning from the function, iterate through the array and overwrite all the original data values...but this is hardly better than returning an array (or Point2/Point3).

So hard to write clean code :(

magicm
10-19-2005, 01:45 AM
I hope I haven't misunderstood your question, but this is what I usually do when a function needs to return multiple values: (see the "by-reference parameter passing" topic in the online help)

(

fn testfn &a &b &c &d =
(
a = 1
b += 5
c += " world"
insertitem 3 d 3
)

local e
local f = 5
local g = "hello"
local h = #(1, 2, 4)

format "Before :\n\te = %\n\tf = %\n\tg = %\n\th = %\n" e f g h

testfn &e &f &g &h

format "After :\n\te = %\n\tf = %\n\tg = %\n\th = %\n" e f g h

)

Cheers,
Martijn

stuh505
10-19-2005, 03:26 AM
Oh, thanks a lot Martijn!

I was thinking to myself, "why can't it just use the regular pass by reference operator?"

I just assumed that the operator didn't exist since everythign is already passed by reference...haha

Bobo
10-19-2005, 02:21 PM
Oh, thanks a lot Martijn!

I was thinking to myself, "why can't it just use the regular pass by reference operator?"

I just assumed that the operator didn't exist since everythign is already passed by reference...haha

What do you mean with "everything"?
Function arguments are not passed by reference by default, unless they are arrays.

stuh505
10-19-2005, 02:56 PM
Function arguments are not passed by reference by default, unless they are arrays.

Yeah they are. But if you change the value of a referenced variable that's not a composite value then it automatically creates a new variable and stores a reference to that under the variable name you were changing. This makes it appear to the user that it was passed by value.

"in all cases the reference is assigned or passed."

"If you assign a variable's value to another variable, the reference to that value's memory is placed in the new variable. Thus, both variables are actually referencing the exact same value. If you assign a new value to one of the variables, that variable will contain a reference to the new value, and the other variable will contain a reference to the old value. Normally this is the desired behavior, as you don't want both variables to reference the new value. However, a complication occurs if a compound value, like a point3 or array, is referenced by more than one variable, and you assign a new value to a component of the compound value. When you assign a value to a component of a compound value, a new compound value is not created. Therefore any variables that reference the compound value will still point to the same value, and will "see" the changed component value. "

CGTalk Moderation
10-19-2005, 02:56 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.