Skip to content

Added networked variants of the entity listeners. #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Networked entity listeners now pass an Entity instance to the callbac…
…ks rather than an index.
  • Loading branch information
jordanbriere committed Sep 23, 2020
commit f69d35c06178af12c0adb7f115d7049f7071ab57
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ Called when a networked entity has been created.
from listeners import OnNetworkedEntityCreated

@OnNetworkedEntityCreated
def on_networked_entity_created(index):
def on_networked_entity_created(entity):
pass


Expand Down Expand Up @@ -240,7 +240,7 @@ Called when a networked entity gets deleted.
from listeners import OnNetworkedEntityDeleted

@OnNetworkedEntityDeleted
def on_networked_entity_deleted(index):
def on_networked_entity_deleted(entity):
pass


Expand Down Expand Up @@ -290,7 +290,7 @@ Called before a networked entity has been spawned.
from listeners import OnNetworkedEntityPreSpawned

@OnNetworkedEntityPreSpawned
def on_networked_entity_pre_spawned(index):
def on_networked_entity_pre_spawned(entity):
pass

.. note:: This listener gets only called in Black Mesa: Source.
Expand Down Expand Up @@ -320,7 +320,7 @@ Called when a networked entity has been spawned.
from listeners import OnNetworkedEntitySpawned

@OnNetworkedEntitySpawned
def on_networked_entity_spawned(index):
def on_networked_entity_spawned(entity):
pass


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,18 @@ def from_inthandle(self, inthandle):
"""
return self[index_from_inthandle(inthandle)]

def on_automatically_removed(self, index):
def on_automatically_removed(self, entity):
"""Called when an index is automatically removed."""

def _on_networked_entity_deleted(self, index):
"""Internal networked entity deletion callback.

:param int index:
The index of the networked entity being removed.
:param Entity entity:
The networked entity being removed.
"""
# Get the index of the entity
index = entity.index

# No need to go further if there is no object associated to this index
if index not in self:
return
Expand Down
4 changes: 2 additions & 2 deletions addons/source-python/packages/source-python/entities/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,6 @@ def initialize(self, index):
# >> LISTENERS
# =============================================================================
@OnNetworkedEntityCreated
def on_networked_entity_created(index):
def on_networked_entity_created(entity):
"""Called when a new networked entity has been created."""
_waiting_entity_hooks.initialize(index)
_waiting_entity_hooks.initialize(entity.index)
15 changes: 9 additions & 6 deletions src/core/sp_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,8 @@ void CSourcePython::OnEntityPreSpawned( CBaseEntity *pEntity )
if (!IndexFromBaseEntity(pEntity, uiIndex))
return;

CALL_LISTENERS(OnEntityPreSpawned, uiIndex);
static object Entity = import("entities").attr("entity").attr("Entity");
CALL_LISTENERS(OnEntityPreSpawned, Entity(uiIndex));
}
#endif

Expand All @@ -630,7 +631,8 @@ void CSourcePython::OnEntityCreated( CBaseEntity *pEntity )
if (!IndexFromBaseEntity(pEntity, uiIndex))
return;

CALL_LISTENERS(OnNetworkedEntityCreated, uiIndex);
static object Entity = import("entities").attr("entity").attr("Entity");
CALL_LISTENERS(OnNetworkedEntityCreated, Entity(uiIndex));
}

void CSourcePython::OnEntitySpawned( CBaseEntity *pEntity )
Expand All @@ -641,19 +643,20 @@ void CSourcePython::OnEntitySpawned( CBaseEntity *pEntity )
if (!IndexFromBaseEntity(pEntity, uiIndex))
return;

CALL_LISTENERS(OnNetworkedEntitySpawned, uiIndex);
static object Entity = import("entities").attr("entity").attr("Entity");
CALL_LISTENERS(OnNetworkedEntitySpawned, Entity(uiIndex));
}

void CSourcePython::OnEntityDeleted( CBaseEntity *pEntity )
{
object oEntity(ptr((CBaseEntityWrapper*) pEntity));
CALL_LISTENERS(OnEntityDeleted, oEntity);
CALL_LISTENERS(OnEntityDeleted, ptr((CBaseEntityWrapper*) pEntity));

unsigned int uiIndex;
if (!IndexFromBaseEntity(pEntity, uiIndex))
return;

CALL_LISTENERS(OnNetworkedEntityDeleted, uiIndex);
static object Entity = import("entities").attr("entity").attr("Entity");
CALL_LISTENERS(OnNetworkedEntityDeleted, Entity(uiIndex));

// Invalidate the internal entity cache once all callbacks have been called.
static object _on_networked_entity_deleted = import("entities").attr("_base").attr("_on_networked_entity_deleted");
Expand Down