Is It Possible to Change the Player Starting Equipment?

Place to get help with not working mods / modding interface.
TheLastRonin
Inserter
Inserter
Posts: 23
Joined: Thu Jan 22, 2015 8:22 pm
Contact:

Is It Possible to Change the Player Starting Equipment?

Post by TheLastRonin »

Can I change the starting equipment of the player somehow?
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Is It Possible to Change the Player Starting Equipment?

Post by Adil »

Example from factorio/data/base/scenarios/freeplay/control.lua :

Code: Select all

game.onevent(defines.events.onplayercreated, function(event)
  local player = game.getplayer(event.playerindex)
  player.insert{name="iron-plate", count=8}
  player.insert{name="pistol", count=1}
  player.insert{name="basic-bullet-magazine", count=10}
  player.insert{name="burner-mining-drill", count = 1}
  player.insert{name="stone-furnace", count = 1}
  if (#game.players <= 1) then
    game.showmessagedialog{text = {"msg-intro"}}
  end
end)
Either tweak those, or make your own mod folder in type analogous code there.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
TheLastRonin
Inserter
Inserter
Posts: 23
Joined: Thu Jan 22, 2015 8:22 pm
Contact:

Re: Is It Possible to Change the Player Starting Equipment?

Post by TheLastRonin »

Adil wrote:Example from factorio/data/base/scenarios/freeplay/control.lua :

Code: Select all

game.onevent(defines.events.onplayercreated, function(event)
  local player = game.getplayer(event.playerindex)
  player.insert{name="iron-plate", count=8}
  player.insert{name="pistol", count=1}
  player.insert{name="basic-bullet-magazine", count=10}
  player.insert{name="burner-mining-drill", count = 1}
  player.insert{name="stone-furnace", count = 1}
  if (#game.players <= 1) then
    game.showmessagedialog{text = {"msg-intro"}}
  end
end)
Either tweak those, or make your own mod folder in type analogous code there.
How do I use a mod to clear items from the inventory or prevent them from being added?
Kexík
Long Handed Inserter
Long Handed Inserter
Posts: 71
Joined: Mon Dec 01, 2014 12:52 pm
Contact:

Re: Is It Possible to Change the Player Starting Equipment?

Post by Kexík »

There will be probably some simply solution, i think DyTech override starting items so you could check it. I never done it but i think starting items from base factorio should be in inventory when your mod defines.events.onplayercreated is called, so you could check player inventory and delete them if they are there.
User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Is It Possible to Change the Player Starting Equipment?

Post by FreeER »

Kexík wrote:i think starting items from base factorio should be in inventory when your mod defines.events.onplayercreated is called, so you could check player inventory and delete them if they are there.
From what I've seen this is the case, you can use

Code: Select all

require "defines"
game.onevent(defines.events.onplayercreated, function(event)

  -- "hard" way shown, easy way would be getting the player and use player.removeitem with the name and a high count

  local inventory = game.players[event.playerindex].getinventory(defines.inventory.playermain)
  -- at this point you could call "inventory.clear()" if you wanted to delete everything in the main inventory
  -- other inventories are also defined in the defines.lua file (data/core/lualib/defines.lua)

  local contents = inventory.getcontents()
  for name, count in pairs(contents) do
    if name == "name of item to remove" or name == "name of another item to remove" then
      inventory.remove({name=name, count=count})
    end
  end
  -- can insert with inventory.insert({name="item-name", count=some_count})
end)
As for preventing items being in the player's inventory (you can't prevent them from being added unless the mod author provides a script interface for that) the only thing you can do is continuously check the player's inventory for those items in defines.event.ontick inside an if statement checking that event.tick % 60 == 0 (so it's once every second instead of 60 times a second), you can use player.getitemcount("item name") if you just want to check for specific items being on the player (for instance, to damage the player for having 'hot' or 'radioactive' items in their hands), for a specific inventory, you'd need to get the inventory and use inventory.getitemcount("item name").
cartmen180
Filter Inserter
Filter Inserter
Posts: 358
Joined: Fri Jul 25, 2014 2:53 pm
Contact:

Re: Is It Possible to Change the Player Starting Equipment?

Post by cartmen180 »

Code: Select all

game.onevent(defines.events.onplayercreated, function(event)
	  local character = game.player.character
	  character.clearitemsinside()
	  character.insert{name="stone-axe", count=1}
	end)
this clears anything the character is given when starting a new game and adds what you want, in this case a stone axe.
Check out my mods
drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

Re: Is It Possible to Change the Player Starting Equipment?

Post by drs9999 »

cartmen180 wrote:

Code: Select all

game.onevent(defines.events.onplayercreated, function(event)
	  local character = game.player.character
	  character.clearitemsinside()
	  character.insert{name="stone-axe", count=1}
	end)
this clears anything the character is given when starting a new game and adds what you want, in this case a stone axe.
But only in SP ;)
For MP you have to loop through all players.

Btw is game.player.character really required? Shouldn't it work with game.player only?
Because in sandbox there is no character attached to the player, so it will also throw an error.
User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Is It Possible to Change the Player Starting Equipment?

Post by FreeER »

cartmen180 wrote:game.player.character
Slight issue here, it will only work in single player. MP would need to use game.player[event.playerindex] instead (btw, I'd forgotten about the clearitemsinside method, and the wiki shows it on the player but testing shows it works on the character too, hm wiki version history has "LuaEntity::clearitemsinside and getitemcount works on all entities (hopefully)" on 5.0 so I should have looked harder lol).
drs9999 wrote:For MP you have to loop through all players.
??? what, why? Especially during onplayercreated that doesn't make sense to me.
drs9999 wrote:Shouldn't it work with game.player only?
yes, it does. Which is nice for compatibility with sandbox
drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

Re: Is It Possible to Change the Player Starting Equipment?

Post by drs9999 »

FreeER wrote:??? what, why? Especially during onplayercreated that doesn't make sense to me.
game.player doesn't exist in mp-games it is game.players then , where players is an array that contains all player.

But yes, I was too fast. Obviously if onPlayerCreated returns the playerIndex it isn't needed to iterate through all players. You can get the explicit player from the array directly.
User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Is It Possible to Change the Player Starting Equipment?

Post by FreeER »

drs9999 wrote:...
Here's a list of events with playerindex (as of 11.11)
playerindex events
Post Reply

Return to “Modding help”