Jump to content

How to Make and Item Constantly Check For


Zeklo

Recommended Posts

So I recently began modding and I'm wondering how do I make an item constantly check for something. In this case player health. I have the code working fine except that it only checks for the players health once they equip the item. This is because I have it in the onequip part. So where do I need to put it to constantly check?

 

This is the code that checks if the player health is below 100 and if so will then freeze enemies that hit them.

if owner.components.health.currenthealth < 100 then	inst.freezefn = function(attacked, data)		if data and data.attacker and data.attacker.components.freezable then			data.attacker.components.freezable:AddColdness(12.0)			data.attacker.components.freezable:SpawnShatterFX()		end 	endend
Link to comment
Share on other sites

The key to most object oriented environments is to avoid 'constantly' doing anything, as you want to keep those cpu cycles free for whatever else needs to be done.  As a general rule of thumb, you want to be hooking into events as much as possible to accomplish this type of task.  A "healthdelta" event gets pushed whenever health changes:

 

components/health.lua:

  self.inst:PushEvent("healthdelta", {oldpercent = old_percent, newpercent = self:GetPercent(), overtime=overtime, cause=cause})

You need to register an event listener for this event.  You can see that it pushes an object containing several values of interest to you. oldpercent you will want to make sure was equal to 100.  newpercent you will want to make sure is below 100, and cause you will want to make sure was an attacker(I would probably start by checking this, so the others dont need to be checked every time your health changes for any reason).

Link to comment
Share on other sites

I'll see what I can do. Thanks for the response.

 

EDIT: How can I check for what season it is? I know its WorldState but how can I do this with an equipable?

This is in the onequip section. It crashes because TheWorld is not defined or something along those lines.

if TheWorld.state.issummer then		inst.freezefn = function(attacked, data)			if data and data.attacker and data.attacker.components.freezable then				data.attacker.components.freezable:AddColdness(12.0)				data.attacker.components.freezable:SpawnShatterFX()			end 		end	end
Link to comment
Share on other sites

 

I'll see what I can do. Thanks for the response.

 

EDIT: How can I check for what season it is? I know its WorldState but how can I do this with an equipable?

This is in the onequip section. It crashes because TheWorld is not defined or something along those lines.

if TheWorld.state.issummer then		inst.freezefn = function(attacked, data)			if data and data.attacker and data.attacker.components.freezable then				data.attacker.components.freezable:AddColdness(12.0)				data.attacker.components.freezable:SpawnShatterFX()			end 		end	end

 

GetWorld() == TheWorld

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...