Page 1 of 1

Is It Possible to Change the Player Starting Equipment?

Posted: Sat Jan 24, 2015 7:42 am
by TheLastRonin
Can I change the starting equipment of the player somehow?

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

Posted: Sat Jan 24, 2015 1:10 pm
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.

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

Posted: Sun Jan 25, 2015 11:46 am
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?

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

Posted: Sun Jan 25, 2015 11:55 am
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.

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

Posted: Sun Jan 25, 2015 8:54 pm
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").

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

Posted: Sun Jan 25, 2015 10:07 pm
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.

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

Posted: Sun Jan 25, 2015 10:58 pm
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.

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

Posted: Sun Jan 25, 2015 11:10 pm
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

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

Posted: Sun Jan 25, 2015 11:37 pm
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.

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

Posted: Mon Jan 26, 2015 12:14 am
by FreeER
drs9999 wrote:...
Here's a list of events with playerindex (as of 11.11)
playerindex events