Hero Complex Posted January 14, 2018 Share Posted January 14, 2018 (edited) 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 January 14, 2018 by robert5 Link to comment https://forums.kleientertainment.com/forums/topic/86437-need-help-with-listenforevent-in-a-non-character-mod/ Share on other sites More sharing options...
Aquaterion Posted January 14, 2018 Share Posted January 14, 2018 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) Link to comment https://forums.kleientertainment.com/forums/topic/86437-need-help-with-listenforevent-in-a-non-character-mod/#findComment-992879 Share on other sites More sharing options...
Hero Complex Posted January 15, 2018 Author Share Posted January 15, 2018 Thanks! I'll give it a try and see how it goes. I expect to run into many more issues along the way, but at least now I can get past the first part to start working on them. Link to comment https://forums.kleientertainment.com/forums/topic/86437-need-help-with-listenforevent-in-a-non-character-mod/#findComment-992939 Share on other sites More sharing options...
Hero Complex Posted January 15, 2018 Author Share Posted January 15, 2018 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). Link to comment https://forums.kleientertainment.com/forums/topic/86437-need-help-with-listenforevent-in-a-non-character-mod/#findComment-992956 Share on other sites More sharing options...
Aquaterion Posted January 15, 2018 Share Posted January 15, 2018 (edited) 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 January 15, 2018 by Aquaterion Link to comment https://forums.kleientertainment.com/forums/topic/86437-need-help-with-listenforevent-in-a-non-character-mod/#findComment-992992 Share on other sites More sharing options...
Hero Complex Posted January 15, 2018 Author Share Posted January 15, 2018 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 Link to comment https://forums.kleientertainment.com/forums/topic/86437-need-help-with-listenforevent-in-a-non-character-mod/#findComment-993096 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now