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

Place to get help with not working mods / modding interface.
Post Reply
ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

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

Post 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

kovarex
Factorio Staff
Factorio Staff
Posts: 8078
Joined: Wed Feb 06, 2013 12:00 am
Contact:

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

Post by kovarex »

I just added note about this event here:
https://forums.factorio.com/wiki/inde ... ineditem_2

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

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

Post by ficolas »

what about how to use the insert, caninsert and isvalid methods?
:S

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

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

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

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

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

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

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

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

Post by ficolas »

I dont know how, but I managed a list of entities with the onplace event.

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

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

Post 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 :)
<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

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

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

Post by ficolas »

http://lua-users.org/wiki/TutorialDirectory

And ill share the code when I finish it, is incomplete so it wont work

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

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

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

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

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

Post 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: )
<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

kovarex
Factorio Staff
Factorio Staff
Posts: 8078
Joined: Wed Feb 06, 2013 12:00 am
Contact:

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

Post 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
 

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

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

Post 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

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

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

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

Post Reply

Return to “Modding help”