Skip to content
1010101110 edited this page Mar 22, 2021 · 1 revision

Variants

Variants are styles for items. In this article we will analyze how they are used.

GUI

VariantDialog

this is the little gui window that pops up when a user hits the style button in the crafting menu

there is not much code behind it. it just gets the variants and icons displays them and allows user to select one.

//adds sprite to gui
function Setup(ItemDrop.Itemdata item){
    for (int i = 0; i < item.m_shared.m_variants; i++)
    {
        Sprite sprite = item.m_shared.m_icons[i];
    }
}

//sets selected index
public void OnClicked(int index)
{
    this.m_selected(index);
}

InvetoryGUI

this is where the crafting logic is

this section handles the variant dialog when user clicks the style button

//activates the style button on recipes with variants
public void UpdateRecipe(Player player, float dt)
{
    this.m_variantButton.gameObject.SetActive(this.m_selectedRecipe.Key.m_item.m_itemData.m_shared.m_variants > 1 && this.m_selectedRecipe.Value == null);
}

//opens dialog
public void OnShowVariantSelection()
{
    this.m_variantDialog.Setup(this.m_selectedRecipe.Key.m_item.m_itemData);
}

//sets selected variant
public void OnVariantSelected(int index)
{
    this.m_selectedVariant = index;
}

the update code sets the icon after selecting the style

public void UpdateRecipe(Player player, float dt)
{
    ItemDrop.ItemData value = this.m_selectedRecipe.Value;
    int num2 = (value != null) ? value.m_variant : this.m_selectedVariant;
    this.m_recipeIcon.sprite = this.m_selectedRecipe.Key.m_item.m_itemData.m_shared.m_icons[num2];
}

This is where the actual crafting happens

public void DoCrafting(Player player){
    //get selected variant
    int variant = this.m_craftVariant;
    
    //if its an upgrade get variant from the old item
    if (this.m_craftUpgradeItem != null)
    {
        variant = this.m_craftUpgradeItem.m_variant;
    }

    //do the actual crafting
    player.GetInventory().AddItem(this.m_craftRecipe.m_item.gameObject.name, this.m_craftRecipe.m_amount, num, variant, playerID, playerName)
}

Inventory

item is created and added to player inventory

public ItemDrop.ItemData AddItem(string name, int stack, int quality, int variant, long crafterID, string crafterName)
{
	GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(name);
    GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(itemPrefab);
    ItemDrop component2 = gameObject.GetComponent<ItemDrop>();
    component2.m_itemData.m_variant = variant;
    this.AddItem(component2.m_itemData);
}

Show equiped variant

Player calls VisEquipment to show the look of the character.

This is not just for equipment but for Skin, Beard, Hair as well.

Player.SetupVisEquipment
VisEquipment.UpdateEquipmentVisuals

not all the Sets handle variants, if adding new variants will have to mitigate this

attach is called by set methods. These have variant parameters.

AttachArmor
AttachBackItem
AttachItem


GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(itemHash);
GameObject gameObject2 = UnityEngine.Object.Instantiate<GameObject>(gameObject);
IEquipmentVisual componentInChildren = gameObject2.GetComponentInChildren<IEquipmentVisual>();

ItemStyle.Setup(style)
base.GetComponent<Renderer>().material.SetFloat("_Style", (float)style);
componentInChildren.Setup(variant);

Prefab

so we need to edit the prefab _Style component, m_variants, m_icons

now you need to follow the custom item creation tutorial to see how to edit the prefab.

Clone this wiki locally