How to change inserters' destinations

Place to get help with not working mods / modding interface.
Seelo
Burner Inserter
Burner Inserter
Posts: 10
Joined: Wed Jun 18, 2014 1:33 pm
Contact:

How to change inserters' destinations

Post by Seelo »

Hello,
I recently started working on my first small mod (first one that is meant to be consistent), and one of my ideas is to have an inserter with ability to choose where from and where to the inserter should be transporting items.

I set up an event listener onbuiltentity, obtained handle createdentity to variable called z, and now comes the tricky part. I wanted to use:

Code: Select all

z.pickup_position = {0, -4}
but when placed I get the lua error that pickup_position is not a property of Lua/Entity.
Shouldn't Inserter, which is based on EntityWithHealth, which is based on Entity, have such property?
pickup_position certainly is used when prototyping the inserter, so I like to think my logic was not completely wrong.

Was it so dumb to try and change things that are in prototypes? Do I have to rewrite whole inserter behaviour to achieve the effect or is there a gimmick that I don't know about? (I know a solid bit of C++, but Lua is new to me so I have some trouble when it comes to classes and inheritance).

I'd be very thankful for any help on the matter, because I don't fancy writing some quasi inserters from scratch until it's necessary.
User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: How to change inserters' destinations

Post by FreeER »

Seelo wrote:Was it so dumb to try and change things that are in prototypes?
Not dumb per say...just that Factorio's current implementation does not allow for changing prototype definitions through the control.lua script. You have to create an entirely new inserter prototype just to make one with a different pickup_position (the names need to be different as well, if that's not obvious), plus an item to represent that entity if you want the player to place it, plus a recipe, and maybe tech (depending upon the mod)...
Seelo wrote:and one of my ideas is to have an inserter with ability to choose where from and where to the inserter should be transporting items.
This...would probably need the control.lua script to implement the inserter behavior of getting and moving the items, instead of an actual inserter. That or have a lot of inserter prototypes with different pickup_positions (and names) and the control.lua swapping between entities as needed....

edit: you can see here on the wiki what is available to the control script, there is some limited (and read only) access to prototype definitions via game.itemprototypes["name"], game.entityprototypes["name"], game.player.force.recipes["name"], and game.player.force.technologies["name"]. You can call help on any of those (assuming they're indexed with a valid name) and send it to game.player.print to see what you have access to, for example game.player.print(game.itemprototypes["iron-ore"].help()).
Seelo
Burner Inserter
Burner Inserter
Posts: 10
Joined: Wed Jun 18, 2014 1:33 pm
Contact:

Re: How to change inserters' destinations

Post by Seelo »

Thanks, that's what I was afraid might be the case. I had no access to data:extend from control.lua, so prototyping new inserters on the go does not work anyway. The only "solution" I can think of now is to create inserters for all possible configurations, and just call game.createentity with the correct name depending on what player chooses. Not gonna happen, I can already see how tons of prototypes completely mess things up.

Well, it seems that if factorio devs don't implement dynamic prototyping (or set inserter's positions as editable properties) I'll just have to create some clever item transporter to imitate inserter's behaviour.
User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: How to change inserters' destinations

Post by FreeER »

Seelo wrote:Not gonna happen, I can already see how tons of prototypes completely mess things up.
They don't mess things up too badly as long as you don't need to do anything the devs didn't design the type to allow, though it certainly makes the mod a bit harder to make and maintain. If you were to use guis for the user to choose which input/output positions are then you wouldn't need the recipes/items/tech just the entities that get replaced with createentity (and destroying the old of course). Using C++ inserters would have less overhead than a manual implementation (inherently since a manual implementation must go through the api calls rather than just c++ interaction), especially as you get into whether the 'inserter' should be placing it's 'load' into the main inventory of an entity or a different one; for instance a player wants it to place coal inside of a furnace, I think that just calling furnace.insert(itemstack) would try to place it in the furnace's input slot rather than the fuel slot, not certain on that; but another issue is that entity.caninsert(itemstack) only checks if part of the item stack can be inserted, not if all of it can be inserted so you have to make sure that you're not inserting more than the entity can actually hold and destroying the entire item stack (resulting in a loss of resources)...I'm sure you'd run into another issue or two as well (inserting into assemblers only if they have a recipe that takes that item as an ingredient for instance). Not only does all of these checks/work arounds make the code more complicated but it decreases it's speed in relation to a standard c++ coded inserter.

Of course, sometimes there's no choice but to do it entirely in Lua.
Seelo
Burner Inserter
Burner Inserter
Posts: 10
Joined: Wed Jun 18, 2014 1:33 pm
Contact:

Re: How to change inserters' destinations

Post by Seelo »

Yeah, that's why I wanted to avoid replicating inserters myself. I'll mess around making more prototypes for some time, and if that fails, well, I'll wait for devs to expand modding possibilities :P


EDIT:
Okay, that was unexpected, but few for loops in prototype allowed to make inserters with unique names and placement positions. From now on things should be easy, I'll have a dummy entity that can be crafted (and mined from inserters) and that will get replaced with desired inserter as soon as player specifies his configuration. Thanks for help!
Post Reply

Return to “Modding help”