Jump to content

[Question] Is It Possible To Add Events ?


RCatta

Recommended Posts

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
Share on other sites

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
Share on other sites

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.

Link to comment
Share on other sites

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 = DROPSINGLE

and last, define the function being executed in your component:

function SingleDroppable:Execute(item, doer)    print(item)    print(doer)end

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

Link to comment
Share on other sites

Or if you want to stick to the event, you can do

local 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 = DROPSINGLE

and 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)
Link to comment
Share on other sites

 

Or if you want to stick to the event, you can do

local 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 = DROPSINGLE

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