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

Place to get help with not working mods / modding interface.
Post Reply
User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

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

Post 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?
My native language is russian. Sorry if my messages are difficult to read.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

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

Post 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

User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

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

Post 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 :)
My native language is russian. Sorry if my messages are difficult to read.

User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

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

Post 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?
My native language is russian. Sorry if my messages are difficult to read.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

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

Post 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

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

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

Post 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

Post Reply

Return to “Modding help”