Jump to content

[Solved] Custom item - Health and Sanity ingredient


Recommended Posts

Hey-ho!

I'm trying to create a custom item that would cost 20 Hunger and 20 Sanity to craft.

I've used the following coding, but it seems like I'm missing out something, the game crashes when I open the tab the item is in:

 

Spoiler

 

local require = GLOBAL.require
local STRINGS = GLOBAL.STRINGS

local AllRecipes = GLOBAL.AllRecipes
local _G = GLOBAL
local TUNING = _G.TUNING
local CHARACTER_INGREDIENT = GLOBAL.CHARACTER_INGREDIENT
local CHARACTER_INGREDIENT_SEG = GLOBAL.CHARACTER_INGREDIENT_SEG
local RECIPETABS = _G.RECIPETABS
local Ingredient = _G.Ingredient
local TECH = _G.TECH
local CUSTOM_RECIPETABS = _G.CUSTOM_RECIPETABS
local PREFAB_SKINS = _G.PREFAB_SKINS
local PREFAB_SKINS_IDS = _G.PREFAB_SKINS_IDS
local SKIN_AFFINITY_INFO = GLOBAL.require("skin_affinity_info")
 

TRINGS.NAMES.WISH_TAB = "Wish"

AddRecipe("wishpower",{ Ingredient(CHARACTER_INGREDIENT.HUNGER, 20), Ingredient(CHARACTER_INGREDIENT.SANITY, 20)}, wish_tab, TECH.NONE,nil,nil,nil,1,"lamprenter", "images/inventoryimages/wishpower.xml", "wishpower.tex")


STRINGS.RECIPE_DESC.WISHPOWER = "Wishing for more... wishes?"

 

I've attached the crash screen I've got.

What have I've messed up?

Névtelen.png

Edited by BillTheCipher
Link to comment
Share on other sites

The error is shown to be in recipe.lua file in line 34. If you go there you'll see this function:

function Ingredient:GetImage()
	if self.image == nil then
		self.image = self.type..".tex"
	end
	return self.image
end

 

This error is present because self.type is a nil value. If you scroll a bit up you'll see where this variable is created and assigned a value:

Ingredient = Class(function(self, ingredienttype, amount, atlas, deconstruct, imageoverride)
	--Character ingredient multiples of 5 check only applies to
	--health and sanity cost, not max health or max sanity
	if ingredienttype == CHARACTER_INGREDIENT.HEALTH or
		ingredienttype == CHARACTER_INGREDIENT.SANITY then
		--V2C: string solution due to inconsistent precision errors with math.floor
		--local x = math.floor(amount)
		local x = tostring(amount)
		x = x:sub(x:find("^%-?%d+"))
		x = tonumber(x:sub(x:len()))
		--NOTE: if you changed CHARACTER_INGREDIENT_SEG, then update this assert
		assert(x == 0 or x == 5, "Character ingredients must be multiples of "..tostring(CHARACTER_INGREDIENT_SEG))
	end
	self.type = ingredienttype -- <<< RIGHT HERE
	self.amount = amount
	self.atlas = atlas and resolvefilepath(atlas) or nil
	self.image = imageoverride
	self.deconstruct = deconstruct
end)

 

Since self.type is given the value of ingredienttype, the first variable when referring to the Ingredient it means that either CHARACTER_INGREDIENT.HUNGER or CHARACTER_INGREDIENT.SANITY is a nil value (basically doesn't exist). So I looked through constants.lua in search for these and found this:

CHARACTER_INGREDIENT =
{
	--NOTE: Value is used as key for NAME string and inventory image
	HEALTH = "decrease_health",
	MAX_HEALTH = "half_health",
	SANITY = "decrease_sanity",
	MAX_SANITY = "half_sanity",
}

 

As you can see there is no HUNGER in the CHARACTER_INGREDIENT table. That's why your recipe crashes the game.

  • Like 2
Link to comment
Share on other sites

From what I can read, ingredienttype determines what resources are consumed when crafting an item. Since hunger hasn't been used by the developers (or at least i think they never used it) as a resource they didn't code it into the game, but you can get around it using event listeners.

With event listeners you can listen for when your item is crafted and then surtract 20 points from hunger of the person who crafted the item. This method will not set the resource image however, since this function is coded in the recipe.lua.

function Ingredient:GetImage()
	if self.image == nil then
		self.image = self.type..".tex" -- This sets the image for the resource
	end
	return self.image
end

 

  • Like 2
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...