A "not-modal" rollout declared inside a struct cannot access owner's private members!


#1

Hi guys… I have a maxscript issue but believe me… I really don’t know what’s happening here!!!
Refactoring my scripts I’m trying different ways to do stuff and I’m applying (hoping in the proper way) some hints by our “MAX Doctor” @denisT about rollouts and structs.
My problem is that a rollout opened with modal:true works properly… but if opened withmodal:false (or without it) fails to access a private member of the owner struct :tired_face:
And the “fun fact” is that it fails only inside events like the on button pressed or on dropdownlist selected!
If you try to access the private member for example inside the on MainUI open block it’s ok…
I wrote an example script… its structure is exactly the same of mine, only simpler.
The button and the dropdown list both read the content of the private array _List and the events simply print the value.

Here the working snippet…


global myStruct
(
	struct myStruct
	(
		private
			_self = myStruct,
			_List = #("AAA","BBB","CCC","DDD","EEE"),
		
		public
			fn CreateNew = _self(),
			mainUI =
			(
				rollout mainUI "Rollout" width:300 height:200
				(
					local owner = if owner != undefined do owner
					button 'btn_1' "Btn" width:100 height:30 align:#center
					dropdownList 'ddl_List' "Account" align:#center
					label 'lbl_item' "--ITEM--" align:#center
					
					on mainUI open do
					(
						ddl_List.items = owner._List
					)
					
					on btn_1 pressed do
					(
						print owner._List[ddl_List.selection]
						lbl_item.text = owner._List[ddl_List.selection]
					)
					
					on ddl_List selected selectedItem do
					(
						print owner._List[ddl_List.selection]
						lbl_item.text = owner._List[ddl_List.selection]
					)
				)
			),
			
			fn ShowUI mode=
			(
				createdialog mainUI modal:mode
			),
			
			on create do
				mainUI.owner = this
	)	
	myStruct = myStruct()
	ok
)
myStruct.ShowUI true

If you change the last line with


myStruct.ShowUI false

You’ll receive a
– MAXScript Rollout Handler Exception:
– Runtime error: Attempting to access private member: “_List” in: (myStruct mainUI:Rollout:mainUI)

I discovered this problem while trying to add a second rollout (declared in the same way) and created pressing a button in the first one: I’ve set the first rollout as modal:false and set true the second… so had the error.

Any help will be very very appreciated!!!
Thanks!!


#2
 owner._List

here is the problem. You can’t access any private property of a struct from anywhere except from the struct instance itself.

you have to provide get/set methods inside the struct or to make the property public:

struct ABCD
(
	private 
		the_list = #(#a, #b, #c, #d),
	public 
		fn get_the_list = the_list
)	


_abcd = ABCD()


_abcd.the_list -- NO!!!

a = _abcd.get_the_list() -- YES!!!
a[1] = #aa
_abcd.get_the_list()

#3

Hi Denis… sorry to have “summoned” you :slightly_smiling_face:

Yes, it’s clear, but there are 2 things I don’t understand very well.

1- Why can I read owner._List in the “open” event and not in “pressed” or “selected”? :roll_eyes:

2- The “modal true/false” thing: if I set it to true I get no error. How can it be? It basically seems that if modal I can access that private member after all: the rollout is inside the struct itself, doesn’t it? So I’m still inside the struct instance when accessing the member.

Getter and Setter are surely an option, but even making members public.
I was suggested also to make a shallow copy of the _List at open rollout time and work on the copy. It works! But unfortunately that is not possible with simple variables without nasty workarounds…

Many thanks as always!!


#4

these events are different by “happening”…
“open” is static, “pressed” and “selected” are dynamic.

so at the moment of compilation in the first case the compiler assigns the current value, but at the second case a pointer to the property… (not really like i say, but i tell it the way to be more clear).


#5

Clear… It’s quite a difference.
You should write book about maxscript one day… I would buy it :wink: