RCatta Posted September 19, 2013 Share Posted September 19, 2013 So yeah , like the title says , is it possible to add custom events to 'listen' for ? Such as: inst:ListenForEvent ("attacked", dosomecoolstuff) inst:ListenForEvent("lightningstrike",dosomething) Those events are already present in the game. Don't exactly know where and how they work yet. Link to comment https://forums.kleientertainment.com/forums/topic/27966-question-is-it-possible-to-add-events/ Share on other sites More sharing options...
Heavenfall Posted September 19, 2013 Share Posted September 19, 2013 Yes it is. Just pay careful attention to wat entity issues the event. Link to comment https://forums.kleientertainment.com/forums/topic/27966-question-is-it-possible-to-add-events/#findComment-325507 Share on other sites More sharing options...
Malacath Posted September 19, 2013 Share Posted September 19, 2013 (edited) If it was part of your question then the code for doing that is:inst:PushEvent("myevent", {something = somevariable, othervar = lastvar})And always keep in mind what Heavenfall said. Edited September 19, 2013 by Malacath Link to comment https://forums.kleientertainment.com/forums/topic/27966-question-is-it-possible-to-add-events/#findComment-325548 Share on other sites More sharing options...
TheDanaAddams Posted September 23, 2013 Share Posted September 23, 2013 Okay, so I just can't get this to work... I'm clearly doing something wrong... One of my items has an "on drop" effect that kicks in by listening for "ondropped" - that works... but what I want to do, is make it only happen when I drop it through its "single drop" action.I think I can do it just by pushing a new event with drop single, and telling it to listen for that new event... but I can't get the event to happen.This is tricky stuff, so I don't really know what I'm doing... Here's the dropsingle code in my modmain:local DROPSINGLE = Action(1, true, true)DROPSINGLE.id = "DROPSINGLE"DROPSINGLE.str = "Drop"DROPSINGLE.fn = function(act) if act.doer.components.inventory then return act.doer.components.inventory:DropItem(act.invobject, false, false, act.pos) end endAddAction(DROPSINGLE)GLOBAL.ACTIONS.DROPSINGLE = DROPSINGLE The activation code, and component in the item prefab file: inst:AddComponent("singledroppable") -- inst:ListenForEvent("ondropped", function(inst) local fire = SpawnPrefab("dinfire") fire.Transform:SetPosition(inst.Transform:GetWorldPosition()) GetPlayer().components.health.fire_damage_scale = 0 GetPlayer():AddTag("dinsfire") inst:Remove() ------------------------------ GetPlayer().components.inventory:GiveItem( SpawnPrefab("dinrock") ) ------------------------------ inst:DoTaskInTime(15, function(inst) if not GetPlayer():HasTag("wearingredtunic") then GetPlayer().components.health.fire_damage_scale = 1 GetPlayer():RemoveTag("dinsfire") end end, inst)end) -- And the singledrop component Lua file:local SingleDroppable = Class(function(self, inst) self.inst = instend)function SingleDroppable:CollectInventoryActions(doer, actions) table.insert(actions, ACTIONS.DROPSINGLE)endreturn SingleDroppable In its current state, it works - I just only want it to perform the "on dropped" action when I use the "drop single" right-click action... so I can drop it normally by grabbing it in the inventory and putting it on the ground, without triggering the effect. I have been trying for hours, and I've just gone around in circles... >.< Link to comment https://forums.kleientertainment.com/forums/topic/27966-question-is-it-possible-to-add-events/#findComment-328486 Share on other sites More sharing options...
Heavenfall Posted September 23, 2013 Share Posted September 23, 2013 (edited) Edit: Nvm, that solution won't work. So, just to be clear, you have a stackable item in your inventory. You can drop it on the ground in a stack, and you can drop it in singles on the ground, but only when you activate your special action do you want something to happen, yes? Then I would suggest that you do not drop the item at all, simply remove the item from your inventory and follow up with what you want to happen. Edited September 23, 2013 by Heavenfall Link to comment https://forums.kleientertainment.com/forums/topic/27966-question-is-it-possible-to-add-events/#findComment-328493 Share on other sites More sharing options...
TheDanaAddams Posted September 23, 2013 Share Posted September 23, 2013 Edit: Nvm, that solution won't work.I was just about to say that the item doesn't stack. I wasn't sure how that would work, but I guess that's why. xD Link to comment https://forums.kleientertainment.com/forums/topic/27966-question-is-it-possible-to-add-events/#findComment-328495 Share on other sites More sharing options...
Heavenfall Posted September 23, 2013 Share Posted September 23, 2013 (edited) If it doesn't stack, this is how I would approach it: first, skip the whole listenforevent thing. It's not going to get you where you want. You still need to add the component to the item, of course. Second, alter your action so it executes a function in your item's component:local DROPSINGLE = GLOBAL.Action(1, true, true)DROPSINGLE.id = "DROPSINGLE"DROPSINGLE.str = "Drop"DROPSINGLE.fn = function(act) if act.doer.components.inventory then return act.invobject.components.singledroppable:Execute(act.invobject, act.doer) end endAddAction(DROPSINGLE)GLOBAL.ACTIONS.DROPSINGLE = DROPSINGLEand last, define the function being executed in your component:function SingleDroppable:Execute(item, doer) print(item) print(doer)endThat final function you'll have to tweak yourself, but it should be a small thing (I printed the parameters so you know what is what). Edited September 23, 2013 by Heavenfall Link to comment https://forums.kleientertainment.com/forums/topic/27966-question-is-it-possible-to-add-events/#findComment-328497 Share on other sites More sharing options...
Heavenfall Posted September 23, 2013 Share Posted September 23, 2013 (edited) Or if you want to stick to the event, you can dolocal DROPSINGLE = Action(1, true, true)DROPSINGLE.id = "DROPSINGLE"DROPSINGLE.str = "Drop"DROPSINGLE.fn = function(act) if act.doer.components.inventory then act.invobject.didwedropsingle = true return act.doer.components.inventory:DropItem(act.invobject, false, false, act.pos) end endAddAction(DROPSINGLE) GLOBAL.ACTIONS.DROPSINGLE = DROPSINGLEand in the listenforevent inst:ListenForEvent("ondropped", function(inst) if inst.didwedropsingle == true then local fire = SpawnPrefab("dinfire") fire.Transform:SetPosition(inst.Transform:GetWorldPosition()) GetPlayer().components.health.fire_damage_scale = 0 GetPlayer():AddTag("dinsfire") inst:Remove() ------------------------------ GetPlayer().components.inventory:GiveItem( SpawnPrefab("dinrock") end) ------------------------------ inst:DoTaskInTime(15, function(inst) if not GetPlayer():HasTag("wearingredtunic") then GetPlayer().components.health.fire_damage_scale = 1 GetPlayer():RemoveTag("dinsfire") end end, inst) Edited September 23, 2013 by Heavenfall Link to comment https://forums.kleientertainment.com/forums/topic/27966-question-is-it-possible-to-add-events/#findComment-328501 Share on other sites More sharing options...
TheDanaAddams Posted September 23, 2013 Share Posted September 23, 2013 Or if you want to stick to the event, you can dolocal DROPSINGLE = Action(1, true, true)DROPSINGLE.id = "DROPSINGLE"DROPSINGLE.str = "Drop"DROPSINGLE.fn = function(act) if act.doer.components.inventory then act.invobject.didwedropsingle = true return act.doer.components.inventory:DropItem(act.invobject, false, false, act.pos) end endAddAction(DROPSINGLE) GLOBAL.ACTIONS.DROPSINGLE = DROPSINGLEand in the listenforevent inst:ListenForEvent("ondropped", function(inst) if inst.didwedropsingle == true then local fire = SpawnPrefab("dinfire") fire.Transform:SetPosition(inst.Transform:GetWorldPosition()) GetPlayer().components.health.fire_damage_scale = 0 GetPlayer():AddTag("dinsfire") inst:Remove() ------------------------------ GetPlayer().components.inventory:GiveItem( SpawnPrefab("dinrock") ) ------------------------------ inst:DoTaskInTime(15, function(inst) if not GetPlayer():HasTag("wearingredtunic") then GetPlayer().components.health.fire_damage_scale = 1 GetPlayer():RemoveTag("dinsfire") endend end, inst)Okay, I can definitely understand what this one is doing - I'll use this.Looks like it should work perfectly. Thank you very much! <3 Link to comment https://forums.kleientertainment.com/forums/topic/27966-question-is-it-possible-to-add-events/#findComment-328502 Share on other sites More sharing options...
Heavenfall Posted September 23, 2013 Share Posted September 23, 2013 @TheDanaAddams I moved an "end", make sure you get the edited version above. Link to comment https://forums.kleientertainment.com/forums/topic/27966-question-is-it-possible-to-add-events/#findComment-328507 Share on other sites More sharing options...
TheDanaAddams Posted September 23, 2013 Share Posted September 23, 2013 @TheDanaAddams I moved an "end", make sure you get the edited version above.No problem - I figured it out. =)Thanks again! Works perfectly. Does exactly what I want it to, and I can understand it, so if it breaks, I can hopefully fix it. xD Link to comment https://forums.kleientertainment.com/forums/topic/27966-question-is-it-possible-to-add-events/#findComment-328508 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