Instant Crafting

Dahrona

Member
how do i craft by script? i try to call CraftSelectedQuantity() from Crafting menu. but it gives an error.
 

Sangemdoko

Moderator
Staff member
If you are going to craft by script I would recommend calling the Crafting processor directly it gives a lot more control.
You can read through the CraftingMenu code to see how it is done.
Also if you are interested check out how to make custom crafting processors if you want to expand the crafting system
 

Dahrona

Member
what if I want to run crafting with a button other than the submit button. so with the custum button I can do crafting too. if I run crafting from the crafting menu, it will run the quantity panel via the submit button
 
Last edited:

Sangemdoko

Moderator
Staff member
That's why I said you should do it directly on the CraftingProcessor. You can get the CraftingProcessor from the Crafter (which is public in the CraftingMenu)

Code:
// Check if you can craft
var canCraft = m_Crafter.Processor.CanCraft(m_SelectedRecipe, m_Inventory, amount);

//Craft
var result = m_Crafter.Processor.Craft(m_SelectedRecipe, m_Inventory, quantity);


To make it easier in the future I made this change to the CraftingMenuBase script
Code:
        /// <summary>
        /// Wait for the player to select a quantity.
        /// </summary>
        /// <returns>The task.</returns>
        public virtual void CraftSelectedQuantity()
        {
            var quantity = m_RecipePanel.Quantity;

            DoCraft(quantity);
        }

        /// <summary>
        /// Do craft the item.
        /// </summary>
        /// <param name="quantity">The quantity to craft.</param>
        public virtual void DoCraft(int quantity)
        {
            if (quantity >= 1) {
                var result = m_Crafter.Processor.Craft(m_SelectedRecipe, m_Inventory, quantity);
                OnCraftComplete(result, m_SelectedRecipe, m_Inventory, quantity);
            }

            DrawRecipes();
            m_RecipePanel.SetQuantity(1);
            m_RecipePanel.Refresh();
        }
 

Dahrona

Member
what should i put in m_selectedRecipe? i put SelectedRecipe from crafting menu but it give an null. where can i get the reference of the currently selected recept in the grid?
 

Sangemdoko

Moderator
Staff member
The m_SelectedRecipe is the CraftingRecipe object you selected in the grid.
If it is null, then the recipe hasn't been selected or the CraftingMenu hasn't been setup properly.
You can find how the CraftingMenu gets a reference to the selected recipe by checking the CraftingMenuBase script, it is fully commented
 

Dahrona

Member
how to change processor in script? i look in documentation we can use :
// Change the Crafting Processor at anytime
m_Crafter.Processor = newProcessor;

but what form is the newProcessor in? I tried to write a script like this.

CustomCraftingProcessor newProcessor;
SimpleCraftingProcessor oldProcessor;

m_Crafter.Processor = newProcessor;

but it gives error
 

Sangemdoko

Moderator
Staff member
WHen you get an error, always paste the fulle error message please, otherwise I cannot know what is wrong.
did you inherit CraftingProcessor for your CustomCraftingProcessor script?
 

Dahrona

Member
okay i get the solution, with add new crafter instead change the processor.

can I get information about the grid that selected in script, like the transform?
 

Sangemdoko

Moderator
Staff member
You should have access to that if you make a custom CraftingMenu.
The code is fully commented so you should be able to find what is selected and how to get a reference to it
 

Dahrona

Member
what if i want to add sfx when recipe can't be crafted. if I am using singleton class that save audio sfx. i can't call it in crafting menu script
 

Sangemdoko

Moderator
Staff member
Are you using a custom Crafting Menu or are you modifiying the existing code?
If you you are making a custom one you can override
Code:
 public virtual void DoCraft(int quantity)
Then you should be able to cal your code to audio if there isn't enough ingredients.

if you are modifying the existing CraftingMenu and don't have access to your code, that's probably because of the Assembly definitions
You'll need to add your own assembly in the UIS assembly dependencies to allow your code to appear in our code.
1669021135207.png
(This is NOT recommended, it is highly recomemended you make a custom script by inheriting CraftingMenu within your own assembly)

If you are not familiar with assembly definitions check this video
 

Dahrona

Member
got it.

how to made cursor can't moving when I want to show pop up in front of grid?? it is like quantity picker but just confirmation button. I'm curious how the quantity picker panel works which makes the cursor button behind it not work.
 

Sangemdoko

Moderator
Staff member
I'm not sure if I understand correctly.
But if you mean preventing the player from pressing button in the background when there it a popup. An easy solution is to add a transparent Image to your popup that fills up the entire screen. this should prevent clicks from going through.
 

Dahrona

Member
i did it,
but i am using gamepad for navigation cursor, and when pop up showed I can move the cursor still. different with quantity picker panel.
 

Sangemdoko

Moderator
Staff member
You'll need to add a canvas group on the Grid
Then when the Popup opens you can use the Unity event for OnOpen to set the Grid canvas interactable to false,
Then set the OnClose event to set the Grid canvas to interactable true and it should allow you to prevent selecting things in the background.

Another approach is to set the Selectable Navigation manually and making sure the popup DisplayPanel component has a Selectable set in the Selectable on open field
 
Top