Jump to content


how to use the same object in each button ?


  • Please log in to reply
16 replies to this topic

#1 mantragora

mantragora

    Illusionist

  • Members
  • PipPipPip
  • 268 posts
  • Joined: 08-December 11
  • Location:frozen hell
  • Name:to be, or not to be, TD ?

Posted 18 August 2012 - 03:03 AM

Hello.

3dsmax question.

In the code below I create two buttons. I wanted to use the same object that I could update and use to setup data in each button. But I hit the wall. If I try to use object already created in my struct, inside button Event, I get:

Quote

Runtime error: Struct member access requires instance: _dataContainer <<
with this code:
struct MyContainer
(
	public
	lockView = true
)

struct TestUI
(
	private
	text = "hello",
	_dataContainer = MyContainer(),
	
	public
	fn Create = 
	(
		rollout MainWindow "Test"
		(

			button GI_btn @" GI " width:100 _height:30 pos:[20,40]
			button Farm_btn @" FARM " width:100 height:30 pos:[20, 70]
			
			/* EVENTS ---------------------------------------- */
			on GI_btn pressed do
			(
				_dataContainer.lockView = true
			)
				
			on Farm_btn pressed do
			(
				messagebox (_dataContainer.lockView as string)
			)
		)
		
		createDialog MainWindow 200 200
	)
)
_ui = TestUI()
_ui.Create()

Now if I modify the code:
struct MyContainer
(
	public
	lockView = true
)

struct TestUI
(
	private
	text = "hello",
	
	public
	fn Create _dataContainer= 
	(
		rollout MainWindow "Test"
		(

			button GI_btn @" GI " width:100 _height:30 pos:[20,40]
			button Farm_btn @" FARM " width:100 height:30 pos:[20, 70]
			
			/* EVENTS ---------------------------------------- */
			on GI_btn pressed do
			(
				_dataContainer.lockView = true
			)
				
			on Farm_btn pressed do
			(
				messagebox (_dataContainer.lockView as string)
			)
		)
		
		createDialog MainWindow 200 200
	)
)
_my = MyContainer()
_ui = TestUI()
_ui.Create _my
and try to pass data thru function and then use them in button events, i get:

Quote

Compile error: No outer local variable references permitted here:  _dataContainer

So, how to correctly pass the same object into each button so I could work with the same data in each ?
magic happens here... sometimes

Vimeo
Twitter

#2 mantragora

mantragora

    Illusionist

  • Members
  • PipPipPip
  • 268 posts
  • Joined: 08-December 11
  • Location:frozen hell
  • Name:to be, or not to be, TD ?

Posted 18 August 2012 - 06:12 AM

I can modify code like this:
struct MyContainer
(
	 public
	 lockView = true
)

struct TestUI
(
	 private
	 text = "hello",
  
	 public
	 fn Create = 
	 (
		 rollout MainWindow "Test"
		 (
			 /* LOCAL data ---------------------------------------- */
			 local _dataContainer = MyContainer()

			 /* UI elements ---------------------------------------- */
			 button GI_btn @" GI " width:100 _height:30 pos:[20,40]
			 button Farm_btn @" FARM " width:100 height:30 pos:[20, 70]
  
			 /* EVENTS ---------------------------------------- */
			 on GI_btn pressed do
			 (
				 _dataContainer.lockView = true
			 )
  
			 on Farm_btn pressed do
			 (
				 messagebox (_dataContainer.lockView as string)
			 )
		 )

		 createDialog MainWindow 200 200
	 )
 )
_ui = TestUI()
_ui.Create()
and it will work perfectly.

Now lets create different setup:
struct MyContainer
(
	public
	lockView = true
)

struct TestUI
(
	private
	text = "hello",
	_data = MyContainer(),
	_buttonsRollout,
	_buttonsRolloutName = "Buttons",
	_optionsRollout,
	_optionsRolloutName = "Options",


	public
	fn Create = 
	(
		rollout _buttonsRollout _buttonsRolloutName
		(
			/* UI elements ---------------------------------------- */
			button GI_btn @" GI " width:60 height:30 pos:[20,10]
			button Farm_btn @" FARM " width:60 height:30 pos:[100, 10]
 
			/* EVENTS ---------------------------------------- */
			on GI_btn pressed do
			(
				messagebox (_data.lockView as string)
			)
 
			on Farm_btn pressed do
			(
				messagebox (_data.lockView as string)
			)
		)
		rollout _optionsRollout _optionsRolloutName
		(
			/* UI elements ---------------------------------------- */
			button GI_btn @" GI " width:60 height:30 pos:[20,10]
			button Farm_btn @" FARM " width:60 height:30 pos:[100, 10]
 
			/* EVENTS ---------------------------------------- */
			on GI_btn pressed do
			(
				messagebox (_data.lockView as string)
			)
 
			on Farm_btn pressed do
			(
				messagebox (_data.lockView as string)
			)
		)

		MainWindow = newRolloutFloater "Bake Utils" 200 200
		addRollout _buttonsRollout MainWindow
		addRollout _optionsRollout MainWindow
	)
)
_ui = TestUI()
_ui.Create()

Now I would like to acces the same data not only in buttons but also in both rollouts.
In this example adding "local _data = MyContainer()" to first rollout will not help. If I define MyContainer as global it will work , but I don't want to do this. If rollout can use private variable from struct as its name, why it can't use variable in button event ?

Edited by mantragora, 18 August 2012 - 06:34 AM.

magic happens here... sometimes

Vimeo
Twitter

#3 cpb

cpb

    Illusionist

  • Members
  • PipPipPip
  • 288 posts
  • Joined: 17-March 03
  • Name:craig brown

Posted 18 August 2012 - 08:14 AM

have you tried accessing MyContainer directly, instead of making it a variable inside another struct?
EG: instead of
_dataContainer.lockView = true
try
MyContainer.lockView = true

#4 cpb

cpb

    Illusionist

  • Members
  • PipPipPip
  • 288 posts
  • Joined: 17-March 03
  • Name:craig brown

Posted 18 August 2012 - 08:15 AM

[dupe post thx singtel]

Edited by cpb, 18 August 2012 - 08:15 AM.


#5 mantragora

mantragora

    Illusionist

  • Members
  • PipPipPip
  • 268 posts
  • Joined: 08-December 11
  • Location:frozen hell
  • Name:to be, or not to be, TD ?

Posted 18 August 2012 - 10:15 AM

View Postcpb, on 18 August 2012 - 08:14 AM, said:

have you tried accessing MyContainer directly, instead of making it a variable inside another struct?
EG: instead of
_dataContainer.lockView = true
try
MyContainer.lockView = true

yeah, I get

Quote

Unknown property: "lockView" in #Struct:MyContainer(
lockView:<data>; Public) <<
when I do this
on GI_btn pressed do
(
	messagebox (MyContainer.lockView as string)
)

Any other ideas ? :)

Edited by mantragora, 18 August 2012 - 10:32 AM.

magic happens here... sometimes

Vimeo
Twitter

#6 magneto

magneto

    Grand Master

  • Members
  • PipPipPipPipPip
  • 1,336 posts
  • Joined: 04-October 11
  • Location:Canada
  • Name:Ryan K

Posted 18 August 2012 - 05:10 PM

Are you using Max? :)

What are you trying to do? Trying to reduce code duplication for similar rollouts? You can do stuff like:

rollout myrollout ""
(
	filein "rollout controls.ms"
)


#7 mantragora

mantragora

    Illusionist

  • Members
  • PipPipPip
  • 268 posts
  • Joined: 08-December 11
  • Location:frozen hell
  • Name:to be, or not to be, TD ?

Posted 18 August 2012 - 05:24 PM

View Postmagneto, on 18 August 2012 - 05:10 PM, said:

Are you using Max? :)

Nope 1. I'm trying to convince myself that I made mistake by assuming that Max is really sh*ty application. There must be a reason all those flies out there use this sh*t.

View Postmagneto, on 18 August 2012 - 05:10 PM, said:

What are you trying to do? Trying to reduce code duplication for similar rollouts?
Nope 2. I want to use/have access to the same data in each rollout. That's why I need to pass the same instance to each. Button one sets some data, saves file. Button two can use the same data, modify it and save file with different name. It has to be this way since this object will be passed into 10 other objects before going into buttons. Some will modify it, others will use to it to set own dat etc.

Edited by mantragora, 18 August 2012 - 05:44 PM.

magic happens here... sometimes

Vimeo
Twitter

#8 magneto

magneto

    Grand Master

  • Members
  • PipPipPipPipPip
  • 1,336 posts
  • Joined: 04-October 11
  • Location:Canada
  • Name:Ryan K

Posted 18 August 2012 - 06:16 PM

lol in that case why not use a global struct? There is nothing wrong in using a global struct.

I like having nested structs like:

Framework.Math.Sqrt etc.

#9 mantragora

mantragora

    Illusionist

  • Members
  • PipPipPip
  • 268 posts
  • Joined: 08-December 11
  • Location:frozen hell
  • Name:to be, or not to be, TD ?

Posted 18 August 2012 - 06:26 PM

View Postmagneto, on 18 August 2012 - 06:16 PM, said:

lol in that case why not use a global struct? There is nothing wrong in using a global struct.

Well, yeah, but I don't need to use this anywhere beside those rollouts so I would like to avoid putting anything in global scope. I'm really curious why variable that is perfectly visible in scope below can't be used in events. That's something really obvious to do. Is there really no other way to pass the same data into two rollouts that are placed in one RolloutFloater ?
magic happens here... sometimes

Vimeo
Twitter

#10 magneto

magneto

    Grand Master

  • Members
  • PipPipPipPipPip
  • 1,336 posts
  • Joined: 04-October 11
  • Location:Canada
  • Name:Ryan K

Posted 18 August 2012 - 06:52 PM

I think that has to do with the rollout being local and not able to correctly reference the function's argument to the rollout.

There are lots of corner cases like this in maxscript, that's why the compiler is made to catch them because they know about them :)

#11 mantragora

mantragora

    Illusionist

  • Members
  • PipPipPip
  • 268 posts
  • Joined: 08-December 11
  • Location:frozen hell
  • Name:to be, or not to be, TD ?

Posted 18 August 2012 - 06:58 PM

View Postmagneto, on 18 August 2012 - 06:52 PM, said:

... because they know about them :)

F**K


Legend:
    ** = UC

Edited by mantragora, 18 August 2012 - 06:59 PM.

magic happens here... sometimes

Vimeo
Twitter

#12 magneto

magneto

    Grand Master

  • Members
  • PipPipPipPipPip
  • 1,336 posts
  • Joined: 04-October 11
  • Location:Canada
  • Name:Ryan K

Posted 18 August 2012 - 07:05 PM

lol I reported many issues before but they don't care. They only added public/private inside structs recently but it's useless :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users