hey @bixarrio - Andrew responded, and this is what he told me about consuming resources from the inventory, in the way I want it to do so (which is right before an item is placed in the game world):
“Hi, sorry for the late response, and I hope you’re managing. To answer your question, you can scroll up in the conversation. I had sent you the methods for that.
The idea is to use: BuildingManager.Instance.OnPlacingBuildingPartEvent.AddListener((BuildingPart part) => { });
This event is triggered ‘after’ you have placed your building part. In this way, resources are not consumed when you switch to preview mode but only when you validate the placement.”
I myself am a bit confused on exactly how we can use that to consume inventory products, but I hope it helps in anyway (I am yet to investigate it)
You may want to have a look at ‘UICircularMenu.Start()’ and ‘UICircularBuildingMenuForRPGBuilder.Start()’
The main problem I have now is, how do we feed this event with data about our recipe so it consumes the right recipe resources?
Not sure if this will work or not, neither do I know how I’ll do it, but this is my plan in ‘BuildingManager.PlaceBuildingPart()’, to use the event Andrew mentioned:
Instance.OnPlacingBuildingPartEvent.AddListener((BuildingPart part) =>
{
// It's all happening in 'BuildingManager.cs', so I assume the 'buildingPart' scripts are right under it:
foreach (BuildingPart buildingPart in GetComponents<BuildingPart>())
{
// 1. Get the building part identifier
// 2. if they match, get the 'RequiredIngredientsBuildingCondition.cs' script attached to that object
// 3. Loop over the ingredients in 'BuildingRecipe.cs' attached to that script
// 4. Consume the resources in that loop
}
});
Edit 2: Here’s what I managed to create so far, but it’s not working for some reason…:
Instance.OnPlacingBuildingPartEvent.AddListener((BuildingPart part) =>
{
Debug.Log("listener for placing building part event");
var playerInventory = Inventory.GetPlayerInventory();
foreach (BuildingPart buildingPart in GetComponents<BuildingPart>())
{
// 1. Get the building part identifier
string identifier = buildingPart.GetGeneralSettings.Identifier;
Debug.Log($"identifier: {identifier}\n part Identifier: {part.GetGeneralSettings.Identifier}");
// 2. if they match, get the 'RequiredIngredientsBuildingCondition.cs' script attached to that object
if (identifier == part.GetGeneralSettings.Identifier)
{
RequiredIngredientsBuildingCondition buildingCondition = GetComponent<RequiredIngredientsBuildingCondition>();
// 3. Get the recipe for that building
BuildingRecipe recipe = buildingCondition.GetBuildingRecipe();
// 4. Loop over the ingredients in 'BuildingRecipe.cs', attached to that script
foreach (var craftingItem in recipe.GetIngredients())
{
// 5. Consume the resources in that loop
playerInventory.RemoveItem(craftingItem.Item, craftingItem.Amount);
}
}
}
});
What I’m trying to do above, is scan through the ‘BuildingManager.cs’ children, and find the ID of the parts inside that GameObject… (the ‘Building Manager’ script has a list of GameObjects attached to it), and compare it to the ID of what we currently have. If they match, get their building conditions, and from there we can get the building recipe. Using that recipe, we can consume the resources needed from the inventory
I know for a fact that the listener is indeed being called the moment the player constructs the part, because the first line of debugging works. However, after that, whatever is in the loop is not working for some reason… (6 hours later, I figure it’s because the GetComponent gets nothing, literally, so the loop doesn’t even start to begin with, and I have no idea why). Do you think you or @Brian_Trotter (sorry Brian for luring you into this again… ) can please have a look at it?
I placed this event-listening script in ‘BuildingManager.PlaceBuildingPart()’