Jump to content

Recommended Posts

Let's say I want to bind button "x" to do special action for my mod (like groundpounder). How does one do that?

Few years ago I had Kzisor/Ysovuka helped me with this on DST (not DS) but this mod is not DST.

There's popular DS mod "Geometric Placement" that does complex things with KeyHandler but that's so beyond me.

Thanks.

Link to comment
https://forums.kleientertainment.com/forums/topic/140815-how-to-add-keyhandler/
Share on other sites

This works. However I still need help, I don't know how to pass itself (my mod) to the function. How do you do it?

In modmain.lua:

Quote

local function testAoE(inst) --tried with testAoE(), with testAoE(self) but none works
    local player = GLOBAL.GetPlayer()
    local x,y,z = player.Transform:GetWorldPosition()
    local ents = TheSim:FindEntities(x,y,z, 8)
    
    --this does not work
    --if self ~= nil and self.inst ~= nil and self.inst.prefab ~= nil then
    --    print (self.inst.prefab)
    --end
    
    --this does not work either
    if inst ~= nil and inst.prefab ~= nil then
        print (inst.prefab)
    end
    
    for i, mob in ipairs(ents) do
        if not mob:HasTag("structure") and not mob:HasTag("wall") and mob ~= player
        and not mob:HasTag("companion")
        and not (mob.components.follower and mob.components.follower.leader == player)
        and mob.components.health ~= nil
        and not mob.components.health:IsDead() then
            local mobx,moby,mobz = mob.Transform:GetWorldPosition()
            local fx = GLOBAL.SpawnPrefab("firesplash_fx")
            --inst.SoundEmitter:PlaySound("dontstarve/common/shrine/sadwork_fire") --cant do this because inst is nil
            fx.Transform:SetPosition(mobx, moby, mobz)
            
            if mob.components.health ~= nil then
                mob.components.health:DoDelta(-50)
            end
            if mob.components.sleeper and mob.components.sleeper:IsAsleep() then
                mob.components.sleeper:WakeUp()
            end
            if mob.sg then
                mob:PushEvent("attacked", {attacker = player, damage = 0})
            end
            if mob.components.combat ~= nil then
                mob.components.combat:SuggestTarget(player)
            end
        end
    end
end

GLOBAL.TheInput:AddKeyDownHandler(GLOBAL.KEY_X, testAoE)

Thanks.

Edit: I could get around it by getting player's equipped item but that feels hacky.

Quote

--get player's equipped item on neck/body
    if player.components.inventory ~= nil then
        if GLOBAL.EQUIPSLOTS.NECK ~= nil then
            amulet = player.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.NECK)
        else
            amulet = player.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.BODY)
        end
    end
    
    --check if its prefab is "mymod"
    if amulet ~= nil and amulet.prefab ~= nil then
        if amulet.prefab ~= "mymod" then
            return
        end
    else
        print ("amulet is nil")
        return
    end

 

Edited by SenL
4 hours ago, SenL said:

I could get around it by getting player's equipped item but that feels hacky.

Feels good.

 

4 hours ago, SenL said:

This works. However I still need help, I don't know how to pass itself (my mod) to the function. How do you do it?

Functions like AddPrefabPostInit insert the entity to the parameters of the function passed to it, so we have "inst" in AddPrefabPostInit. In AddComponentPostInit, what is passed to our function is the component itself.

In the case of a TheInput:AddKeyDownHandler, there is no entity being passed. You can see how this is done in input.lua (I think this is it)

Anyway, you can just use the mob to play the sound (or the player), just like you did with other parts.

  • Thanks 1
22 hours ago, SenL said:

What do I need to add to check if the game is paused?

GLOBAL.IsPaused()


For check if console is open (this return FALSE if console is open):

GLOBAL.TheInput:IsDebugToggleEnabled()



 

Edited by Leonidas IV
  • Like 1

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