I read the blog post on the Love2d blog about ternary operators and so wanted to try but couldn't get it to work with the two variables I wanted to switch between. The blog talks about
1 is a constant that is neither false nor nil, thus the expression on the right side of the assignment will always evaluate to 2. It doesn't really make sense to use a constant as the first argument of the operator.
Is there a way to do it like this or indeed any other way to write it shorter?
You check against "1", not "variable", so it doesn't work. Note that this is not a real ternary operator, it's just the way Lua's "and" and "or" work allows to make things like that. It will fail to work if your first value resolves to "false" and will return the second value instead.
raidho36 wrote: ↑Thu Feb 15, 2018 2:21 pm
You check against "1", not "variable", so it doesn't work.
Yes that's why I said it clearly doesn't work. There didn't seem to be a way but Grump has provided a solution.
raidho36 wrote: ↑Thu Feb 15, 2018 2:21 pm
Note that this is not a real ternary operator, it's just the way Lua's "and" and "or" work allows to make things like that. It will fail to work if your first value resolves to "false" and will return the second value instead.
Yes I realise there are no real ternary operators in lua, otherwise it would be simple.
Nixola wrote: ↑Thu Feb 15, 2018 7:36 pm
If you're just toggling that variable between 1 and 2, you may want to use true and false instead if it makes sense in its context.
Yeah of course but unfortunately it doesn't in this case since they were actually levels. There are only two right now so that was just some temp code to switch between them. Looking at the code annoyed me 'cos it was so long and I hate any code that looks long. Especially since it was only temporary. So it was good that I could at least shorten it down some.
I mean you can use meta-programming to get a new syntax like "variable = newvalue ifeq oldvalue". It's definitely a tradeoff to take, as you'll be introducing meta-programming into your project, and that comes with a cost. I personally am using Moonscript (for room scripts only), and don't use any meta-programming in Lua. I do like meta-programming much when I am using Lisp languages.
other than what metatables offer (which isn't much), using metaprogramming with löve may be harder than what's it worth.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.