Yakuzashi Posted August 24, 2018 Share Posted August 24, 2018 I was wondering how to trigger an action by pressing a key. Maybe someone here knows how to make it (and is willing to share knowledge with others). Thanks to K1NGT1GER609 I finished Mosin Nagant rifle for my character, but now I would like to upgrade PPSh SMG with hold to fire button. I have met so called keyhandlers before during my research, but problem here is to attach an action for a key. For instance in PPSh prefab: local assets= { Asset("ANIM", "anim/ppsh_smg.zip"), Asset("ANIM", "anim/swap_ppsh_smg.zip"), Asset("ATLAS", "images/inventoryimages/ppsh_smg.xml"), Asset("IMAGE", "images/inventoryimages/ppsh_smg.tex"), } local prefabs = { } local function OnEquip(inst, owner) owner.AnimState:OverrideSymbol("swap_object", "swap_ppsh_smg", "ppsh") owner.AnimState:Show("ARM_carry") owner.AnimState:Hide("ARM_normal") end local function OnUnequip(inst, owner) owner.AnimState:Hide("ARM_carry") owner.AnimState:Show("ARM_normal") end local function onattack_gun(inst, owner, target) inst.SoundEmitter:PlaySound("dontstarve/ghost/bloodpump") end local function fn(Sim) local inst = CreateEntity() inst.entity:AddTransform() inst.entity:AddAnimState() inst.entity:AddNetwork() inst.entity:AddSoundEmitter() MakeInventoryPhysics(inst) inst.AnimState:SetBank("ppsh") inst.AnimState:SetBuild("ppsh_smg") inst.AnimState:PlayAnimation("idle") inst:AddTag("sharp") if not TheWorld.ismastersim then return inst end inst.entity:SetPristine() inst:AddComponent("inventoryitem") inst.components.inventoryitem.imagename = "ppsh_smg" inst.components.inventoryitem.atlasname = "images/inventoryimages/ppsh_smg.xml" inst:AddComponent("inspectable") inst:AddComponent("equippable") MakeHauntableLaunch(inst) inst.components.equippable:SetOnEquip( OnEquip ) inst.components.equippable:SetOnUnequip( OnUnequip ) inst:AddComponent("finiteuses") inst.components.finiteuses:SetMaxUses(2000) inst.components.finiteuses:SetUses(2000) inst.components.finiteuses:SetOnFinished(inst.Remove) inst:AddComponent("weapon") inst.components.weapon:SetDamage(25) inst.components.weapon:SetRange(14) inst.components.weapon:SetProjectile("smgammo") inst.components.weapon:SetOnAttack(onattack_gun) inst.HasAmmo = function(inst, owner) if (owner and owner.components.inventory and owner.components.inventory:Has("smgammo", 1)) then owner.components.inventory:ConsumeByName("smgammo", 1) inst.components.weapon:SetDamage(25) return true end return false end return inst end return Prefab("common/inventory/ppsh_smg", fn, assets, prefabs) What line shall I write into code to make weapon firing a bullet every key press ? Link to comment https://forums.kleientertainment.com/forums/topic/94770-code-press-to-attack/ Share on other sites More sharing options...
K1NGT1GER609 Posted September 7, 2018 Share Posted September 7, 2018 Keyhandlers is a long order for what you want so here's a start. local function onattack_gun(inst, owner, target, data) inst.SoundEmitter:PlaySound("dontstarve/ghost/bloodpump") if data.inst == ThePlayer then--make sure its a player if data.inst.HUD and (data.inst.HUD:IsChatInputScreenOpen() or data.inst.HUD:IsConsoleScreenOpen()) then return end --make sure not to trigger on chat or console if owner and owner.components.inventory and owner.components.inventory:Has("smgammo", 1) and data:IsKeyDown(KEY_R) then --or is it TheInput:IsKeyDown(KEY_R) inst.components.weapon:LaunchProjectile(inst, target) end end local function fn(Sim) if not TheWorld.ismastersim then return inst end --reference inst:AddComponent("keyhandler") The only main part thats missing is adding a additional component keyhandler by making a folder in: scripts-components-keyhandler.lua. other than that this code hasn't been tested and this material is still new to me. keyhandler.lua Link to comment https://forums.kleientertainment.com/forums/topic/94770-code-press-to-attack/#findComment-1081975 Share on other sites More sharing options...
Yakuzashi Posted September 7, 2018 Author Share Posted September 7, 2018 When I start the game and press "R" then nothing happens, but if I attack something I see error. Link to comment https://forums.kleientertainment.com/forums/topic/94770-code-press-to-attack/#findComment-1081991 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