PDA

View Full Version : script runs second time, but not the first time...


CapitanRed
04-08-2008, 03:30 PM
fn testFn = (
if myRollout.YesOrNo.state == true then(
print "yes"
)
else(
print "no"
)
)
rollout myRollout "myRollout"(
checkbox YesOrNo "YesOrNo" checked:true
button testB "test"
on testB pressed do testFn()
)
testUtility = newRolloutFloater "jsut a test" 200 200
addRollout myRollout testUtility



I don't understand that! I know that it has has a variable that it had not in the first time running...but the message is:

unknown property "YesOrNo" in undefined!

how can the YesOrNo checkbox be undefined when it is in the rollout??

how do I solve this probleme?

davestewart
04-08-2008, 03:38 PM
A couple of ways - either define the function after the rollout, or pre-declare the rollout:


global myRollout

fn testFn = (
if myRollout.YesOrNo.state == true then(
print "yes"
)
else(
print "no"
)
)
rollout myRollout "myRollout"(
checkbox YesOrNo "YesOrNo" checked:true
button testB "test"
on testB pressed do testFn()
)
testUtility = newRolloutFloater "jsut a test" 200 200
addRollout myRollout testUtility


It's a bit annoying it's not like other languages, but such is life :D

CapitanRed
04-08-2008, 05:16 PM
hey, thank you dave, now it works prefectly.
sometimes you sit hours in front of a script, and the solution is so easy :p

davestewart
04-08-2008, 05:59 PM
Whenever you get a script that runs only the second time, you can be pretty damn sure it's a pre-declaration issue.


You'd actually benefit from putting your function inside the rollout anyway as it's specific to the rollout - that way it keeps from cluttering "global" scope (where it's not needed anyway), plus you don't need the global reference to the rollout.

Note how I defined the function AFTER the controls were defined so the function can reference them correctly.


-- rollout
rollout myRollout "myRollout"(

-- interface
checkbox YesOrNo "YesOrNo" checked:true
button testB "test"

-- functions
fn testFn = (
if YesOrNo.state == true then(
print "yes"
)
else(
print "no"
)
)


-- handlers
on testB pressed do testFn()
)
testUtility = newRolloutFloater "jsut a test" 200 200
addRollout myRollout testUtility

Hope that helps
:D

eek
04-08-2008, 06:31 PM
You can put all your variables, functions in a struct too - this also allows it to be used across mutiple scenes, assets etc.

using a Global has a caveat that now it exists in the world. So if another function of max etc uses the same name your'll get errors. With a struct everything is encapsulated - and you can keep your code clean.



struct _myStruct
(
yes = "Hello",
no = "Get Lost!",
fn testFn button = (if button.state == true then(messagebox yes)else(messagebox no)),
endOfStruct
)


myStruct = _myStruct() -- instance the struct

rollout myRollout "myRollout"
(
checkbox YesOrNo "YesOrNo" checked:true
button testB "test"
on testB pressed do myStruct.testFn yesOrNo
)
testUtility = newRolloutFloater "jsut a test" 200 200
addRollout myRollout testUtility

ZeBoxx2
04-09-2008, 03:56 PM
yeah, but what if somebody uses the same name as your struct for... well... another struct in this case.. "_myStruct" would suffer from the same problem as any global variable.

Best practice is to name your structs and any variables, callback IDs, etc. unique to you, your project, etc. to mimize the risk in any of those cases.

( ours all start with "sf_" to indicate splutterfish, followed by "b2_" where it's Brazil r/s v2-specific, then the utility's short name, for example. Chances of random collissions are pretty minimal that way. )

CapitanRed
04-09-2008, 04:07 PM
hmm...thank you guys. I'm jsut trying to understand :P
this struct thing is very new for me, but i think i'll give it a try...phuu!

eek
04-09-2008, 04:28 PM
yeah, but what if somebody uses the same name as your struct for... well... another struct in this case.. "_myStruct" would suffer from the same problem as any global variable.

Best practice is to name your structs and any variables, callback IDs, etc. unique to you, your project, etc. to mimize the risk in any of those cases.

( ours all start with "sf_" to indicate splutterfish, followed by "b2_" where it's Brazil r/s v2-specific, then the utility's short name, for example. Chances of random collissions are pretty minimal that way. )


Its why i use "cl_" :)

davestewart
04-09-2008, 06:12 PM
Probably best to work towards understanding the basics before worrying about best practices and hypothetical collisions! (but yes I know I started it!) ;)

In the spirit of Getting Real, It's a Problem When It's a Problem (http://gettingreal.37signals.com/ch04_Its_a_Problem_When_Its_a_Problem.php) !!

ZeBoxx2
04-09-2008, 07:19 PM
In the spirit of Getting Real, It's a Problem When It's a Problem (http://gettingreal.37signals.com/ch04_Its_a_Problem_When_Its_a_Problem.php) !!
Given the recent events (http://tech.slashdot.org/article.pl?sid=08/04/09/1256222) that post (Edit: the one linked to) suddenly becomes hi-lar-i-ous :)
But /off-topic from me :)

CapitanRed - all good to go now? :D

davestewart
04-10-2008, 02:06 AM
Ha ha, yes I can see the ironies there!

Don't worry, I don't think that everything 37Signals state is gospel - quite the opposite in fact. I think their mantra of simplification is actually a recipe for frustration! As a power-user I certainly can't see myself using their apps.

But I think concepts such as encapsulation and tools such as structs seem pretty esoteric when you're still learning maxscript, and CaptainRed seems to be on more of a gentler learning curve right now.

So my (not intended to be big!) point was that although good practice is certainly important, when you're tackling a new concept (such as scope, in this case), more advanced concepts such as structs and naming of said structs to avoid global collisions may be a little overwhelming.

I was probably a beginner more recently than you two guys, so just coming in on the side of keeping-it-easy - ie, "it's (structs, collisions, encaspulation) a problem when it's a problem" ;)

No feathers were ruffled, were they!?

ZeBoxx2
04-10-2008, 11:24 AM
No feathers were ruffled, were they!?
Absolutely not :)

And I agree, there's always going to be some part - especially with scripting - that one should say "aw to heck with that" and leave things 'unclean'.

For example, I daresay 99% of the functions I've written do no input validation; I.e. if I have the function...
fn multiplyAbyB a b = ( a * b )
...I'm not going to bother with checking whether A and B are numbers. I simply expect that all the scripts using it will call it correctly, and if a third party should use it and decide to send it a string for absolutely no good reason.. well, not my problem.

I do think, however, that scope and variable naming should be near the beginning of any courseware, rather than towards the middle. Just my 2 cents (3.18 cents in USD) :)

davestewart
04-10-2008, 11:49 AM
Cool :) And in total agreement!

Ironically, his rollouts are already prefixed! Ha ha :D


rollout myRollout "myRollout"(

CapitanRed
04-10-2008, 12:35 PM
...I'm not going to bother with checking whether A and B are numbers. I simply expect that all the scripts using it will call it correctly, and if a third party should use it and decide to send it a string for absolutely no good reason.. well, not my problem.




defining the type of variabls is what I was looking for but in max all variables seem to be type free

I do understand the struct thing now...but i have still little trouble to use it :p
I need some experience ;)

ZeBoxx2
04-10-2008, 12:54 PM
defining the type of variabls is what I was looking for but in max all variables seem to be type free
Correct :)

BrVB
05-05-2008, 06:38 PM
In Maxscript 2009 you can access any value by using
NameRollout.NameSpinner.value
The global issue has been eliminated
but if you try with global he doesn't find the value, pretty frustrating

ZeBoxx2
05-05-2008, 11:06 PM
<rollout>.<control>.<something> has been available at least since Max5.. pretty sure in Max4 as well ;)

Kramsurfer
05-06-2008, 02:00 AM
Speaking of rollout Scoping. I've been passing values into rollouts by using an "init" function in the rollout, instead of the "on open" which is with in the scope of the R.O.


(
rollout myrollout "Look at ME!"
(
label LB00 ""

function init NodeorDataIwantToWorkWithandIsNotSelectedorGloballyscoped =
(
LB00.text = NodeorDataIwantToWorkWithandIsNotSelectedorGloballyscoped
)
)

createDialog myrollout 300 90
myrollout.init "Look At Me"
)


It works, but is there a better way to get data, that is not globally scoped, into the scope of a rollout? and no, that's not a typical variable name for me...

erilaz
05-06-2008, 02:39 AM
and no, that's not a typical variable name for me...

Haha! I was about to say... :D

CGTalk Moderation
05-06-2008, 02:39 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.