Skip to content

Commit

Permalink
feat: add GetPlayerFrom utility methods
Browse files Browse the repository at this point in the history
  • Loading branch information
roflmuffin committed Nov 2, 2023
1 parent 7655c27 commit c54eda1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
9 changes: 4 additions & 5 deletions docs/src/content/docs/reference/referencing-players.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ Userids are similar to a slot, and they are what show in the console when you ty
All entity instances have an entity index (similar to CSGO), which means both the player controller and the player pawn both have different indexes. The Player Controller has a reserved entity index (because of the slot system 0-MAXPLAYERS(64)), but a player pawn does not, so it is common to retrieve a player pawn with an index in the hundreds.

### Entity Pointers & Handles
All "entity objects" you interact with in CounterStrikeSharp are actually wrappers around a __pointer__ on the server, which can be accessed by retrieving the `.Handle` property. Which means to go from a CPlayerSlot, UserID or Index value, you must first convert to an index, and then supply this to a native method which can convert the index to an entity pointer. At time of writing this is `NativeAPI.GetEntityFromIndex()` but will likely change in the future. Examples:
All "entity objects" you interact with in CounterStrikeSharp are actually wrappers around a __pointer__ on the server, which can be accessed by retrieving the `.Handle` property. Which means to go from a CPlayerSlot, UserID or Index value, you must use the matching utility method to fetch the entity pointer. There are three utility methods to get an instance of a player using these identifiers:

```csharp
var entity = new CCSPlayerController(NativeAPI.GetEntityFromIndex(slot + 1)); // Slot -> Index -> Pointer
var entity = new CCSPlayerController(NativeAPI.GetEntityFromIndex(index)); // Index -> Pointer
var entity = new CCSPlayerController(NativeAPI.GetEntityFromIndex((userid & 0xFF) + 1)); // Userid -> Index -> Pointer
var entity = new CCSPlayerController(NativeAPI.GetEntityFromIndex(pointer); // IntPtr directly
var player = Utilities.GetPlayerFromUserid(userid);
var player = Utilities.GetPlayerFromIndex(index);
var player = Utilities.GetPlayerFromSlot(slot);
```

:::note[Entity Safety]
Expand Down
22 changes: 21 additions & 1 deletion managed/CounterStrikeSharp.API/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ public static IEnumerable<T> FlagsToList<T>(this T flags) where T : Enum
.Where(x => flags.HasFlag(x)).AsEnumerable();
}

public static T GetEntityFromIndex<T>(int index) where T : CEntityInstance
{
return (T)Activator.CreateInstance(typeof(T), NativeAPI.GetEntityFromIndex(index))!;
}

public static CCSPlayerController GetPlayerFromIndex(int index)
{
return Utilities.GetEntityFromIndex<CCSPlayerController>(index);
}

public static CCSPlayerController GetPlayerFromSlot(int slot)
{
return Utilities.GetEntityFromIndex<CCSPlayerController>(slot + 1);
}

public static CCSPlayerController GetPlayerFromUserid(int userid)
{
return Utilities.GetEntityFromIndex<CCSPlayerController>((userid & 0xFF) + 1);
}

public static IEnumerable<T> FindAllEntitiesByDesignerName<T>(string designerName) where T : CEntityInstance
{
var pEntity = new CEntityIdentity(NativeAPI.GetFirstActiveEntity());
Expand Down Expand Up @@ -84,4 +104,4 @@ public static unsafe string ReadStringUtf8(IntPtr ptr)
}
}
}
}
}

0 comments on commit c54eda1

Please sign in to comment.