On Prop +use from Player

Please post any questions about developing your plugin here. Please use the search function before posting!
Predz
Senior Member
Posts: 158
Joined: Wed Aug 08, 2012 9:05 pm
Location: Bristol, United Kingdom

On Prop +use from Player

Postby Predz » Tue May 24, 2016 9:23 am

I am trying to hook onto the "use" function available when a player uses "+use" on a prop of any kind. The hook works perfectly however the arguments returned are of the Entity itself, and another that I cannot obtain info on. The returned arguments are (Pointer, Pointer), the first being the Entity, the second I hoped would be the Player. On my tests the server just crashes upon trying to obtain a index from said pointer. Code is beneath:

Syntax: Select all

from engines.precache import Model
from messages import SayText2
from players.entity import Player
from players.helpers import index_from_userid
from entities.entity import Entity
from entities.hooks import EntityPreHook
from entities.hooks import EntityCondition
from events import Event
from memory import make_object

model = Model('models/props/cs_militia/toilet.mdl')

global index

@Event('player_say')
def _on_say(event):
player = Player(index_from_userid(event['userid']))
origin = player.view_coordinates

entity = Entity.create('prop_physics_override')
entity.origin = origin
entity.model = model
entity.spawn_flags = 265
entity.spawn()
SayText2(str(entity.index)).send()

global index
index = entity.index


@EntityPreHook(EntityCondition.equals_entity_classname('prop_physics_override'), 'use')
def _on_start_touch(stack):
entity = make_object(Entity, stack[0])
# Retrieving index from stack[1] causes the crash.
# I know I am trying to create a Player object, but just retrieving the index_from_pointer causes it too.
player = make_object(Player, stack[1])
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Re: On Prop +use from Player

Postby BackRaw » Tue May 24, 2016 9:36 am

Maybe try making an Entity or BaseEntity object first out of stack[1]. That's what I do when I don't have any clue about which info I would get. If the server doesn't crash, print stack[1]'s classname to figure out exactly what is happening. Maybe the stack[1] is player only at some point, and at some other point it's worldspawn or something. :D
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: On Prop +use from Player

Postby Ayuto » Tue May 24, 2016 9:38 am

I think it's an InputFunction, so the second argument would be InputData. But you could also simply use the OnEntityOuutput listener.

Which SP version are you running btw.?

@Backraw:
Index from pointer conversions work for simple entities and players.
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Re: On Prop +use from Player

Postby BackRaw » Tue May 24, 2016 10:34 am

Ayuto wrote:@Backraw:
Index from pointer conversions work for simple entities and players.

Yeah it was just a guess. :)
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: On Prop +use from Player

Postby iPlayer » Tue May 24, 2016 11:09 am

Ayuto wrote:But you could also simply use the OnEntityOuutput listener.


Yes, but it would only work if there are actual output connections on that entity. What if there're none?
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
Predz
Senior Member
Posts: 158
Joined: Wed Aug 08, 2012 9:05 pm
Location: Bristol, United Kingdom

Re: On Prop +use from Player

Postby Predz » Tue May 24, 2016 12:22 pm

I am trying to detect a player pressing +use on a prop which I can then parent to the player, looking like they are carrying it. So I could just check for the closest player. But again that is just working on a "derped" method rather than actually trying to fix my problem. :P

I did look to see if any in game offsets would be available for said function, but have no luck so far.

SP Version: 323
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: On Prop +use from Player

Postby satoon101 » Tue May 24, 2016 12:50 pm

Well, as Ayuto stated, the 2nd argument is InputData. So, you could use make_object to get the object and the use the (I believe) activator attribute to find out which entity caused it.
Image
Predz
Senior Member
Posts: 158
Joined: Wed Aug 08, 2012 9:05 pm
Location: Bristol, United Kingdom

Re: On Prop +use from Player

Postby Predz » Tue May 24, 2016 1:46 pm

Sorry took me a while to catch on...

Working code below for reference:

Syntax: Select all

from engines.precache import Model
from messages import SayText2
from players.entity import Player
from players.helpers import index_from_userid
from entities.entity import Entity
from entities.datamaps import InputData
from entities.hooks import EntityPreHook
from entities.hooks import EntityCondition
from events import Event
from memory import make_object

model = Model('models/props/cs_militia/toilet.mdl')

global index

@Event('player_say')
def _on_say(event):
player = Player(index_from_userid(event['userid']))
origin = player.view_coordinates

entity = Entity.create('prop_physics_override')
entity.origin = origin
entity.model = model
entity.spawn_flags = 265
entity.spawn()
SayText2(str(entity.index)).send()

global index
index = entity.index


@EntityPreHook(EntityCondition.equals_entity_classname('prop_physics_override'), 'use')
def _on_start_touch(stack):
entity = make_object(Entity, stack[0])
input = make_object(InputData, stack[1])

player = Player(input.activator)
SayText2(player.name).send()

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 52 guests