Jump to content

(SOLVED)Need some help with event listeners (item that restores its durability every morning )


Recommended Posts

As i said in the title i want to make a weapon that is limited to 1 use per day, i tough of doing it by making its durability 1, and every morning restoring it back, but i cant seem to find a way to get that to happen every morning, i thought of using ListenForEvent() but i don't really know how it works, and i don't know if there's an event for that, the part of restoring the durability  i tried to doing it with

local function Morning("inst","dont know if i should add other variable")
		if inst.components.finiteuses.current == 0 and TheWorld.state.isday then
 			inst.components.finiteuses:SetUses(1)
		end
end

but i don't understand event listeners very well, im kinda new to coding

thanks in advance

Edited by Hector1324
Link to comment
Share on other sites

local function Morning(inst)
	if inst.components.finiteuses.current == 0 and TheWorld.state.isday then
		inst.components.finiteuses:SetUses(1)
	end
end

You need this function,

inst:ListenForEvent("phasechanged", Morning) --this
================================
inst:WatchWorldState("isday", Morning) --or this one

and add one line in your item's main function.

I don't know below one would work but upper one should be work.

  • Thanks 1
Link to comment
Share on other sites

Thanks, it worked with WatchWorldState. I checked worldstate.lua and i wondered if it was that event, thanks, so i need to write the events in lowercase, i have another question, can i create a custom event, for example i want to make an item that, after being used, grants the weapon another use, so can i create an event on the item that gets activated after being used and place somethting like:
 

local function Mana(inst)
		inst.components.finiteuses:DoDelta(1)
end

===================================
 inst:ListenForEvent("eaten", Mana)

with eaten being a new event created for my item?

Link to comment
Share on other sites

Yes, you can make custom events.

but inst:ListenForEvent, will only 'listen' to event, so you have to push the event first,

inst:PushEvent("eventname", value_to_send, can_be_more_than_one)

Like this one.

In this case, you have to make consumable(edible I guess?) item coded like:

local function PushEvent(inst, eater)
	if eater.components.inventory then
		local item = eater.components.inventory:FindItem(function(item) return item.prefab == "youritemprefabname" end)
		if item ~= nil then
			item:PushEvent("eaten")
		end
	end
end
============================

inst:AddComponent("edible")
inst.components.edible.oneaten = PushEvent

wow what a mess

It will find your item is in your inventory, then will push event to that item.

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