Getting player instance in set_name hook.

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
Doldol
Senior Member
Posts: 201
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

Getting player instance in set_name hook.

Postby Doldol » Sun Dec 27, 2015 9:05 pm

Is there a better/more efficient way of doing this?
Specifically, can I go from a CBaseClient pointer to a player index/pointer/instance with a SP conversion function?

Syntax: Select all

from memory import make_object, get_object_pointer
from memory.hooks import PreHook
from entities.helpers import index_from_edict
from filters.players import PlayerIter
from players import BaseClient, PlayerGenerator
from players.entity import Player
from engines.server import server, engine_server


def preSetName(args):
base_client_pointer = args[0]
for edict in PlayerGenerator():
if base_client_pointer == (get_object_pointer(server.get_client(index_from_edict(edict) - 1)) - 4):
player = Player(index_from_edict(edict))


def load_when_human(args=False):
global pre_hook_set_name
for player in PlayerIter():
base_client = make_object(BaseClient, player.base_client)
pre_hook_set_name = PreHook(base_client.set_name)
pre_hook_set_name(preSetName)
if args:
event_manager.unregister_for_event("player_activate", load_when_human)
break
else:
raise ValueError('No human player found!')


try:
load_when_human()
except ValueError:
from events.manager import event_manager
event_manager.register_for_event("player_activate", load_when_human)
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Mon Dec 28, 2015 12:20 pm

This has probably the same performance, but is much easier to read.

Syntax: Select all

from players.entity import Player
from filters.players import PlayerIter

def base_client_to_player(base_client):
for player in PlayerIter():
if player.base_client == base_client:
return player

raise ValueError('Conversion failed.')
However, the class also stores the entity index at offset 16, so that's probably what you want.

Syntax: Select all

entity_index = base_client.get_int(16)

Btw. this line is a bit unecessarry, because player.base_client already returns a BaseClient object.

Syntax: Select all

base_client = make_object(BaseClient, player.base_client)
User avatar
Doldol
Senior Member
Posts: 201
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

Postby Doldol » Mon Dec 28, 2015 10:37 pm

Thanks! Especially for the offset tip, I'll be using that!

Return to “Plugin Development Support”

Who is online

Users browsing this forum: Bing [Bot] and 17 guests