[Solved] Insert to Player Inventory

Place to get help with not working mods / modding interface.
rookhaven
Inserter
Inserter
Posts: 22
Joined: Sun May 07, 2017 9:21 pm
Contact:

[Solved] Insert to Player Inventory

Post by rookhaven »

I know there are 2582625620620 posts on this subject, but I can't get the inventory logic to work in my mod script which is called from control.lua. I actually took the for loop from Space Exploration. Neither of these snippets work, although the notification to the player works fine as does the research queue. So I know I'm referencing the player correctly, but I can't seem to access the inventory properly. I'm using this in a normal freeplay new start. I tried the god_mode defines as well - also didn't work. In the first snippet the for loop executes, but the inner if isn't true. In the second snippet, the else path with the player.print is what's executed.

Years ago, I used to use the simple player.insert(<items) which doesn't work anymore either - alot of posts still list that and the API looks like it should work but I'm not getting it to work.

Please help and thank you in advance.

First Snippet:

Code: Select all

	local player = game.players[event.player_index]
	if not (player and player.valid) then return end
	player.force.research_queue_enabled = true

	for _, inv in pairs({defines.inventory.character_main}) do
		if player.get_inventory(inv) then
			player.get_inventory(inv).clear()
			player.get_inventory(inv).insert({name="k2cp-city", count=4})
		end
	end

Second Snippet (continued from first):

Code: Select all

	local inv = player.get_main_inventory()
	if (inv) then
		inv.clear();
		inv.insert({name="iron-plate", count=50})
	else
		player.print("Main inventory not returned")	
	end
Last edited by rookhaven on Thu Sep 17, 2020 8:49 pm, edited 2 times in total.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3717
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Insert to Player Inventory

Post by DaveMcW »

Both of your snippets work perfectly if I modify them to run from the console. Something else is wrong with your mod.

Code: Select all

/c local player = game.player
local inv = player.get_main_inventory()
if (inv) then
  inv.clear();
  inv.insert({name="iron-plate", count=50})
else
  player.print("Main inventory not returned")	
end
rookhaven
Inserter
Inserter
Posts: 22
Joined: Sun May 07, 2017 9:21 pm
Contact:

Re: Insert to Player Inventory

Post by rookhaven »

Yep I was seeing the same thing. I've always been able to execute regular inventory inserts in the console.

What's odd is that they player references and everything else works fine - just these two inventory snippets don't work. Not sure what else to check.

Thanks for confirming tho.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Insert to Player Inventory

Post by eradicator »

rookhaven wrote: Thu Sep 17, 2020 1:06 am I'm using this in a normal freeplay new start.
You have to use the remote interface to add starting items in freeplay. The player does not have a character until the end of the intro. And you really shouldn't Inventory.clear() because that will destroy every other mods starting items - and those might be essential to play a mod at all.

See my tutorial for detailed explanations.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
rookhaven
Inserter
Inserter
Posts: 22
Joined: Sun May 07, 2017 9:21 pm
Contact:

Re: Insert to Player Inventory

Post by rookhaven »

Ok thank you. Marking resolved.
Post Reply

Return to “Modding help”