Jump to content

Return ingredients upon crafting issue.


bigpak

Recommended Posts

Alright so I am using an altered recipes.lua file, here is that first of all:

require "class"require "util" Ingredient = Class(function(self, type, amount, atlas)    self.type = type    self.amount = amount        self.atlas = (atlas and resolvefilepath(atlas))                                        or resolvefilepath("images/inventoryimages.xml")end) local num = 0Recipes = {} Recipe = Class(function(self, name, ingredients, tab, level, placer, min_spacing, nounlock, numtogive, oncraftfn)    self.name          = name    self.placer        = placer    self.ingredients   = ingredients    self.product       = name    self.tab           = tab     self.atlas         = resolvefilepath("images/inventoryimages.xml")     self.image         = name .. ".tex"    self.sortkey       = num    self.level         = level or {}    self.level.ANCIENT = self.level.ANCIENT or 0    self.level.MAGIC   = self.level.MAGIC or 0    self.level.SCIENCE = self.level.SCIENCE or 0    self.placer        = placer    self.min_spacing   = min_spacing or 3.2     self.nounlock      = nounlock or false     self.numtogive     = numtogive or 1     self.oncraftfn     = oncraftfn or function() print("Something is wrong.") end     num                = num + 1    Recipes[name]      = selfend) function Recipe:GetLevel()    return self.levelendfunction GetAllRecipes()        return Recipesend function GetRecipe(name)    return Recipes[name]end

So there is that, now whenever I try to build the item with the return ingredients back code, it will not prototype it, however it does return the ingredients back and the crafted product.

 

Here is the code I am using for that:

local function OnBuildFn(component)        print("Overwriting builder component.")        local original_dobuild = component.DoBuild        component.DoBuild = function(self, recname, pt)        local recipe = GLOBAL.GetRecipe(recname)        if recipe and recipe.oncraftfn then                print("OnCraftFn sucessfully integrated.")                recipe.oncraftfn()        end                original_dobuild(self, recname, pt)        end    	endAddComponentPostInit("builder", OnBuildFn)			--Warhammer Testlocal warhammer = GLOBAL.Recipe("warhammer",{Ingredient("nightmarefuel", 2)}, RECIPETABS.TOOLS, TECH.MAGIC_TWO) local function warhammer_return()        local ingredients = {                "spear",                "spear",        }         for k,ingredient in pairs(ingredients) do                local item = SpawnPrefab(ingredient)                local player = GLOBAL.GetPlayer()                print("Giving player ingredients back.")                player.components.inventory:GiveItem(item)        endend warhammer.oncraftfn = warhammer_return

Does anyone have any idea on why it is doing this and if it is fixable, if so how? I would really appreciate any help, as I am pretty desperate right now.

Link to comment
Share on other sites

@bigpak Ahh.. hmm. I think this whole code is a little more complicated than it needs to be. Firstly, you don't need to modify the recipe file at all. You're already checking for self.oncraftfn in the builder, so it would work either way. It's best to not replace files, especially when you're only changing a small portion.

 

The OnBuild function returns a boolean value, you aren't checking/storing that at all, or returning anything in your replacement function.

I'd suggest to put it like this, to ensure it cooperates with the original functionality.

local function OnBuildFn(component)	print("Overwriting builder component.")	local original_dobuild = component.DoBuild	component.DoBuild = function(self, recname, pt)		-- Call original fn, store returned value		local orig_return = original_dobuild(self, recname, pt)		local recipe = GLOBAL.GetRecipe(recname)		-- Only execute code if the orig fn worked		if (orig_return and recipe.oncraftfn) then			-- Meaning the recipe is possible.			-- You don't want to execute the oncraftfn if the player can't make the recipe			print("OnCraftFn sucessfully integrated.")			recipe.oncraftfn()		end		-- Return the orig value		return orig_return	endend
Link to comment
Share on other sites

Thanks, I felt like I would never figure out what you were saying or figure this entire problem out, and now its actually starting to make sense, the code I mean. I can now read code and for the most part understand it, if I study it long enough.

 

welp, onto animating :p

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...