Kill player

Place to get help with not working mods / modding interface.
Post Reply
TheLaurens
Manual Inserter
Manual Inserter
Posts: 4
Joined: Wed Dec 24, 2014 11:20 pm
Contact:

Kill player

Post by TheLaurens »

I want the player to die, now I should be able to call the die method, but I am not sure how to reach it, is it part of character?
if so: game.player.character.die() gives: attempt to index character, no such field (nil value)

how do I call the die method? Or should I kill the player in a different way?

User avatar
L0771
Filter Inserter
Filter Inserter
Posts: 516
Joined: Tue Jan 14, 2014 1:51 pm
Contact:

Re: Kill player

Post by L0771 »

TheLaurens wrote:I want the player to die, now I should be able to call the die method, but I am not sure how to reach it, is it part of character?
if so: game.player.character.die() gives: attempt to index character, no such field (nil value)

how do I call the die method? Or should I kill the player in a different way?
i tested game.player.character.die() and... i lost the game...
where use this code?

try with this

Code: Select all

if game.player.character then
game.player.character.die()
end

TheLaurens
Manual Inserter
Manual Inserter
Posts: 4
Joined: Wed Dec 24, 2014 11:20 pm
Contact:

Re: Kill player

Post by TheLaurens »

I think I figured out what exactly the problem is, I am killing the player after he deleted the bridge he was standing on. However I think the player switches to god mode or something when he falls into the water (if you fall in, you become invisible and can fly around and still remove things), and you can probably not kill that...

Code: Select all

game.onevent(defines.events.onpreplayermineditem, function(event)
	if event.entity.name == "bridge" then
		name = game.gettile(event.entity.position.x, event.entity.position.y).name
		len = string.len (game.gettile(event.entity.position.x, event.entity.position.y).name)
		game.settiles{{name = string.sub(game.gettile(event.entity.position.x, event.entity.position.y).name,1,len-2), position = event.entity.position}}
		--game.player.print(string.sub(game.gettile(event.entity.position.x, event.entity.position.y).name,1,len-2))
		if math.floor(game.player.position.x) == math.floor(event.entity.position.x) and math.floor(game.player.position.y) == math.floor(event.entity.position.y) then
			if game.player.character then
				game.player.print("in here!")
				game.player.character.die()
			end
		end
	end
end
)
The thing is though, I am calling it in the on preplayermined event, so it should do it before the player is dropped in the water right?

Rahjital
Filter Inserter
Filter Inserter
Posts: 435
Joined: Thu May 29, 2014 10:44 am
Contact:

Re: Kill player

Post by Rahjital »

Oh! Another person working on a bridge mod, I see? I hope you'll do better than me, I currently somehow got Factorio crash randomly and I have no idea how.

You are doing this by changing water tiles to some temporary walkable tiles of yours that you remove when you destroy the bridge, right? If so, reverting the tiles to water destroys any entity - the equivalent of calling entity.destroy(), NOT entity.die(). If a player has no character, they enter a sandbox mode where they can do anything. You should be able to end the game manually with this command:

Code: Select all

game.setgamestate = {gamefinished = true, playerwon = false}
Note that this would most likely end the game for all players in multiplayer, so you might want some special logic for that. Probably not much concernse since you are using game.player (which make the mod singleplayer-only), but if you'll ever want to port it for multiplayer, it's something to keep in mind.

Alternatively, you could also spawn a new player character at a valid position, assign it to the player and then kill it. That's a bit of a more convoluted way to do it, but it should work fine with more players.

Post Reply

Return to “Modding help”