Skip to content

PlayerUtil

Kanisuko edited this page May 24, 2026 · 7 revisions

PlayerUtil

Namespace: ScavLib.util

Safe, null-protected wrappers around the player's inventory and basic state. All methods return safe default values (0, false, null) when no world is loaded — they will never throw a NullReferenceException.


Inventory Shortcuts

Method Returns Description
GiveItem(string id) GameObject Spawn item at player feet and auto-pickup. Returns the spawned GameObject, or null on failure.
TakeItem(string id) bool Drop the first inventory item matching the given ID. Returns true if found and dropped.
HasItem(string id) bool Check if the player has at least one item with the given ID in their surface inventory (slots + wearables).
GetAllItems() List<Item> Get all items currently in the player's slot inventory (not wearables). Returns an empty list if not in game.

State Queries

Method Returns Condition
IsAlive() bool brainHealth > 0
IsConscious() bool Alive AND consciousness > 30
IsDying() bool Any critical vital threshold breached

Vital Reads

Method Returns Notes
GetHunger() float Current hunger value. Returns 0 if not in game.
GetThirst() float Current thirst value. Returns 0 if not in game.

Usage Example

using ScavLib.util;
using ScavLib.event_bus;
using ScavLib.event_bus.events;

public class MyMod : BaseUnityPlugin
{
    private void Awake()
    {
        EventBus.Register(this);
    }

    [Subscribe]
    private void OnWorldLoaded(WorldLoadedEvent e)
    {
        if (PlayerUtil.IsAlive())
        {
            GameUtil.Log($"Hunger: {PlayerUtil.GetHunger()}");
            GameUtil.Log($"Thirst: {PlayerUtil.GetThirst()}");
        }

        PlayerUtil.GiveItem("bandage");

        if (PlayerUtil.HasItem("rifle"))
            GameUtil.Log("Player has a rifle!");
    }
}

Clone this wiki locally