Add to Player Starting Inventory

Place to get help with not working mods / modding interface.
Post Reply
User avatar
Templarfreak
Burner Inserter
Burner Inserter
Posts: 15
Joined: Thu Oct 05, 2017 2:21 pm
Contact:

Add to Player Starting Inventory

Post by Templarfreak »

So I'm taking my first go at making a mod and everything has gone pretty smoothly so far, except this. Everything I look at to try and figure out what to do is several years old and thus out-dated information (a lot say to do require "defines" which is a file that no longer exists). Any help would be appreciated.

User avatar
5thHorseman
Smart Inserter
Smart Inserter
Posts: 1193
Joined: Fri Jun 10, 2016 11:21 pm
Contact:

Re: Add to Player Starting Inventory

Post by 5thHorseman »

Check out my mod, Laziest Bastard. Link in my Sig. It's current and almost literally all it does is add to the player's inventory at game start.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Add to Player Starting Inventory

Post by darkfrei »

Start any game and open control.lua it the /temp/

Vanilla:

Code: Select all

script.on_event(defines.events.on_player_created, function(event)
  local player = game.players[event.player_index]
  player.insert{name="iron-plate", count=8}
  player.insert{name="pistol", count=1}
  player.insert{name="firearm-magazine", count=10}
  player.insert{name="burner-mining-drill", count = 1}
  player.insert{name="stone-furnace", count = 1}
  player.force.chart(player.surface, {{player.position.x - 200, player.position.y - 200}, {player.position.x + 200, player.position.y + 200}})
  if (#game.players <= 1) then
    game.show_message_dialog{text = {"msg-intro"}}
  else
    player.print({"msg-intro"})
  end
  silo_script.gui_init(player)
end)

script.on_event(defines.events.on_player_respawned, function(event)
  local player = game.players[event.player_index]
  player.insert{name="pistol", count=1}
  player.insert{name="firearm-magazine", count=10}
end)
Last edited by darkfrei on Sat Nov 04, 2017 10:53 pm, edited 1 time in total.

User avatar
Templarfreak
Burner Inserter
Burner Inserter
Posts: 15
Joined: Thu Oct 05, 2017 2:21 pm
Contact:

Re: Add to Player Starting Inventory

Post by Templarfreak »

Thanks, 5th, that worked. Very simple, just not very visible to learn unfortunately. Still worked very well, though. :D

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Add to Player Starting Inventory

Post by eradicator »

(Bit late to the party but posting for future readers of this thread:)

In my mod Hand Crank Generator i add an item to the starting inventory or quickbar depending on player settings. When adding things to the player inventory you need to be aware that different scenarios handle player creation differently. Depending on how you do things your method might not work in some scenarios. Here's the code i use in above mod:

Code: Select all

script.on_event(defines.events.on_player_created, function(event)
  --inserts the item into the players inventory when they first join the map
  --as we don't care about anything else we can just do a direct call to
  --insert into the inventory.
  --Calling .insert diretcly on the player object instead of trying to guess
  --what valid inventories a player might have. This circumvents some oddities
  --with scenario maps where the player.character is not created simultaneously
  --with the player as such.
  
  local player  = game.players[event.player_index] --"pointer" to the player object
  local success = false
  
  if (config.start_with_item_in_quickbar == false) then
    success = (player.insert{name="hand-crank-generator"} > 0) --bool
  else
    --Sandbox players and normal players have different inventories,
    --so we need to know the type if we want to insert into the quickbar
    local typ = player.controller_type
    local inv --variable to hold the pointer to the player inventory
    if typ == defines.controllers.god then
      inv = player.get_inventory(defines.inventory.god_quickbar   ) --freeplay
    elseif typ == defines.controllers.character then
      inv = player.get_inventory(defines.inventory.player_quickbar) --sandbox
      end
    if inv and inv.valid then
      success = (inv.insert{name="hand-crank-generator"} > 0) --bool
      end
    if success == false then
      --backup plan (quickbar full?)
      success = (player.insert{name="hand-crank-generator"} > 0) --bool
      end
    end

  if success == false then
    game.print('Failed to find inventory for Hand Crank Generator') --TODO: localize, fix for PvP scenario
  elseif (success == true) then
    -- game.print('success')
  end
end)
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.

User avatar
Templarfreak
Burner Inserter
Burner Inserter
Posts: 15
Joined: Thu Oct 05, 2017 2:21 pm
Contact:

Re: Add to Player Starting Inventory

Post by Templarfreak »

Probably a good idea for certain mods, but I think mine is fine. I'd rather leave scenarios and other things untouched, if what 5th posted does not affect them already. I am only adding certain items into the inventory for convenience sake, they are not necessities to play the mod. It just makes it so you have potential jump right into the tech tree instead of having to go through a little bit of Vanilla tech tree first.

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Add to Player Starting Inventory

Post by bobingabout »

As a modder myself (and if you don't know my name, what rock have you been living under?), I found adding to the list of starting items... although easy to do, annoying in it's method.

While trying to work with my classes mod, one of the things I was trying to do was replace the player entity with a new one (with specific stats set) and get his own starting inventory.

however, everything else added by other mods therefore gets deleted.

What I was looking for (and unable to find due to the implementation) was a script method to find this list of things to add, so I could add and remove objects from it specific to my new class, which would also allow items added by other mods to still exist when I replace the player entity.


So I looked at an alternative method, Migrating the player entity to another type, however with it's current implementation, it doesn't work with the player unit, the only real way to migrate the player entity is to delete it and add the new one. (Migration would be a lot easier on the player too)
Which gives you two options. Do you delete the player entity when you open the GUI (which leaves the player in a state where he can freely explore in godmode, and uncover the map, even if he can't interact with the world otherwise, deleting the knowledge of what items he was holding along with it) and create a new one when he chooses the class, or do you leave the player with an entity (and possible mining and adding items to his inventory in the process), and then migrate the entity afterwards (Which has no easy copy items function, modular armor for example would be a huge pain to try and copy manually from the old entity to the new), however the latter also means you end up with both sets of inventory items (Those that the base game and any other mods add, plus the ones you want for your class), this is less of an issue though, because I could add a script to remove certain items too (such as deleting the pistol to give you a SMG on a fighter class)

I THINK after a few back and forths on the forums here, one of the devs added proper player entity migration support for 0.16.

But anyway, this is the main reason why I didn't release my classes mod.

along with that, the issue of death interception creating a dead player inventory chest only intercepting the death of a player entity currently controlled by the a player, and not disconnected player character entities is the main reason why I didn't add a multiple bodies mod.

And although no real issues with this part, there was also going to be another mod of the set that gives you a personal player enhancement skill tree, the issue was that I couldn't decide how the player should gain skill points, and after the failure of the previous two, lost motivation to actually write it.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

Post Reply

Return to “Modding help”