SenL Posted June 11, 2022 Share Posted June 11, 2022 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 More sharing options...
Leonidas IV Posted June 12, 2022 Share Posted June 12, 2022 GLOBAL.TheInput:AddKeyDownHandler(key, function) -- Keys are in constants.lua (like GLOBAL.KEY_K) Link to comment https://forums.kleientertainment.com/forums/topic/140815-how-to-add-keyhandler/#findComment-1576605 Share on other sites More sharing options...
SenL Posted June 12, 2022 Author Share Posted June 12, 2022 (edited) 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 June 13, 2022 by SenL Link to comment https://forums.kleientertainment.com/forums/topic/140815-how-to-add-keyhandler/#findComment-1576629 Share on other sites More sharing options...
Leonidas IV Posted June 13, 2022 Share Posted June 13, 2022 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. 1 Link to comment https://forums.kleientertainment.com/forums/topic/140815-how-to-add-keyhandler/#findComment-1576645 Share on other sites More sharing options...
SenL Posted June 15, 2022 Author Share Posted June 15, 2022 Hi again. The keyhandler works but it works even when game is paused or console ( ` ) is up (and input bar is showing). What do I need to add to check if the game is paused? Thanks. Link to comment https://forums.kleientertainment.com/forums/topic/140815-how-to-add-keyhandler/#findComment-1576960 Share on other sites More sharing options...
Leonidas IV Posted June 16, 2022 Share Posted June 16, 2022 (edited) 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 June 16, 2022 by Leonidas IV 1 Link to comment https://forums.kleientertainment.com/forums/topic/140815-how-to-add-keyhandler/#findComment-1577077 Share on other sites More sharing options...
SenL Posted June 16, 2022 Author Share Posted June 16, 2022 Works. Thanks! Link to comment https://forums.kleientertainment.com/forums/topic/140815-how-to-add-keyhandler/#findComment-1577121 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