Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(+1)

How do I check for more than one condition in a single if statement?

(+2)

&  is AND,   |  is OR.

if thing1.value & thing2.value # AND
if thing1.value | thing2.value # OR
if ! thing1.value # NOT

Do these examples work for what you need? There's more in this post as well.

(3 edits)
x:1 y:1 z:1
if x=1&y=1&z=1 "success" else "failure" end

That helped me quite a lot. Thank you!


I did have an issue modifying the datePicker contraption though, but got it to work eventually.

I created an additional field with the selected date (v) versus the navigated-to date (p). I only wanted to invert the day of the month cell when it matched the exact date that was selected (not selecting new days of the month as the months and years were cycled through).

I don’t think I need to post the whole code block, but for some reason the if statement only works when I put the conditions in their own brackets:

if (index=p.day-1) & (p.month=v.month) & (p.year=v.year)
   canv.invert[cell+1 cellsize-2]
end

Any idea as to why the above code works, but removing the brackets causes it to fail?

(The line of code in question is at the bottom of the prototype script in the datePicker contraption, if someone requires more context: if index=p.day-1 canv.invert[cell+1 cellsize-2] end.)

(+2)

As noted in the previous post Ahm linked, Lil has uniform operator precedence; expressions are evaluated right-to-left unless parentheses are used.

The expression

x=1&y=1&z=1

In your first example is only working by coincidence; It is not equivalent to

(x=1)&(y=1)&(z=1)

But rather

x=(1&(y=(1&(z=1))))
(3 edits)

I did check out that link, but I honestly didn’t fully comprehend it. The explanation you provided has given me a lot more clarity. Thank you!

I had a sort of aha moment when I was customizing the datePicker and changed the text output to include the year beside the month at the top.

canv.text[""fuse month_names[p.month-1]," - ",p.year (0,5)+canv.size*.5,0 "top_center"]

I’m not a strong programmer, but I feel like that line’s syntax could only work evaluating right to left. Am I understanding that correctly? (More so about nesting the fuse command in there, I feel.)

Also, is the fuse command the only way to concatenate strings?


Edit: I guess I didn’t need the fuse part. I just edited it to:

canv.text[month_names[p.month-1]," - ",p.year (0,5)+canv.size*.5,0 "top_center"]

Nevermind, I have a lot more learning to do.

(+1)

The two ways of concatenating strings in Lil are fuse, which takes a simple string to intercalate between the elements of a list:

 " : " fuse "Alpha","Beta"             # -> "Alpha : Beta"

And format, which offers a richer printf-like string formatting language:

 "%s : %s" format "Alpha","Beta"       # -> "Alpha : Beta"  

The format primitive is useful in many situations, including gluing a fixed prefix and/or suffix onto strings:

"Prefix %s Suffix" format "MyString"   # -> "Prefix MyString Suffix"

In situations where a function, operator, or interface attribute expects a string, you can often get away with simply providing a list of strings (perhaps joined together into a list with the comma (,) operator) which will be implicitly fused together. Per The Lil Reference Manual for type coercions:

When a string is required, numbers are formatted, and lists are recursively converted to strings and joined. Otherwise, the empty string is used.

This is why you're able to elide the fuse in your example; "field.text" always treats a written value as a string

(+1)

That makes perfect sense. Thank you for your patience.