How do I use the insert, caninsert and isvalid? methods
How do I use the insert, caninsert and isvalid? methods
I need to insert an z amount of ore into a chest in x y position, and i need to check if I can, how can I do it? I dont know how can I use those methods, can somebody explain?
Also, the onmined event isnt at the wiki.
Do the onmined event create a table like event.minedentity??
Do the onmined event exist? It is not working for me :S
Also, the onmined event isnt at the wiki.
Do the onmined event create a table like event.minedentity??
Do the onmined event exist? It is not working for me :S
Re: How do I use the insert, caninsert and isvalid? methods
I just added note about this event here:
https://forums.factorio.com/wiki/inde ... ineditem_2
https://forums.factorio.com/wiki/inde ... ineditem_2
Re: How do I use the insert, caninsert and isvalid? methods
what about how to use the insert, caninsert and isvalid methods?
:S
:S
Re: How do I use the insert, caninsert and isvalid? methods
Can somebody please give me an example of use of those methods??
I know how to use the insert method with the player (game.geplayer().insert{name= , amount= })
So to use it with an entity (container) I guess is something like game.findentity(somearguments).insert{name= ,amount= })
help?
I know how to use the insert method with the player (game.geplayer().insert{name= , amount= })
So to use it with an entity (container) I guess is something like game.findentity(somearguments).insert{name= ,amount= })
help?
Re: How do I use the insert, caninsert and isvalid? methods
Was playing around for a few hours with this (because I can't figure out how to set it to find an existing chest) but this should give at least a bit of an example:
if you know how to make the game find an existing entity you should be able to replace with the proper code. And if you do know how you should post it here for everyone to see 
I was trying to use and the game just threw an error about trying to perform arithmetic on 'wooden'.
The reason I was trying to use "wooden-chest" was because in util.lua the function findfirstentity says it needs a name, I assumed it needed the name of the entity:
also I couldn't get createboundingbox to work or i could have shortened the first portion of findfirstentity to createboundingbox(-1,1,1,-1) or at least I think that's how it should have worked
Code: Select all
glob.chest = game.createentity{name="steel-chest", position={x=13, y=3}}
local chestinventory = glob.chest.getinventory(defines.inventory.chest)
if chestinventory.isvalid() and chestinventory.caninsert({name="iron-ore", count="200"}) then
chestinventory.insert({name="iron-ore", count="200"})
end
Code: Select all
game.createentity{name="steel-chest", position={x=13, y=3}}

I was trying to use
Code: Select all
glob.chest = util.findfirstentity({topleft = {x = -1, y = 1}, bottomright = {x = 1, y = -1}}, "wooden-chest")
The reason I was trying to use "wooden-chest" was because in util.lua the function findfirstentity says it needs a name, I assumed it needed the name of the entity:
Code: Select all
function findfirstentity(boundingbox, name)
for _, entity in ipairs(game.findentities(boundingbox)) do
if entity.name == name then
return entity
end
end
return nil
end
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me
Or drop into #factorio on irc.esper.net
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me

Or drop into #factorio on irc.esper.net
Re: How do I use the insert, caninsert and isvalid? methods
I dont know how, but I managed a list of entities with the onplace event.
Re: How do I use the insert, caninsert and isvalid? methods
care to share your code? I've not gotten into lua much but there's not much documentation right now so anything i can find helps me learn 

<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me
Or drop into #factorio on irc.esper.net
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me

Or drop into #factorio on irc.esper.net
Re: How do I use the insert, caninsert and isvalid? methods
http://lua-users.org/wiki/TutorialDirectory
And ill share the code when I finish it, is incomplete so it wont work
And ill share the code when I finish it, is incomplete so it wont work
Re: How do I use the insert, caninsert and isvalid? methods
cool, thanks
I've used a tiny bit of lua (with minecraft's computercraft turtles) but not much
This will probably help out


<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me
Or drop into #factorio on irc.esper.net
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me

Or drop into #factorio on irc.esper.net
Re: How do I use the insert, caninsert and isvalid? methods
Have you managed to get this working yet (not begging/whining just wondering)? I finally managed to get this working for me lol
if you (or anyone else reading this) has a better way let me/us know
(I'll even ask nicely. Pleeeeeease?
)

Code: Select all
game.onevent = function(event)
if event.name == "onbuiltentity" then
if event.createdentity.name=="wooden-chest" then
entities = game.findentities{topleft = {x = event.createdentity.position.x - .1, y = event.createdentity.position.y - .1}, bottomright = {x = event.createdentity.position.x + .1, y = event.createdentity.position.y + .1}}
built = "something"
end
end--end onbuiltentity
if event.name == "ontick" then
if built~=nil then
for fieldName, _ in pairs(entities) do
chest=entities[fieldName].getinventory(defines.inventory.chest)
end
if chest.isvalid() and chest.caninsert({name="iron-ore", count="200"}) then
chest.insert({name="iron-ore", count="200"})
game.showmessagedialog("Gratis")
end
--built=nil --remove first comment to work on every chest instead of just the first
end
end --end ontick
end --end onevent


<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me
Or drop into #factorio on irc.esper.net
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me

Or drop into #factorio on irc.esper.net
Re: How do I use the insert, caninsert and isvalid? methods
I tried to read the code, but I didn't understand what are you trying to achieve.FreeER wrote:Have you managed to get this working yet (not begging/whining just wondering)? I finally managed to get this working for me lol
if you (or anyone else reading this) has a better way let me/us knowCode: Select all
game.onevent = function(event) if event.name == "onbuiltentity" and event.createdentity.name=="wooden-chest" then entities = game.findentities{topleft = {x = event.createdentity.position.x - .1, y = event.createdentity.position.y - .1}, bottomright = {x = event.createdentity.position.x + .1, y = event.createdentity.position.y + .1}} built = "something" end end--end onbuiltentity if event.name == "ontick" then if built~=nil then for fieldName, _ in pairs(entities) do chest=entities[fieldName].getinventory(defines.inventory.chest) end if chest.isvalid() and chest.caninsert({name="iron-ore", count="200"}) then chest.insert({name="iron-ore", count="200"}) game.showmessagedialog("Gratis") end --built=nil --remove first comment to work on every chest instead of just the first end end --end ontick end --end onevent
(I'll even ask nicely. Pleeeeeease?
)
If you want to place 200 iron plates in every chest the player builds, you can do this:
Code: Select all
game.onevent = function(event)
if event.name == "onbuiltentity" and event.createdentity.name == "wooden-chest" then
local chestinventory = event.createdentity.getinventory()
if chestinventory.caninsert({name="iron-ore", count="200"}) then
chestinventory.insert({name="iron-ore", count="200"})
game.showmessagedialog("Gratis")
end
end
Re: How do I use the insert, caninsert and isvalid? methods
Yes, I had it working, but my computer broke, so I cant release the mod...
I will release it when the comp is fixed
I will release it when the comp is fixed
Re: How do I use the insert, caninsert and isvalid? methods
For the most part i was trying to determine how to find an entity (even if it had been created earlier in the game) using findentities if you already had the x y coords (or, say the first within a few steps of the player). I didn't (and still don't really) understand the result you get from findentities so I wasn't sure how to take that result and use it. I ended up copying code from several places (lol) and managing to get it working with findentities.kovarex wrote: I tried to read the code, but I didn't understand what are you trying to achieve.
I'd explain more (if you needed me to) but i'm about to be late for college...
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me
Or drop into #factorio on irc.esper.net
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me

Or drop into #factorio on irc.esper.net