[Solved] Insert to Player Inventory
Posted: Thu Sep 17, 2020 1:06 am
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:
Second Snippet (continued from first):
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