Jump to content

[API Request] AddModCharacterIngredient


Recommended Posts

@Ipsquiggle, @PeterA, @ScottHansen, @V2C,

This is a request to have a very specific API added to the mod scene, one that used to be possible until very recently.

Per this thread:

I have determined that this is no longer possible with the previous method which I devised in this post:

Due to recent changes with A New Reign in the recipe.lua file; the simple method which I created previously no longer works. So I decided to create the code to have it reviewed and possibly added to the mod API, to support something the community would love to have. Please note that none of this code is tested, that is why I am wanting to have it reviewed and possibly be used as a quick start example to making this possible.

The following files will need to be modified in order to make this a reality.

  1. scripts/components/builder.lua
  2. scripts/components/builder_replica.lua
  3. scripts/modutil.lua
  4. scripts/recipe.lua
  5. scripts/widgets/recipepopup.lua

scripts/components/builder.lua:311

for k,v in pairs(recipe.mod_character_ingredients) do
    if MOD_CHARACTER_INGREDIENT[v.type] ~= nil then
        if MOD_CHARACTER_INGREDIENT[v.type].removeFn ~= nil then
           MOD_CHARACTER_INGREDIENT[v.type].removeFn(self.inst.components, v) 
        end
    end
end

scripts/components/builder.lua:334

function Builder:HasModCharacterIngredient(ingredient)
    if MOD_CHARACTER_INGREDIENTS[ingreident.type] ~= nil then
        if MOD_CHARACTER_INGREDIENTS[ingredient.type].hasFn ~= nil then
            return MOD_CHARACTER_INGREDIENTS[ingredient.type]:hasFn(self.inst.components, ingredient)
        end
    end
    return false, 0
end

scripts/components/builder.lua:518

for i, v in ipairs(recipe.mod_character_ingredients) do
    if not self:HasModCharacterIngredient(v) then
        return false
    end
end

scripts/components/builder_replica.lua:185

function Builder:HasModCharacterIngredient(ingredient)
    if MOD_CHARACTER_INGREDIENTS[ingreident.type] ~= nil then
        if MOD_CHARACTER_INGREDIENTS[ingredient.type].hasFn ~= nil then
            return MOD_CHARACTER_INGREDIENTS[ingredient.type]:hasFn(self.inst.replica, ingredient)
        end
    end
    return false, 0
end

scripts/components/builder_replica.lua:270

for i, v in ipairs(recipe.mod_character_ingredients) do
    if not self:HasModCharacterIngredient(v) then
        return false
    end
end

scripts/recipe.lua:48

local MOD_CHARACTER_INGREDIENTS = { }

local is_mod_character_ingredient = nil
function IsModCharacterIngredient(ingredienttype)
    if is_mod_character_ingredient == nil then
        is_mod_character_ingredient = {}
    end
    return ingredienttype ~= nil and is_mod_character_ingredient[ingredienttype] == true
end


function AddModCharacterIngredient(ingredienttype, hasFn, removeFn)
    -- should we worry about duplicate mod ingreident types?
    MOD_CHARACTER_INGREDIENTS[ingredienttype] = { }
    MOD_CHARACTER_INGREDIENTS[ingreidnettype].hasFn = hasFn
    MOD_CHARACTER_INGREDIENTS[ingreidnettype].removeFn = removeFn
end

scripts/recipe.lua:68

self.ingredients   = {}
self.mod_character_ingredients = {}
self.character_ingredients = {}
self.tech_ingredients = {}

for k,v in pairs(ingredients) do
    table.insert(
        (IsModCharacterIngredient(v.type) and self.mod_character_ingredients) or
        (IsCharacterIngredient(v.type) and self.character_ingredients) or
        (IsTechIngredient(v.type) and self.tech_ingredients) or
        self.ingredients,
        v
    )
end

scripts/widgets/recipepopup.lua:329

local num =
    (recipe.ingredients ~= nil and #recipe.ingredients or 0) +
    (recipe.mod_character_ingredients ~= nil and #recipe.mod_character_ingredients) +
    (recipe.character_ingredients ~= nil and #recipe.character_ingredients or 0) +
    (recipe.tech_ingredients ~= nil and #recipe.tech_ingredients or 0)

scripts/widgets/recipepopup.lua:369

for i, v in ipairs(recipe.mod_character_ingredients) do
    --#BDOIG - does this need to listen for deltas and change while menu is open?
    --V2C: yes, but the entire craft tabs does. (will be added there)
    local has, amount = builder:HasModCharacterIngredient(v)
    local ing = self.contents:AddChild(IngredientUI(v.atlas, v.type..".tex", v.amount, amount, has, STRINGS.NAMES[string.upper(v.type)], owner, v.type))
    if num > 1 and #self.ing > 0 then
        offset = offset + half_div
    end
    ing:SetPosition(Vector3(offset, self.skins_spinner ~= nil and 110 or 80, 0))
    offset = offset + w + half_div
    table.insert(self.ing, ing)
end

scripts/modutil.lua:479

env.AddModCharacterIngredient = AddModCharacterIngredient

After these changes have been made the optimal usage is as followed:

--AddModCharacterIngredient(ingredientName, checkFn, removeFn)

local _ingredientName = "magic"
AddModCharacterIngredient(_ingredientName,
function(inst, ingredient)
	if inst.magic ~= nil then
		return inst.magic:GetCurrent() >= ingredient.amount, inst.magic:GetCurrent()
	end
end,
function(inst, ingredient)
	if inst.magic ~= nil then
		inst.magic:DoDelta(-ingredient.amount)
	end
end)

If following this particular code example, the only thing the end modder should be required to do is determine how to handle their newly modded character ingredients availability check (first function in call) and removal of the ingredients (second function in call).

Alternatively as a stop gap measure you could add the following code to the scripts/recipe.lua and we could continue to use the previous method, by simply calling the function below.

function AddModCharacterIngredient(ingredienttype)
    if is_character_ingredient == nil then
        is_character_ingredient = {}
        for k, v in pairs(CHARACTER_INGREDIENT) do
            is_character_ingredient[v] = true
        end
    end
    is_character_ingredient[ingredienttype] = true
end

I am bringing this request to light because I know there are at least 1 mod on the workshop currently that has been broken because of this particular issue.

http://steamcommunity.com/sharedfiles/filedetails/?id=644104565&searchtext=chakra

Regards,

Kzisor/Ysovuka

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...