cmooref17 / ReservedItemSlotMods

https://thunderstore.io/c/lethal-company/p/FlipMods/ReservedItemSlotCore/
MIT License
1 stars 1 forks source link

[api Suggestion/Request] GetReservedSlot #10

Open CatsArmy opened 6 months ago

CatsArmy commented 6 months ago

A public function that takes the index of a reserved slot and returns that slot/item

Perhaps: public static GameObject GetReservedSlot(int index)

the point of this function is to let people who want to do stuff to the reserved slot (like fixing the AC energy bar not spawning for reserved slots) without the trouble of searching how to access just the reserved slots.

cmooref17 commented 6 months ago

The API is minimal right now, but all of those functions are in there. (maybe not public) I will be working on the API after I fix some of the immediate issues.

cmooref17 commented 6 months ago

By the way, if you want to access these functions now, you will want to run code similar to this.

First off, SessionManager keeps track of which item slots are unlocked by which players. If you have a mod that adds and item to a slot, but the host doesn't have the mod, then your item will not be in the slot. If you add a slot in your mod, and the host doesn't have that mod, that slot will not be in SessionManager either.

if (SessionManager.TryGetUnlockedItemSlotData("MyItemSlotName", out var itemSlotData))
{
    // This will run if your slot is unlocked
    var localPlayerController = StartOfRound.Instance.localPlayerController;
    int indexInInventory = itemSlotData.GetIndexInInventory(localPlayerController);
    var grabbableObject = localPlayerController.ItemSlots[indexInInventory];

    // If you need the item slot frame from the HUD for that slot
    int indexInReservedSlots = itemSlotData.GetReservedItemSlotIndex();
    var itemSlotFrame = HUDPatcher.reservedItemSlots[indexInReservedSlots];
}

Let me know if you'd like more assistance! I know the API isn't super straightforward, but the main goal was to allow adding slots and adding items to slots, so it's not super extensive right now.

CatsArmy commented 6 months ago

I am trying to pretty much do a for each reserveditem in reserveditemslots Add energy bar object to slot if slot has item and item uses battery

CatsArmy commented 6 months ago

So if the slot isn't unlocked then I don't really care as it won't have a item in it

CatsArmy commented 6 months ago

ok so wait

namespace ReservedItemSlotCore;
public static class SessionManager
{
public static Dictionary<string, ReservedItemData> allReservedItemData = new Dictionary<string, ReservedItemData>();
/// ...
}

is the:

Dictionary<string, ReservedItemData> allReservedItemData

the list of all reserved item slots?

CatsArmy commented 6 months ago

ok so if i am understanding correctly then:

namespace ReservedItemSlotCore;
public static class ReservedItemSlotsAPI_CatsTest
{
public static void Test()
{
//is the local player
var player = PlayerPatcher.localPlayerData;
//but the SessionManager which by its name is about the local player i would assume that it should handle the Session of the local player and if it does then it should probly have a variable for accessing that local player even if its the same one as the PlayerPatcher
var LocalSessionPlayer = SessionManager.localPlayer;
}
CatsArmy commented 6 months ago

nvm i think the ReservedHotbarManager is the class im looking for now

cmooref17 commented 6 months ago

Sorry for the slow responses. And it is a little confusing right now. I'm going to try and clean the api up a bit more, and hide methods and classes that devs won't need so it won't be so confusing.

You could do this to loop through them all.

foreach (var reservedItemSlot in SessionManager.unlockedReservedItemSlots)
{
    int indexInInventory = reservedItemSlot.GetIndexInInventory(StartOfRound.Instance.localPlayerController);
    var heldItem = playerData.itemSlots[indexInInventory];
    if (heldItem != null && heldItem.itemProperties.requiresBattery /* etc */)
    {
        // Do this
    }
}
cmooref17 commented 6 months ago

I will be adding helper functions soon to allow grabbing items directly out of a ReservedItemSlotData by passing in a player.

cmooref17 commented 6 months ago

I did update the core mod by the way, and added some more helpful API functions. I included the examples in the README if they are useful for you.