Page 1 of 1

How do I use the insert, caninsert and isvalid? methods

Posted: Fri Mar 01, 2013 12:09 pm
by ficolas
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

Re: How do I use the insert, caninsert and isvalid? methods

Posted: Fri Mar 01, 2013 2:35 pm
by kovarex
I just added note about this event here:
https://forums.factorio.com/wiki/inde ... ineditem_2

Re: How do I use the insert, caninsert and isvalid? methods

Posted: Fri Mar 01, 2013 2:50 pm
by ficolas
what about how to use the insert, caninsert and isvalid methods?
:S

Re: How do I use the insert, caninsert and isvalid? methods

Posted: Fri Mar 01, 2013 11:24 pm
by ficolas
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?

Re: How do I use the insert, caninsert and isvalid? methods

Posted: Sat Mar 02, 2013 3:36 am
by FreeER
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:

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
if you know how to make the game find an existing entity you should be able to replace

Code: Select all

game.createentity{name="steel-chest", position={x=13, y=3}}
with the proper code. And if you do know how you should post it here for everyone to see :)


I was trying to use

Code: Select all

glob.chest = util.findfirstentity({topleft = {x = -1, y = 1}, bottomright = {x = 1, y = -1}}, "wooden-chest")
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:

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

Re: How do I use the insert, caninsert and isvalid? methods

Posted: Sat Mar 02, 2013 9:55 am
by ficolas
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

Posted: Sat Mar 02, 2013 10:31 am
by FreeER
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 :)

Re: How do I use the insert, caninsert and isvalid? methods

Posted: Sat Mar 02, 2013 10:42 am
by ficolas
http://lua-users.org/wiki/TutorialDirectory

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

Posted: Sat Mar 02, 2013 10:47 am
by FreeER
cool, thanks :) I've used a tiny bit of lua (with minecraft's computercraft turtles) but not much :) This will probably help out

Re: How do I use the insert, caninsert and isvalid? methods

Posted: Sun Mar 03, 2013 11:48 pm
by FreeER
Have you managed to get this working yet (not begging/whining just wondering)? I finally managed to get this working for me lol :)

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
 
if you (or anyone else reading this) has a better way let me/us know :) (I'll even ask nicely. Pleeeeeease? :lol: )

Re: How do I use the insert, caninsert and isvalid? methods

Posted: Mon Mar 04, 2013 11:45 am
by kovarex
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 :)

Code: 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
 
if you (or anyone else reading this) has a better way let me/us know :) (I'll even ask nicely. Pleeeeeease? :lol: )
I tried to read the code, but I didn't understand what are you trying to achieve.
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

Posted: Mon Mar 04, 2013 3:36 pm
by ficolas
Yes, I had it working, but my computer broke, so I cant release the mod...
I will release it when the comp is fixed

Re: How do I use the insert, caninsert and isvalid? methods

Posted: Mon Mar 04, 2013 6:26 pm
by FreeER
kovarex wrote: I tried to read the code, but I didn't understand what are you trying to achieve.
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.

I'd explain more (if you needed me to) but i'm about to be late for college...