Variable reference passing

Variable reference passing

Anonymous
Not applicable
634 Views
2 Replies
Message 1 of 3

Variable reference passing

Anonymous
Not applicable

I'm suffering.

I need to pass a reference to the material parameter to another function that'll change it(actually, it changes parts of matrix3 parameter, but that's not the point).

 

Reference passed value throws an error in line 2:

-- Unknown property: "shaderType" in (prop shaderType Local:result)

 

Here's the test code:

fn setvalue val = (
	*val = 666
)
fn createmtl = (
	local	Result = (Standard()),
			shaderType = (&Result.shaderType)
	--setvalue (&Result.shaderType) --this works...
	setvalue shaderType --but i REALLY need to pass the parameter using the local variable!
	Result.shaderType
)
createmtl()

Seems like it'll work only if Result's a global variable...I don't like that way. 

Any help???

0 Likes
635 Views
2 Replies
Replies (2)
Message 2 of 3

Swordslayer
Advisor
Advisor

I would like to actually know what you are using it for and why you want to keep it as a local; there might be much simpler approach - here, of course, it would be:

 

fn setValue &val =
val = 666 fn createmtl = ( local Result = Standard() setValue &Result.shaderType Result.shaderType ) createmtl()

or passing the result material itself but as you've already said, this is not the code you are using it in. If you cannot show the code, at least describe it or tell us what advantages you want to get using referencing/dereferencing here.

0 Likes
Message 3 of 3

Anonymous
Not applicable

I'm using setvalue function to change material matrix3 parameter components, because they won't change in usual way.

Here's the code that explains why I've tried to use referencing:

plugin	Material	TestMtl	ClassID:#(1954676047, 1613573786) (
	parameters Params (
		Data	type:#matrix3	Default:(Matrix3 [0, 0, 0] [0, 0, 0] [0, 0, 0] [0, 0, 0])
		on Data set NewVal do
			Format "<Material event> Data set %\n" NewVal
	)
	rollout ui "ui" (
	)
)
fn CreateMaterial = (
	Format "Create new material\n"
	Mtl = (TestMtl())
)
fn CheckData Mtl =(
	Format "Data check: %\n\n"(
		if Mtl.Data[1][1] == 666 then
			"Success!"
		else
			"FAIL!"
	)
)
(
	ClearListener()
	local	Mtl
		
	Mtl = (CreateMaterial())
	Format "Trying to set directly\n"
	Mtl.Data[1][1] = 666
	CheckData Mtl
	
	Mtl = (CreateMaterial())
	Format "Trying to update 1st matrix row\n"
	Mtl.Data[1] = [666, Mtl.Data[1].y, Mtl.Data[1].z]
	CheckData Mtl
		
	Mtl = (CreateMaterial())
	Format "Trying to set Mtl.Data using new Matrix3\n"
	Mtl.Data = (Matrix3 [666, Mtl.Data[1].y, Mtl.Data[1].z] Mtl.Data[2] Mtl.Data[3] Mtl.Data[4]) --works, but loks like sh*t and very hard t write and read
	CheckData Mtl
		
	OK
)

In fact, I've already rewritten my code...I can't use smth like (&Mtl.BlaBlaData) because parameter names can vary in SomeDataChanger function:

 

plugin	Material	TestMtl	ClassID:#(1954676047, 1613573786) (
	parameters Params (
		Data	type:#matrix3	Default:(Matrix3 [0, 0, 0] [0, 0, 0] [0, 0, 0] [0, 0, 0])
	)
	rollout ui "ui" (
	)
)

fn UpdateMatrix Mtx Coords Data = (
	local	UpdatePointer = (IsKindOf Mtx ValueRef),
			UpdateCoord = (IsKindOf Coords Point2),
			TargetMtx = (
				if UpdatePointer then
					(*Mtx)
				else
					Mtx
			),
			ElementID = (
				if UpdateCoord then
					Coords.x
				else
					Coords
			),
			Element
	if UpdateCoord then (
		Element = TargetMtx[ElementID]
		Element[Coords.y] = Data
	) else
		Element = Data
	TargetMtx[ElementID] = Element
	if UpdatePointer then
		*Mtx = TargetMtx
	OK
)

fn SomeDataChanger = (
	local	PropertyName = #Data,
			Mtl = (TestMtl())
				
	Print #AfterCreate --test
	Print Mtl.Data --test
				
	Print #AfterSet --test
	local	Mtx = (GetProperty Mtl PropertyName)
	UpdateMatrix Mtx [2, 2] 666 --can use (&Mtl) too
	SetProperty Mtl PropertyName Mtx
				
	Print Mtl.Data --test
	OK
)

ClearListener()
SomeDataChanger()

I'm clearly sure that the line with GetProperty generates new Matrix3 value, consuming extra memory. Am I right?

 

Just a little test:

fn getref ref = (
	Print ref
)
fn checkrefs = (
	local	Mtl = (standard()),
			ShaderType = (&Mtl.ShaderType)
	getref (&Mtl.ShaderType)
	getref ShaderType
	OK
)
ClearListener()
checkrefs()

Listener output:

&(prop shaderType Local:mtl)
(prop shaderType Local:mtl)

Passed ShaderType variable appears to point on the local variable after passing into another function, not the material property...

It all still looks like a workaround, not like a clean code must be.

0 Likes