Jump to content

Recommended Posts

While I'm sure there's many other issues with my mod, I can't start troubleshooting them until I get past the part where I figure out how to correctly use this:

inst:ListenForEvent("equipped", function(inst,data) weaponcheck(inst,data) 
	print("Weapon was equipped")
	end)

I know I need to wrap it in something, but don't know what that something is. From what I've seen, there appears to be AddPrefabPostInit and a function in the prefab called master_postinit. 

I've looked at so many examples, but this isn't a character. The function weaponcheck is in the modmain and what I'm trying to do is have the game listen for when an item was equipped, which then runs weaponcheck, a function that checks to see if the equipped item went into the hand slot and has the right tag. 

I'm sure there's many more issues with my mod, but I've never been able to start troubleshooting them because I've never been able to get it to print "Weapon was equipped" no matter what I've tried.

I'm going to keep trying anything I can find, but if someone knows the answer, I'd really appreciate the help.

Essentially, I just need this to work:

inst:ListenForEvent("equipped", weaponcheck)

 

Edited by robert5

issue is that that event is fired on ITEMS when they are equipped. You'd have to alter the equip function;

AddComponentPostInit("equippable", function(self)
	self.OldEquip = self.Equip
	function self:Equip(owner)
		self:OldEquip(owner)
		--[[either create a new event to listen to, so you would do inst:ListenForEvent("onequip", weaponcheck)]]
		owner:PushEvent("onequip", self.inst)
		--[[or just check what u need here]]
		--if self.inst:HasTag("your_tag") then
			--your code
		--end
	end
end)

 

So I followed your second suggestion first, but decided I liked the first way better. The problem for me is that I still don't know where to put the line,

inst:ListenForEvent("onequip", weaponcheck)

so that inst is defined properly.

Here's what I've got right now: 

AddComponentPostInit("equippable", function(self)
	self.OldEquip = self.Equip
	function self:Equip(owner)
		self:OldEquip(owner)
		owner:PushEvent("onequip", self.inst)
		print("Item was equipped")
	end
end)

Followed by 

local function weaponcheck(inst, item)
	if item.components.equippable.equipslot == EQUIPSLOTS.HANDS and item:HasTag("throwable") then
		local throwable = GLOBAL.SpawnSaveRecord(inst._item)
		print("Item is", throwable)
		throwable:AddComponent('weaponthrowable')
		throwable.components.weaponthrowable:SetRange(8, 10)
		throwable.components.weaponthrowable:SetOnAttack(weaponthrow_onattack)
		throwable.components.weaponthrowable:SetProjectile("throwable_projectile")

(the rest of the code follows after that).

since you probably want it for all characters, i would do it like so:

AddComponentPostInit("equippable", function(self)
	self.OldEquip = self.Equip
	function self:Equip(owner)
		self:OldEquip(owner)
		if self.inst:HasTag("throwable") and not self.inst.components.throwable then
			weaponcheck(owner, self.inst)
			print("throwable equipped")
		end
	end
end)

but what I don't get is, if you're adding the tag throwable to the items, why not just add the component and stats at the same time?

Edited by Aquaterion

I guess my concern is with later parts of the code. I've attached the entire modmain so it won't be a mystery. Right now I actually have the entire code wrapped in the weaponcheck function. Before when I was making weapons throwable, I was using Rezecib's throwable spears code, but giving each weapon its own copy of the code, a prefab, and a component. It was incredibly inefficient and I eventually ran into a problem where only a certain number of weapons would work in a single mod.  Plus it would take way too long to add even just one weapon. I'm trying to circumvent that by only having one copy of the original code, but with every mention of spear replaced by the weapon that the character is holding. The throwable tag was my solution to incorporating config options in case someone didn't want to throw a certain group of weapons.

But what you just suggested actually sounds really good to me. I'll add the component and its functions during the tagging process. If I could make it so the rest of the code use the item from the equipped event, it should work just fine. I'm just kinda bad at making sure that item is defined correctly and usable in all subsequent functions.

modmain.lua

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
×
  • Create New...