Lesson 6: If that case then do what huh now?

Next topic for my dissection is conditions statements, which is the basic logical choices we can write in code.

a = 3
if a == 3 then print "Somebody slap me, I think it's a three!"

So we say a equals 3, then ask if a equals equals three, the print “insert witty comment here”…. right so first question, why == not = ?  The technical answer is to say if a equals and only equals 3 then…. which doesn’t really make it any clearer but it’s just  way of maxscript to understand that we’re trying to check a value here and not assigning a to equal 3. Now it’s important to understand here that it’s passing the condition test here not because a equals 3, but actually that a == 3 returns true…. It’s import to understand this because we can save ourselves a lot of hassle writing long scripts if certain values already equal true, like a checkbox’s tickedness.. (that’s not a word I just made it up.).

if chk_testcheckbox.state then print "I really have issues with making up words! By the way the checkbox is ticked."

So lets look at that… we could write, if chk_testcheckbox.state == true then… which is just going to return true, which is what the value of the .state is anyway, so you see how we’re not having to actually do a comparision, we just need to get true somehow to pass our condition test. we can use further shorthand to test whether it’s false by using not.

if not chk_testcheckbox.state then print " The checkbox is unticked."

So if the checkbox is unticked the .state value will return false so our condition check will stop, but because we’re saying “not false” which inverts our boolean (true/false value) to true we pass the condition test. At first with your script writing it will probably seem easier and clearer to write

if chk_testcheckbox.state == false then print "the checkbox is unticked"

But you can see how this is going to add up with lots of extra code in there, lets see what and how we can compare things… we can use the != which means not equals.

if chk_testcheckbox.state != true then print "the checkbox is unticked"

So if the checkbox’s state is not equal to true then… which if the checkbox is unticked is going to return true for our condition test.

We can do more number stuff…. dig out your old algebra textbook… (maybe not that complicated)…. != >, <, >=, <=, ‘greater than’, ‘less than’, ‘greater than or equal to’ and ‘less than or equal to’.

 a = 3
 b = 5
 c = 5

 b == c --returns true, b equals c, 5 == 5
 b != a --returns true, b is not equal to a, 5 does not equal 5
 b != c --returns false, 5 is not equal to 5, but 5 equals 5 so returns false.
 a > b  --returns false,  3 is not greater than 5
 a < b  --returns true, 3 is less than 5
 a <= b --returns  true, 3 is less than or equal to 5
 a >= b -- returns false, 3 is not greater than or equal to 5
 b >= c -- returns true, 5 is greater than or equal to 5
 b <= c -- returns true, 5 is less than or equal to 5
 b < c   --returns false, 5 is not less than 5
 b > c  --returns false , 5 is not greater than 5

Hopefully that all makes sense, note the difference between just saying something is greater than, and greater than or equal to, it’s really important to use the correct one depending on your circumstances.

It’s important to think about the different type of value you’re trying to compare, for instance you can’t compare a boolean value of ‘true’ with a string value of “elephant”. Maxscript doesn’t know how to compare these two, so only compare like for like.

Obviously you can only do < less than or > greater than comparisons on number values,  with string values you can only check exact matches…

a = "test"
if a == "test" do print "hmm a passed this test"

If you want to match bits of string values, ie find “at” in “that” then you need to use MatchPattern or Findstring, but more on that later….

It’s probably a good moment to talk about the structure of scripts, it’s really easy to get lost in your script once you start using conditional statements with multi-lined code, its’ really good practice to have a consistent formatting at least, there’s no defined way to do it, more personal preference but here’s how I do it. I like to keep all my conditions enclosed with brackets, you don’t need to if it’s a single line of code in your condition, but I find it easier to read.

If a == "test" then print "a is test"

Works as does this….

If a == "test then
    print "a is test"
 print "I'm not in the test so don't care what a is"

So only the line after ‘then’ is considered part of the condition test… This works better (for me anyway)…

If a == "test then
 (
    print "A is test"
 )
print "I'm not in the test so don't care what a is"

I find this a lot easier to read and you can add to it later more easily, and you can do multi-lined code inside the brackets.

If a == "test" then
(
    print "a is test"
    print "b is at this point unknown"
)

I keep the brackets on the same line indentation as the test, and then all the code that’s in that test indented in so I can tell it’s part of it, compare it with this messier layout.

If a == "test" then (
print "a is test"
print "b is at this point unknown")

It may not seem that bad now but give it a few tests and it’ll get messy.

If b != undefined then (
If a == "test" then (
print "a is test"
print "b is not undefined....."))

You can see it’s a bit difficult to read, but with some simple tab indentations….

If b != undefined then
 (
    If a == "test" then
    (
        print "a is test"
        print "b is not undefined....."
    )
 )

Right so let’s expand on if tests a bit more, and include the else statement. We can use this to say if something = something , then do that… otherwise (else)… do this instead…. Pretty easy to understand (hopefully)…

if a == "test then
 (
    print "a is test"
 )
 else
 (
    print "a is not test"
 )

Again, If we were to look at that without bracket formatting this it what it would look like….

if a == "test" then
    print "a is test"
else
    print "a is not test"

And of course you can mix and match your formatting…

if a == Test then
(
    print "a is test"
    print "YES!"
)
else
    print "a is not test"

I’m just including all these ways of writing these so you get exposed to the different ways you can write the code and different formatting, I remember it confused me at first. If in doubt always use brackets.

To be continued….

Advertisements

About davewortley

Somewhere between an artist and a programmer, I like technical things, but being creative. I love problem solving and coming up with elaborate solutions. This blog is where I shall share ideas, tips, tutorials and anything that amuses me.
This entry was posted in Lessons, MaxScript. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s