[API] Skins for modded items 1.0.2

Sign in to follow this  

1 Screenshot

About This File

This is the API for making your own skins for modded items. Guide on how to do it is here:

 


What's New in Version 1.0.2   See changelog

Released

- Fixed reskin tool crash when playing on dedicated servers




User Feedback

Recommended Comments

 My game froze and crashed with the API when someone spawned a clean sweeper.

This is the error I got:
 

Spoiler

[00:30:47]: error calling PrefabPostInit: reskin_tool in mod workshop-1422448457 (Thomas, The Skeleton Chef): 
[string "../mods/workshop-1422448457/scripts/tools/i..."]:222: attempt to index field 'spellcaster' (a nil value)
LUA ERROR stack traceback:
        ../mods/workshop-1422448457/scripts/tools/itemskins_api.lua(222,1)
        =(tail call) ?
        =[C] in function 'xpcall'
        scripts/mods.lua(162,1) in function 'mod'
        scripts/mainfunctions.lua(267,1)	
[00:30:47]: Disabling workshop-1422448457 (Thomas, The Skeleton Chef) because it had an error.	
[00:30:47]: [string "../mods/workshop-1422448457/scripts/tools/i..."]:222: attempt to index field 'spellcaster' (a nil value)
LUA ERROR stack traceback:
        ../mods/workshop-1422448457/scripts/tools/itemskins_api.lua(222,1)
        =(tail call) ?
        =[C] in function 'xpcall'
        scripts/mods.lua(162,1) in function 'mod'
        scripts/mainfunctions.lua(267,1)
[00:30:48]: [string "../mods/workshop-1422448457/scripts/tools/i..."]:222: attempt to index field 'spellcaster' (a nil value)
LUA ERROR stack traceback:
        ../mods/workshop-1422448457/scripts/tools/itemskins_api.lua(222,1)
        =(tail call) ?
        =[C] in function 'xpcall'
        scripts/mods.lua(162,1) in function 'mod'
        scripts/mainfunctions.lua(267,1)	
[00:30:48]: error calling PrefabPostInit: reskin_tool in mod workshop-1341057026 (Stormish): 
[string "../mods/workshop-1341057026/scripts/tools/i..."]:222: attempt to index field 'spellcaster' (a nil value)
LUA ERROR stack traceback:
        ../mods/workshop-1341057026/scripts/tools/itemskins_api.lua(222,1)
        =(tail call) ?
        =[C] in function 'xpcall'
        scripts/mods.lua(162,1) in function 'mod'
        scripts/mainfunctions.lua(267,1)	
[00:30:48]: Disabling workshop-1341057026 (Stormish) because it had an error.	

 

 

Share this comment


Link to comment
Share on other sites

 I found out the reason why the game crashes when the Clean Sweeper is spawned.

The reason why it crashed is because you didn't rmake it check if the spellcaster component existed or not and since it's doing this on client side, it wasn't able to find the spellcaster component so the game crashed for that very reason, hence why it says spellcaster is a "nil" value.

This is what the PrefabPostInit function for "reskin_tool" should look like now in the API code:

Spoiler

env.AddPrefabPostInit("reskin_tool", function(inst)
	local spellcaster = inst.components.spellcaster
	if spellcaster ~= nil then
		local _can_cast_fn = spellcaster.can_cast_fn
		spellcaster.can_cast_fn = function(doer, target, ...)
			if HasSkins(target) then
				return true
			end
			
			return _can_cast_fn(doer, target, ...)
		end
		
		-- Our custom reskinner!
		local _spell = spellcaster.spell
		spellcaster.spell = function(inst, target, ...)
			target = target or inst.components.inventoryitem.owner --if no target, then get the owner of the tool. Self target for beards
			if not HasSkins(target) then
				return _spell(inst, target, ...)
			end
			
			SpawnAt("explode_reskin", target)

			local prefab = target.base_prefab or target.prefab
			local skin = target.skinname or target.prefab
			
			if not inst._cached_reskinname[prefab] then
				inst._cached_reskinname[prefab] = skin
			end
			
			while inst._cached_reskinname[prefab] == skin do
				for item_type, _ in pairs(MODDED_SKINS[prefab]) do
					if item_type ~= skin then
						inst._cached_reskinname[prefab] = item_type
						break
					end
				end
			end
			
			ReskinModEntity(target, inst._cached_reskinname[prefab])
		end
	end
end)

 

 

Edited by Stormish

Share this comment


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