Creating an Item with selectable place_results

Place to get help with not working mods / modding interface.
Post Reply
jona5
Burner Inserter
Burner Inserter
Posts: 14
Joined: Sat Mar 25, 2017 1:19 pm
Contact:

Creating an Item with selectable place_results

Post by jona5 »

Hi,

i just started creating my first mod but now i kinda stuck at some basic stuff i cannot figure out.

i want to have an item where the user can select which entity it places by cycling through a list of entities. this should be done while the user has the item stack in its hand with a keyboard press (kinda what rotating does).

soooo i figured that there is no way that an ItemPrototype can have multiple place_results or that the place_result can be changed during control.lua.

i tried to do it some hacky way by creating multiple (hidden) item prototypes from the (craftable) base item for each placeable entity and changing the players item stack when pressing the keyboard key. This works in a way, but creates a multitude of problems, for example when the player places this stack back into its inventory or in a container i would need to change the item stack back to the base item prototype.

i am getting to the point where i need to work around the problems of a workaround and my (very basic) lua skills are running thin.

is there actually no way to accomplish this in a handy way?

thanks! :)

321freddy
Fast Inserter
Fast Inserter
Posts: 125
Joined: Fri Sep 23, 2016 10:16 pm
Contact:

Re: Creating an Item with selectable place_results

Post by 321freddy »

Firstly welcome to the forums! :)

I think your approach isn't too bad. The only thing you need to add is a function to scan the inventory of the player or the entity that he has opened and revert your item to it's original prototype.

Use these events to detect when something in the players inventories changed:
  • on_player_cursor_stack_changed
  • on_player_quickbar_inventory_changed
  • on_player_main_inventory_changed
You can access the entity the player has opened/selected through player.opened / player.selected (http://lua-api.factorio.com/latest/LuaC ... l.selected)
You scan the players inventories inside these events and then also use player.opened/selected to scan any inventories the player could have put the item into.

The scanning function would look something like this:

Code: Select all

function scanInventory(inventory) -- calls replaceStack on every slot in an inventory, inventory is LuaInventory
	for i = 1, #inventory do
      replaceStack(inventory[i])
   end
end

function replaceStack(stack) -- replaces the stack if it's the modified item, stack is LuaItemStack
	if stack.valid_for_read then
		if stack.name == "MODIFIED ITEM NAME" then
			stack.set_stack{ name = "ORIGINAL ITEM NAME", stack.count }
		end
	end
end

jona5
Burner Inserter
Burner Inserter
Posts: 14
Joined: Sat Mar 25, 2017 1:19 pm
Contact:

Re: Creating an Item with selectable place_results

Post by jona5 »

hey freddy,

thank you very much for answering. i was already heading that way, but it felt really complicated and awkward to handle it this way - at least how i was doing it. :)

so thanks for your examples, i will check that out.

is there anyway no way to have the place_result of an Item dynamically changed? Feels really restricting or is there a reason i am not able to see... ? ;)

cheers

(edited for clarity)

321freddy
Fast Inserter
Fast Inserter
Posts: 125
Joined: Fri Sep 23, 2016 10:16 pm
Contact:

Re: Creating an Item with selectable place_results

Post by 321freddy »

place_result can only be dynamically changed inside your data.lua but not during a game.
However you can read the place result with item.place_result (http://lua-api.factorio.com/latest/LuaI ... ace_result)
Note that this parameter is read-only

The only other way I can think of is using a blueprint-book as the item, filling all your place result as blueprints in there and then automatically reviving the blueprint ghosts once placed, but that would have it's own set of problems...

EDIT: Or you could have only one item and detect entity rotation once placed to dynamically replace the entity based on rotation.

Post Reply

Return to “Modding help”