GM:IsSpawnpointSuitable

From Garry's Mod
Jump to: navigation, search
 GM:IsSpawnpointSuitable( )

Contents

Description

Check if a player can spawn at a certain spawnpoint.

Arguments

Player ply

The player who is spawned

Arguments

Entity spawnpoint

The spawnpoint entity (on the map)

Arguments

boolean makeSuitable

If this is true, it'll kill any players blocking the spawnpoint

Returns

boolean

Return true to indicate that the spawnpoint is suitable (Allow for the player to spawn here), false to prevent spawning

Examples

Example

This will check if anyone is blocking the spawnpoint. If someone is, then it'll, depending on the bMakeSuitable value, kill the player, or return false.

function GM:IsSpawnpointSuitable( ply, spawnpointent, bMakeSuitable )

	local Pos = spawnpointent:GetPos()

	-- Note that we're searching the default hull size here for a player in the way of our spawning.
	-- This seems pretty rough, seeing as our player's hull could be different.. but it should do the job
	-- (HL2DM kills everything within a 128 unit radius)
	local Ents = ents.FindInBox( Pos + Vector( -16, -16, 0 ), Pos + Vector( 16, 16, 72 ) )

	if ( ply:Team() == TEAM_SPECTATOR or ply:Team() == TEAM_UNASSIGNED ) then return true end

	local Blockers = 0

	for k, v in pairs( Ents ) do
		if ( IsValid( v ) && v:GetClass() == "player" && v:Alive() ) then

			Blockers = Blockers + 1

			if ( bMakeSuitable ) then
				v:Kill()
			end

		end
	end

	if ( bMakeSuitable ) then return true end
	if ( Blockers > 0 ) then return false end
	return true

end

Output:

true or false
Personal tools
Navigation