Jump to content

Prohibition on equipping item


Recommended Posts

9 minutes ago, Ultroman said:

Just extend the Equip function on its equippable-component, and do


if owner.prefab ~= "wilson"
	return
end

-- run the original function

 

Should this work?

AddPrefabPostInit("greenamulet", function(inst)
	if GetModConfigData("wilson_ezrecipe") > 0 and GLOBAL.TheWorld.ismastersim then
		local function onequip_green(inst, owner)
			if owner.prefab ~= "wilson"
				return
			end
			owner.AnimState:OverrideSymbol("swap_body", "torso_amulets", "greenamulet")
			owner.components.builder.ingredientmod = TUNING.GREENAMULET_INGREDIENTMOD
			inst.onitembuild = function()
				inst.components.finiteuses:Use(1)
			end
			inst:ListenForEvent("consumeingredients", inst.onitembuild, owner)

		end
	end
end)

Oh, error, need add "then".

I don't know why, but AddPrefabPostInit don't work.

I use this code:

	if owner.prefab == "wilson" then
		owner.components.builder.ingredientmod = TUNING.WILSON_GREEN
	else
		owner.components.builder.ingredientmod = 1
	end

 

Edited by Tezumoto
Link to comment
Share on other sites

No, that won't work. You can't override local functions like that. You should also try your best to avoid copying whole functions and overriding them. Instead, you should extend them. There is a SetOnEquip function for when you want to change the onequip function, since it's a local function which you cannot access.

Also, that's not what I said you should do. I said to extend the Equip function (it's not local, so it's easy). If you override the onequip function, you're too "late in the game" to prevent the whole equipping of the item. The onequip function is executed as the item is being equipped, so at that point it'll be equipped no matter what you do.

This is how you extend a function. The Equip function is not local, so it's easy.

AddPrefabPostInit("greenamulet", function(inst)
	if GetModConfigData("wilson_ezrecipe") > 0 and GLOBAL.TheWorld.ismastersim then
		-- Store the original Equip function (this will keep it compatible with other mods which might also have extended this function)
		local old_equip = inst.components.equippable.Equip
		-- Replace the Equip function with a new one. Notice that we add "self" to it, since the function is declared with a :.
		inst.components.equippable.Equip = function (self, owner)
			if owner.prefab ~= "wilson"
				return
			end
			-- Run the original Equip function, if your check above has not already returned from our function.
			old_equip(self, owner)
		end
	end
end)

 

Edited by Ultroman
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...