Page 1 of 1

[Solved] Insert to Player Inventory

Posted: Thu Sep 17, 2020 1:06 am
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

Re: Insert to Player Inventory

Posted: Thu Sep 17, 2020 4:26 am
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

Re: Insert to Player Inventory

Posted: Thu Sep 17, 2020 5:37 am
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.

Re: Insert to Player Inventory

Posted: Thu Sep 17, 2020 4:05 pm
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.

Re: Insert to Player Inventory

Posted: Thu Sep 17, 2020 8:47 pm
by rookhaven
Ok thank you. Marking resolved.