Jump to content

[Code] Error inside item's prefab function


Recommended Posts

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
Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

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...