read and write tool durability -Solved

Place to get help with not working mods / modding interface.
Post Reply
CNR_Thunder
Burner Inserter
Burner Inserter
Posts: 6
Joined: Sat Nov 07, 2015 9:44 am
Contact:

read and write tool durability -Solved

Post by CNR_Thunder »

Hi Guys

this is my first Post and I hope its at the right place.
I try to make a mod and need your help.

I want to read and write the durability from tools

Code: Select all

chest = --<<--- is the placed chest

for item, count in pairs(chest.get_contents()) do

game.players[1].print(tostring(item))  --<<-----prints the name of the item like "iron-axe"
game.players[1].print(tostring(item.durability)) --<<---prints nil

end

in the wiki is the durability under the read and write but how?^^
https://forums.factorio.com/wiki/inde ... durability

My goal is atm to simulate a tool use in a chest so it will loose durability over time. Do someone know how to access the durability stat of an item?

Thanks for your help

Thunder
Last edited by CNR_Thunder on Tue Nov 24, 2015 3:51 pm, edited 2 times in total.

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: read and write tool durability

Post by prg »

Seems like this key should exist for items of type tool and mining-tool and be nil for other items. Hold an item in the cursor and run

Code: Select all

/c game.local_player.print(game.local_player.cursor_stack.name .. \": \" .. tostring(game.local_player.cursor_stack.durability))
iron-plate: nil
iron-axe: 3975
science-pack-1: 1
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

CNR_Thunder
Burner Inserter
Burner Inserter
Posts: 6
Joined: Sat Nov 07, 2015 9:44 am
Contact:

Re: read and write tool durability

Post by CNR_Thunder »

Thanks for your reply

I tested your code like this and it works

Code: Select all

/c game.local_player.print(game.local_player.cursor_stack.name .. ": " .. tostring(game.local_player.cursor_stack.durability))
ok I'm stupid chest.get_contents() only contains the name and not the item itself -.-

so I need to read the chest slot for slot
can you help me with one two things pls

is there an easy way to get the max size of the chest for my loop like chest.inventory_size

and can I add an stat to my items in the prototype item.lua like the durability
for example

Code: Select all

    type = "tool",
    name = "test",
    icon = "__base__/graphics/icons/science-pack-1.png",
    flags = {"goes-to-main-inventory"},
    subgroup = "science_pack",
    order = "a[science-pack-1]",
    stack_size = 1,
    durability = 1000,
	--durability_description_key = "description.science-pack-remaining-amount"
	anything = 2,   ----<<<<-------
an then use it in the code like this

Code: Select all

print stack.durability -->>1000
print stack.anything --->> 2
I tested this way and get an error "LuaItemStack doesen't contain key anything" so I think it isn't so easy to add new stats to the items


thanks again

Thunder

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: read and write tool durability

Post by prg »

Hm, if you call pairs() on .get_inventory() you only get some stupid userdata thing back. But you can still get at the number of elements that are actually supposed to be in there with # and then iterate over that with something like

Code: Select all

local inv = game.local_player.selected.get_inventory(1)
for i = 1, #inv do
    if inv[i].valid_for_read then game.local_player.print(inv[i].name .. ": " .. tostring(inv[i].durability)) end
end
And no, you can't just make up stats like that.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

Post Reply

Return to “Modding help”