how to use the same object in each button ?

how to use the same object in each button ?

Anonymous
Not applicable
430 Views
3 Replies
Message 1 of 4

how to use the same object in each button ?

Anonymous
Not applicable
Hello.

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:

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:
button Farm_btn @" FARM " width:100 height:30 pos:

/* 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:
button Farm_btn @" FARM " width:100 height:30 pos:

/* 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:

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 ?
0 Likes
431 Views
3 Replies
Replies (3)
Message 2 of 4

Steve_Curley
Mentor
Mentor
The order in which you define things in Rollout code is important. See:-
Visibility of Locals, Functions, Structures and User-Interface Items in Rollout Code
in the Maxscript Help.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 3 of 4

Anonymous
Not applicable
I can modify code like this:

struct MyContainer
(
public
lockView = true
)

struct TestUI
(
private
text = "hello",

public
fn Create =
(
rollout MainWindow "Test"
(
local _dataContainer = MyContainer()
button GI_btn @" GI " width:100 _height:30 pos:
button Farm_btn @" FARM " width:100 height:30 pos:

/* 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:
button Farm_btn @" FARM " width:60 height:30 pos:

/* 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:
button Farm_btn @" FARM " width:60 height:30 pos:

/* 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 _dataContainer = MyContainer()" to first rollout will not help. If I define MyContainer as global it will work , but I don't want to do this. After reading part of documentation you mentioned I still don't get this. If rollout can use private variable from struct as its name, why it can't use variable in button event ?

Do you have any code example ?
0 Likes
Message 4 of 4

Anonymous
Not applicable
Solution for the problem link
0 Likes