Notifications
Clear all

[Closed] Create a when-changes handler inside a struct function

Hi!
In my script I have an array of struct:


struct boneInfo (
                      boneObject,
                      parentBone,
                      parentGuide,
                      callback,
                      fn RegisterCallback = (
                       callback= when transform parentGuide changes do
                                      boneObject.position = parentGuide.position
                      )

Basically, I’d like to have something similar to the Bone Edit mode: elsewhere in my script I’m filling a global array of boneInfo structs, one for each bone of a skeleton. The parentGuide objects are simple meshes corresponding the the joints. I’d like to press a check button, set affectchildren=false, register the handlers for all the bones, move my guides and make the bones follow them.
But this doesn’t work.
As soon I move a guide and its position changes, I got “Struct member access requires instance: parentGuide” error.
I’m quite stuck and have no idea of what to do

Any help will be most welcome

thanks
Daniele

1 Reply

If you are wanting to modify a variable in the struct, you need to make an instance of the struct once it is created. This is effectively like creating a class definition and then instancing it.

--Define the struct:
Struct MyStruct (
	MyVariable = #()
)
-- Instance the Struct:
MyStructInstance = MyStruct()
--then use MyStructInstance  in your callback.

-- If you want to safely self reference the struct from functions within the struct itself then replacing the struct name with the instance is the way to go ( this is the best way to replicate the .self functionality that most other languages have ):
Struct MyStruct (
	MyVariable = #(),
	Fn Function1 =(
		Print( Hello )
	),
	Fn Function2 =(
		MyStruct.Function1()
	)
)
-- Replace reference with the instance:
MyStruct = MyStruct()