Page 1 of 1

Create a function that will work for 10 ticks when it is called.

Posted: Fri Oct 05, 2018 8:37 pm
by WIZ4
What are the ways to implement a function that will work for 10 ticks when it is called?
For example, I create a function that I call using the console.

Code: Select all

function print_message_within_10_tick()
game.print(game.tick)
end
How do I make this function work for 10 ticks?

Re: Create a function that will work for 10 ticks when it is called.

Posted: Fri Oct 05, 2018 8:51 pm
by DaveMcW

Code: Select all

function print_message_within_10_tick()
  if game.tick <= goal then
    game.print(game.tick)
  else 
    script.on_event(defines.events.on_tick, nil)  -- delete listener
  end
end
goal = game.tick + 10
script.on_event(defines.events.on_tick, print_message_within_10_tick)  -- add listener

Re: Create a function that will work for 10 ticks when it is called.

Posted: Fri Oct 05, 2018 9:00 pm
by WIZ4
DaveMcW wrote: Fri Oct 05, 2018 8:51 pm

Code: Select all

function print_message_within_10_tick()
  if game.tick <= goal then
    game.print(game.tick)
  else 
    script.on_event(defines.events.on_tick, nil)  -- delete listener
  end
end
goal = game.tick + 10
script.on_event(defines.events.on_tick, print_message_within_10_tick)  -- add listener
Wow, great work! Thanks for the quick response :)

Re: Create a function that will work for 10 ticks when it is called.

Posted: Fri Oct 05, 2018 9:36 pm
by WIZ4
So, this way of adding a function to an event removes all other functions in this event.
Is there a way around this?
The only solution I found:

Code: Select all

function return_on_tick()
script.on_event(defines.events.on_tick, function(event) 
function_one(event)
function_two(event)
function_three(event)
end)
end

Code: Select all

script.on_init(function()
return_on_tick()
end)

Code: Select all

function print_message_within_10_tick()
  if game.tick < goal then
    game.print(game.tick)
  else 
    script.on_event(defines.events.on_tick, nil)
	return_on_tick()
  end
end

function call_print_message_within_10_tick()
goal = game.tick + 10
script.on_event(defines.events.on_tick, print_message_within_10_tick)
end
But it seems to me this method is not correct. Are there any other solutions?

Re: Create a function that will work for 10 ticks when it is called.

Posted: Fri Oct 05, 2018 9:53 pm
by DaveMcW
If you have a permanent on_tick function, you can just use that.

Code: Select all

function_three(event)
  if goal and goal < game.tick then
    game.print(game.tick)
  end
end

Re: Create a function that will work for 10 ticks when it is called.

Posted: Fri Oct 05, 2018 11:54 pm
by Nexela
With a quick glance, none of the proposed soultions are multiplayer safe


If you ALREADY have a tick handler I use something like this

Code: Select all

local function print_in_10(msg)
  global.future[game.tick] = msg
end

script.on_init(function() global.future = {} end)

script.on_tick(function()
  -- stuff
  if global.future[event.tick] then
    game.print(global.future[event.tick])
    global.future[event.tick] = nil
  end