Collision detection
Posted: Sun Oct 09, 2016 1:28 pm
How can I check if an entity, such as flashbang collide with a player?
Code: Select all
public OnEntityCreated(entity, const String:classname[])
{
// Checks if the entity is a flashbang
if (StrContains(classname, "flashbang_projectile") != -1)
{
SDKHook(entity, SDKHook_StartTouch, StartTouchFlash);
}
}
Syntax: Select all
from entities.entity import Entity
from entities.hooks import EntityCondition, EntityPreHook
from memory import make_object
from messages import SayText2
from players.entity import Player
TARGET_CLASSNAME = 'flashbang_projectile'
@EntityPreHook(
EntityCondition.equals_entity_classname(TARGET_CLASSNAME),
"start_touch")
def pre_start_touch(args):
entity = make_object(Entity, args[0])
# Hook was likely set on CBaseEntity::StartTouch, so we need to filter out other entities
if entity.classname != TARGET_CLASSNAME:
return
# Firstly create an entity out of args[1] and check if it's a player
other = make_object(Entity, args[1])
if not other.is_player():
return
# Secondly, create a player out of args[1]
player = make_object(Player, args[1])
# Do whatever you want
SayText2(
"Flashbang projectile (index={index}) touched player (userid="
"{userid})".format(index=entity.index, userid=player.userid)
).send()
iPlayer wrote:Here's the hook registration & hook body itself.Syntax: Select all
from entities.entity import Entity
from entities.hooks import EntityCondition, EntityPreHook
from memory import make_object
from messages import SayText2
from players.entity import Player
TARGET_CLASSNAME = 'flashbang_projectile'
@EntityPreHook(
EntityCondition.equals_entity_classname(TARGET_CLASSNAME),
"start_touch")
def pre_start_touch(args):
entity = make_object(Entity, args[0])
# Hook was likely set on CBaseEntity::StartTouch, so we need to filter out other entities
if entity.classname != TARGET_CLASSNAME:
return
# Firstly create an entity out of args[1] and check if it's a player
other = make_object(Entity, args[1])
if not other.is_player():
return
# Secondly, create a player out of args[1]
player = make_object(Player, args[1])
# Do whatever you want
SayText2(
"Flashbang projectile (index={index}) touched player (userid="
"{userid})".format(index=entity.index, userid=player.userid)
).send()
Syntax: Select all
...
player_prop = Player(player.index)
player_prop.teleport(velocity=Vector(1000,0,1000))
Syntax: Select all
from entities.entity import Entity
from entities.hooks import EntityCondition, EntityPostHook
from memory import make_object
from messages import SayText2
from players.entity import Player
TARGET_CLASSNAME = 'flashbang_projectile'
@EntityPostHook(
EntityCondition.equals_entity_classname(TARGET_CLASSNAME),
"start_touch")
def post_start_touch(args, ret_val):
entity = make_object(Entity, args[0])
# Hook was likely set on CBaseEntity::StartTouch, so we need to filter out other entities
if entity.classname != TARGET_CLASSNAME:
return
# Firstly create an entity out of args[1] and check if it's a player
other = make_object(Entity, args[1])
if not other.is_player():
return
# Secondly, create a player out of args[1]
player = make_object(Player, args[1])
# Do whatever you want
SayText2(
"Flashbang projectile (index={index}) touched player (userid="
"{userid})".format(index=entity.index, userid=player.userid)
).send()
Syntax: Select all
if entity.classname != TARGET_CLASSNAME:
return
Syntax: Select all
@EntityPostHook(EntityCondition.equals_entity_classname(TARGET_CLASSNAME), "start_touch")