I need to set unit
's exhaust
.
A unit
might have a turnScript
, and the turnScript
might have a delay
. If so, unit.exhaust = unit.turnScript.delay
. Otherwise, unit.exhaust = -1
.
This is what I have now. It works, but it's repetitive and unidiomatic.
unit.exhaust = unit.turnScript && unit.turnScript.delay ? unit.turnScript.delay : -1;
I tried these two as well:
unit.exhaust = unit.turnScript ? unit.turnScript.delay : -1 || -1;
and
unit.exhaust = {unit.turnScript ? unit.turnScript.delay : -1} || -1;
But the first returns undefined when turnScript
exists but not delay
, and the second gives me a syntax error (unexpected .
). Is there a better way to do it?
unit
s stats are calculated differently, it wouldn't get reused. Thanks though. – Devon Parsons Jun 7 at 21:00unit.exhaust = {unit.turnScript ? unit.turnScript.delay : -1} || -1;
) – QPaysTaxes Jun 7 at 22:57