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?
Kill player
-
- Manual Inserter
- Posts: 4
- Joined: Wed Dec 24, 2014 11:20 pm
- Contact:
Re: Kill player
i tested game.player.character.die() and... i lost the game...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?
where use this code?
try with this
Code: Select all
if game.player.character then
game.player.character.die()
end
-
- Manual Inserter
- Posts: 4
- Joined: Wed Dec 24, 2014 11:20 pm
- Contact:
Re: Kill player
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...
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?
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
)
Re: Kill player
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:
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.
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}
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.