CONSUME FISH WHEN EXECUTING A CUSTOM ACTION

Place to get help with not working mods / modding interface.
Post Reply
theandrews9603
Manual Inserter
Manual Inserter
Posts: 2
Joined: Tue May 17, 2022 10:01 am
Contact:

CONSUME FISH WHEN EXECUTING A CUSTOM ACTION

Post 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.

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2540
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: CONSUME FISH WHEN EXECUTING A CUSTOM ACTION

Post 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
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

theandrews9603
Manual Inserter
Manual Inserter
Posts: 2
Joined: Tue May 17, 2022 10:01 am
Contact:

Re: CONSUME FISH WHEN EXECUTING A CUSTOM ACTION

Post 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?

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

Re: CONSUME FISH WHEN EXECUTING A CUSTOM ACTION

Post 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

Post Reply

Return to “Modding help”