Yakuzashi

[Code] Error inside item's prefab function

Recommended Posts

Yakuzashi    81
local function error(inst)           
        inst.components.health:DoDelta(-15) 
end

Code above is located inside custom item's prefab. How to make it work ? I have tried with player/owner instead of inst but it resulted in error anyway.

 

Share this post


Link to post
Share on other sites
Ultroman    619

What do you mean "make it work"? You call the function, providing it an instance of an entity as the parameter.

local function error(inst)           
        inst.components.health:DoDelta(-15) 
end

local function someOtherFunction(inst)           
        error(inst)
end
Edited by Ultroman
  • Like 1

Share this post


Link to post
Share on other sites
Yakuzashi    81

You are right, I should precise it. So the error occurs when I try to call function 'error' activated by component:
 

Quote

   inst:AddComponent("spellcaster")
   inst.components.spellcaster:SetSpellFn(error)

Caster's HP should decrease by 15 points, but instead the error pops up.

Quote

attempt to index field 'health' (a nil value)

Share this post


Link to post
Share on other sites
Ultroman    619

Does the inst with the spellcaster component also have a health component? Are those lines maybe placed before the health component is added? If this is a player character, then they should have a health component automatically. If it is on a weapon, then you're trying to remove health from the weapon. In order to get the holder of the weapon, you need to look at its inventoryitem component, and get its "grand owner" (when in a container, like the backpack, the owner is the backpack, but the grand owner is the player), and then see if it has a health component, and if it does, give it damage.

local function error(inst)
	if inst then
		local invitem = inst.components.inventoryitem
		if invitem then
			local owner = invitem:GetGrandOwner()
			if owner and owner.components.health then
				owner.components.health:DoDelta(-15) 
			end
		end
	end
end

 

Edited by Ultroman
  • Thanks 1

Share this post


Link to post
Share on other sites
Yakuzashi    81
15 hours ago, Ultroman said:

Does the inst with the spellcaster component also have a health component? Are those lines maybe placed before the health component is added? If this is a player character, then they should have a health component automatically. If it is on a weapon, then you're trying to remove health from the weapon. In order to get the holder of the weapon, you need to look at its inventoryitem component, and get its "grand owner" (when in a container, like the backpack, the owner is the backpack, but the grand owner is the player), and then see if it has a health component, and if it does, give it damage.


local function error(inst)
	if inst then
		local invitem = inst.components.inventoryitem
		if invitem then
			local owner = invitem:GetGrandOwner()
			if owner and owner.components.health then
				owner.components.health:DoDelta(-15) 
			end
		end
	end
end

 

Thanks, this works just fine.

  • Like 1

Share this post


Link to post
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