TIR 0 = truthy

Place to post guides, observations, things related to modding that are not mods themselves.
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

TIR 0 = truthy

Post by aubergine18 »

Today I remembered: In Lua, number 0 is a truthy value (only `nil` and `false` are falsey, everything else, including empty string, is truthy).

Code: Select all

if 0 then
  game.print('truthy') -- prints 'truthy' to console
end
So if you're using % in an on_tick event handler, bear that in mind... you can't just use "if not game.tick % 180 then", you have to do something like:

Code: Select all

script.on_event( defines.events.on_tick, function()
  if game.tick % 180 == 0 then
      -- do something every 180 ticks
  end
end
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5341
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: TIR 0 = truthy

Post by Klonan »

Its slightly easier if you do

Code: Select all

if game.tick % 180 ~= 0 then return end
So you don't have to maintain your indent through the whole function
Post Reply

Return to “Modding discussion”