Notifications
Clear all

[Closed] Struct initialization problem? Or

This keeps throwing an error:


(
	global miauuStr
	
	struct miauuTestStr
	(
		defaultLayer = layerManager.getLayer 0,
		numMarks = 0,
		visState = false
	)
	miauuStr = miauuTestStr()
	
	rollout rol_miauu "Test" height:300
	(
		local defaultVal = if miauuStr.numMarks != 0 then 3 else 5
		
		label lbl_30 "Mark 30" pos:[10,10]
		spinner spn_30 "" range:[-1000,1000,defaultVal] fieldWidth:50 pos:[100,10]
		
	)
	createDialog rol_miauu
)

Error:

-- Error occurred in anonymous codeblock; filename: ; position: 397; line: 19
-- Unknown property: "numMarks" in undefined
-- MAXScript callstack:
--	thread data: threadID:8816
--	------------------------------------------------------
--	[stack level: 0]
--	In top-level

Do I have to define the defaultVal as undefined and assign the miauuStr.numMarks in
`on rol_miauu open do’?

11 Replies
global miauuStr
(
	struct miauuTestStr
	(
		defaultLayer = layerManager.getLayer 0,
		numMarks = 0,
		visState = false,
		dialog = 
		(
			rollout dialog "Test" height:300
			(
				local owner = owner
				local defaultVal = if owner != undefined and owner.numMarks != 0 then 3 else 5
				
				label lbl_30 "Mark 30" pos:[10,10]
				spinner spn_30 "" range:[-1000,1000,defaultVal] fieldWidth:50 pos:[100,10]
			)
		),
		on create do
		(
			dialog.owner = miauuStr 
		)
	)
	miauuStr = miauuTestStr()
	ok
)

createDialog miauuStr.dialog

this is the right way

So, there is no straight forward solution if the code is as in my first post?
I will have to reassign the value of the spinners on roll open event?

very unlikely you will use the same structure with multiple dialogs, so it’s better to put the rollout inside the structure. inside the rollout’s body you will have full access to all struct’s properties and methods, and be able to change everything public

also this approach helps to “synch” both struct and rollout developments, because you define and update them together.

The struct is used in about 50 dialogs and some of its variables are used in other 2 structs.

hmm… is it a library? why one structure used by so many dialogs? It’s possible of course, but it should not store any specific for single dialog settings.

Some of the dialogs are dynamically created, so I decided to keep all important variables and functions outside of all dialogs.

I wonder why this works:


(
	global miauuStr
	
	struct miauuTestStr
	(
		defaultLayer = layerManager.getLayer 0,
		numMarks = 0,
		visState = false
	)
	miauuStr = miauuTestStr()
	
	format "miauuStr: %\n" miauuStr
	
	rollout rol_miauu "Test" height:300
	(
		local defaultVal = 2
		
		label lbl_30 "Mark 30" pos:[10,10]
		spinner spn_30 "" range:[-1000,1000,defaultVal] fieldWidth:50 pos:[100,10]
		
		on rol_miauu open do
		(
			defaultVal = if miauuStr.numMarks != 0 then 3 else 5
			spn_30.range = [-1000,1000,defaultVal]
		)
	)
	createDialog rol_miauu
)

@denisT, this also works without an error:

(
	global miauuStr	
	struct miauuTestStr
	(
		defaultLayer = layerManager.getLayer 0,
		numMarks = 0,
		numMarksArr = #(0,1,2,3,4,5,6,7,8,9),
		visState = false
	)
	miauuStr = miauuTestStr()
	
	
	rollout rol_miauu "Test" height:300
	(
-- 		local defaultVal = if miauuStr.numMarks != 0 then 3 else 5	
			
		label lbl_30 "Mark 30" pos:[10,10]
-- 		spinner spn_30 "" range:[-1000,1000,defaultVal] fieldWidth:50 pos:[100,10]			
		spinner spn_30 "" range:[-1000,1000,miauuStr.numMarksArr[2]] fieldWidth:50 pos:[100,10]			
	)
	createDialog rol_miauu
)

and this also works:

(
	global miauuStr	
	struct miauuTestStr
	(
		defaultLayer = layerManager.getLayer 0,
		numMarks = 0,
		numMarksArr = #(0,1,2,3,4,5,6,7,8,9),
		visState = false
	)
	miauuStr = miauuTestStr()
	
	
	rollout rol_miauu "Test" height:300
	(
-- 		local defaultVal = if miauuStr.numMarks != 0 then 3 else 5	
			
		label lbl_30 "Mark 30" pos:[10,10]
		spinner spn_30 "" range:[-1000,1000,( if miauuStr.numMarks != 0 then 3 else 5)] fieldWidth:50 pos:[100,10]			
-- 		spinner spn_30 "" range:[-1000,1000,miauuStr.numMarksArr[2]] fieldWidth:50 pos:[100,10]			
	)
	createDialog rol_miauu
)

Here is a solution:

(
	global miauuStr	
	struct miauuTestStr
	(
		defaultLayer = layerManager.getLayer 0,
		numMarks = 0,
		numMarksArr = #(0,1,2,3,4,5,6,7,8,9),
		visState = false,
			
		ok
	)
	miauuStr = miauuTestStr()
)

(
	
	
	rollout rol_miauu "Test" height:300
	(
		local defaultVal = if miauuStr.numMarks != 0 then 3 else 5	
			
		label lbl_30 "Mark 30" pos:[10,10]		
		spinner spn_30 "" range:[-1000,1000,defaultVal] fieldWidth:50 pos:[100,10]			
	)
	createDialog rol_miauu
)

But, it prints the whole struct in the maxscript listener. Can this be avoided?

Page 1 / 2