TestMobjLocation

From ZDoom Wiki
Jump to navigation Jump to search
Note: This feature is for ZScript only.


Actor

bool TestMobjLocation()

Usage

Checks if the Actor is clipping into actors or geometry at its current position. This can be used to check if a newly spawned actor can actually fit in the place where it was spawned.

Note, this function returns false only when the actor's position is overlapping geometry or other actors. For checking if an actor can move into a specified position, see CheckMove.

Error.gif
Warning: This function only checks if the actor's collision box overlaps another actor or level geometry. As a result, if the actor spawns fully in the void (beyond level boundaries) without actually colliding with another actor or geometry, this function will return true. To fully check if a specific actor position is "valid" (i.e. the actor is not colliding with others and is within the map), you also need to use level.IsPointInLevel.


Return value

  • bool - returns true if the calling actor has enough space. If the actor is clipping into other actors or geometry, returns false.

Examples

This function will spawn a specified monster in front of the calling actor (at the specified distance). If there's no space for the spawned monster, it'll be destroyed and the function will shift the spawn point around the calling actor until it finds a suitable position:

void SpawnFriendAround(class<Actor> cls, double distance = 128)
{
	// If the spawn position is not valid, we'll rotate
	// it by 20 degrees until we can't find a suitable one, 
	// or we finish a full circle:
	for (double ang = 0; ang < 360; ang += 20)
	{
		// Get an offset and rotate it to match the calling actor's
		// facing direction:
		Vector2 dir = AngleToVector(self.angle + ang, distance); 
		// Add it to the actor's current position:
		Vector3 ppos = Vec3Offset(dir.x, dir.y, 0);
		// Check if the position is in the void:
		if (level.IsPointInLevel(ppos) == false)
		{
			continue; //in the void - go to next attempt
		}
		let mo = Spawn(cls, ppos);
		if (mo.TestMobjLocation() == true)
		{
			break; //success - stop here
		}
		else
		{
			mo.Destroy(); //didn't fit - destroy the actor
		}
	}
}

See also AngleToVector, Vec3Offset, IsPointInLevel.

See also