Page 1 of 1

CONSUME FISH WHEN EXECUTING A CUSTOM ACTION

Posted: Tue May 17, 2022 10:16 am
by theandrews9603
I've been trying to make a blink mod that teleports the player a short distance. I copied another mod that already does this but I tweaked the distance, the controls, and some other stuff. (I'm not planning on posting the mod right now: it's just for personal use, because I think combat needs defensive moves, like blocking and dodging but that's off-topic.)

What I want to do: consume 1 fish for each time the action is performed, i.e. consume 1 fish in inventory for each teleportation.

The rest of the mod already works as intended. I just need consumption of fish to balance it. Another solution would be a cooldown, but I don't know how to do that either as I'm new to programming and modding in Factorio.

Re: CONSUME FISH WHEN EXECUTING A CUSTOM ACTION

Posted: Tue May 17, 2022 1:36 pm
by FuryoftheStars
For a cooldown, in the code block that performs the action, you can store game.tick + number of ticks you want the cooldown to be for in the global table, indexed by player, then when the action is performed again, just check to see if game.tick >= stored value.

Rough mockup code (not tested!):

Code: Select all

function teleport(player) -- I'm not sure what your existing function looks like, but I'm assuming it in some way has access to the player that is teleporting
    if not global.teleportcooldown[player.index] or game.tick >= global.teleportcooldown[player.index] then
        -- teleport code
        global.teleportcooldown[player.index] = game.tick + 300 -- 300 ticks is 5 seconds; adjust to your liking
    end
end

Re: CONSUME FISH WHEN EXECUTING A CUSTOM ACTION

Posted: Tue May 17, 2022 5:43 pm
by theandrews9603
Didn't know how the game kept time; well now I do! Thanks. As I said, am a complete noob and still trying to read through the modding documentation.

Any ideas on how to consume fish through a function?

Re: CONSUME FISH WHEN EXECUTING A CUSTOM ACTION

Posted: Tue May 17, 2022 6:02 pm
by DaveMcW

Code: Select all

local consumed = player.remove_item{name="raw-fish", count=1}
if consumed == 1 then
  -- teleport code
else
  player.surface.create_entity{name="flying-text", position=player.position, render_player_index=player.index, color={r=1}, text="Out of fish"}
end