[Closed] Are structs inherently useful?
So I was asking questions about structuring before and I had it recommended by an advanced user that I should use structs. – In preparation for making a gigantic script.
Initially I was trying to put my script (over 1000 lines of code at the time) into only 1 struct, and then decided to break it up into 2 structs.
I named the 2 structs’ appropriate names ( for the groups), but soon found that I was having to reference all variables and functions with the prefix. This made the code almost unreadable (because of the constant prefix), so I changed the struct names to simply “a” and “b”.
Even with a single letter prefix it’s annoying to read the code, so I must ask:
Are they inherently valuable?
I have read a few times that it’s bad to be declaring a stack of globals, but/so can someone explain why?
Thanks.
PS: The script I’m working on is going to be approximately 20’000 lines of code (if at all relevant).
Not that really essentially useful , it just practice you to organize what you do n if you can avoid using global,…who knows …some doctor may be can explain further n correct what Im thinking n writing, I’m not using struct thing to divide all my script to 5 script [on one UI], only using global little after those function run end I remove it n it work. After work little with maxscript I came to little understand that function must be lay out like photoshop layer, what must run first you put it first , 2nd n so on , tho there’s might some exception here n there. Tho if you think you must use global then you must remove it after function run,don’t put too much .That’s what I think.
To use structures you have to instantiate them, meaning you have to prefix each function later or you can create a long list of cached functions for easier access, but that seems to defeat the purpose of the structure and you should then explicitly declare each of them as local.
Sometimes Structures are convenient, for example if you have reusable libraries of functions, but most of the times just working in local scope () should be enough.
If you work in local scope make sure to explicitly declare the local variables if want to prevent conflicts with other scripts that might use the same names as globals.
If you dont feel comfortable using structures, then don’t use them, you will not see any real benefit in the short term. Also you will find that the big majority of scripts out there do not use structures for the whole code, not even all the scripts that comes with Max use them.
Why Global are “dangerous”?
In MaxScript, a global variable is accessible from any script being it yours or not, and so you can easily override it without immediately noticing the consequences, and the result are unpredictable. They could range from crashing Max to make some functions not work properly anymore.
If you have just one script, I would assume you do not need any global except for some callbacks, rollouts, etc. but generally you could use all locals.
Take a look at this very simple example, where the global variable used for a rollout, is overridden later and so causing an error when pressing the “Close” button.
global myRollout
rollout myRollout ""
(
button bt_close "Close"
on bt_close pressed do destroydialog myRollout
)
createdialog myRollout
myRollout = 5
In the code shown in the picture, everything is implicitly declared as a global. So every variable and function you have there are globals, even when you have not explicitly declared them as globals.
To make a variable, function, etc. local, you first need to put the whole code between parentheses and must explicitly declare each variable, function, etc. as local. Otherwise any other script can override them by just using the same name as global.
Also, to circumvent the batch processing of MaxSctipt, you can explicitly declare each function in the beginning of your script as local. That way all functions have access to each other.
(
local fn1, fn2, fn3
fn fn3 =
(
fn1()
fn2()
)
fn fn1 = print "fn1"
fn fn2 = print "fn2"
fn3()
)
Both of these variables are the same, globals:
var1 = 5
or
global var1 = 5
To make sure it is a local variable you would need the following:
(
local var1 = 5
)
If I working on anything bigger than 100 lines of code I’ll break it into structs.
When things get tricky it helps me manage the complexity. I don’t have to use them but I find things are quicker and easier if I do.
That said if you do use structures it helps to write ‘structured’ code. I tend to think in layers of boxes ( struct instances ). If there’s a problem then I can quickly go to the right box to fix it.
Calling function with a prefix doesn’t make code ugly, it makes code clearer.
Structs are useful and worth learning, Organizing your functions and UI into structs provides long term benefits.
If you never have to update , reuse,pass code to another programmer, or distribute it to user with unknown additional scripts installed, then it matters less.
Sadly intelligent use of structs is Nowhere documented in the MXS docs.
You have to peruse these forums for that wisdom.
here is a quick example of the Current use of structs for tools (adapted form Dennis T’s useful advice)
global TheTool
(
struct ToolStruct
(
-- variables
myVariable="hello world",
--functions
fn myFunction=(
MessageBox ("myVariabel is:
"+myVariable) title:"TheTool.myFunction()"
),
--ui dialog
dialog = rollout dialog "Dialog"
(
--create owner varible so UI controlls can access the functions above
local owner = if owner != undefined do owner
button myButton "my function"
on myButton pressed do( owner.myFunction())
),
--dialog display management
fn destroy = try(destroydialog TheTool.dialog) catch(),
fn show =(destroy();createdialog dialog),
on create do (destroy();dialog.owner = this)
)
TheTool = ToolStruct()
ok
)
--launch TheTool
TheTool.show()
Thanks for the answers guys. I’m sick and bed-ridden right now and will read them as soon as I can focus on work again.
global getWellSoon
struct healTheWorld
(
patient = undefined,
ibuProfen = 200,
fn takeIbuprofen num:2 =
(
if num > 10 then
(
messagebox "Don't do it, there's plenty to live for!"
)
else
(
if patient != undefined then
(
IbuProfen*num
if querybox "Sleep?" then sleep 1000000
)
)
)
)
getWellSoon = healTheWorld patient:"Gumnut"
getwellSoon.takeIbuprofen()
Might be good to note that you don’t need to instantiate a struct if it is made from just function calls, it’s only if you have defined member variables
in you case it’s an instance made.
i tried to use ‘only function’ structures… it doesn’t work for me. in practice my tasks always need some properties in structure. so i didn’t find having a structure as its definition only useful.
i didn’t use structures really in my development until i found the way how to use nested structures. since that ‘my world’ changed. mxs struct is like a real class now … i can define everything and store everything inside a structure instance.
but… several month ago i found a new technique… better than structure. sorry, but i’m not ready to share it yet.
This is a nice thread regarding struct, global and local… Im a beginner in writing script. I do write scripts for my own personal works quite sometime…Now i understand now how it works… Thanks for the input guys…