Controlling Alive() boolean

I want to be able to respawn players the moment they die without the ply:Alive() boolean returning true (for that particular player) after they have respawned.

The particular addon this is being used in is for a team and round based gamemode. Once the round has ended and everyone has been respawned Alive() should return true for all.

I’ve been searching for a while and haven’t been able to find a solution myself. I’m hoping someone might be able to guide me in the right direction.

Thanks in advance.

[lua]
function _R.Player.SetAlive(self,bool)

self.__alive = bool

end

function _R.Player.Alive(self)

return self.__alive

end
[/lua]

Hi ralle105

Thanks for your answer. From what I understand of the code, you are redefining the Alive() function and allowing it to be set by a SetAlive() function.

I have two questions concerning this however:

  1. If the Alive() function is now only determined by the _alive bool, does this mean i have to set Alive() whenever the gamemode would have done it naturally? i.e. round end/natural respawn.

  2. To use the SetAlive function, would I type in ply.SetAlive(true) or SetAlive(ply,true). I am not quite sure what _R.Player does in the code.

Thanks once again.

  1. Yes

  2. ply:SetAlive( true ) — _R.Player is the index table for players. (getmetatable( ply ).__index == _R.Player, read about metatables in PiL)

Thanks esalaka for your reply,

If I created a hook for the Spawn function, I could set the player as alive every time, and then manually set alive to false when needed correct?

Since the Spawn function needs an entity before it I had no idea how to create this hook. I tried

hook.Add(“Spawn”,“KeepSame”,SetAlive(true)) [which I’m guessing is wrong]

I also tried creating a function in the hook that SetAlive for a player to true, but considering I didn’t at any point ‘get’ the entity before Spawn, it’s inevitably incorrect.

How would I do this correctly?

First off the hook name is wrong, if you read it from the wiki it would explain to you how to use it.

**[Gamemode.PlayerSpawn

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.PlayerSpawn)**

And once again SetAlive has been set to the player metatable meaning you call it using ply:SetAlive() ply being the player object.

iRzilla, how do I define ply as the player that has just spawned?

**[Gamemode.PlayerSpawn

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.PlayerSpawn)**

Did you not read that?

Lol, yes I did. I realise the player is an argument of that particular function. However in the hook, there are no arguments. Forgive me if i’m a bit slow in picking up the obvious, but it isn’t too obvious to me :S

[lua]
hook_name = “PlayerSpawn”
unique_name = “AnythingYouWant”
hook_function = function(ply_obj) ply_obj:SetAlive(true) end

hook.Add(hook_name, unique_name, hook_function)
[/lua]

Thanks iRzilla :slight_smile:

I think my misunderstanding lies in the ply variable. My understanding was it had to be defined before a function uses it.

From the code you wrote I am guessing ply_obj can be replaced with ply?

Thanks again for your help in this :slight_smile:

Of course, a variable can be called anything you want. Just try not to override anything.