Loken Posted October 10, 2014 Share Posted October 10, 2014 (edited) Hello all,So I'm trying to make a toggle button for my character. Just to see if it's possible for now then maybe expand it to do all sorts of things later. But I've ran into a problems that I can't figure out...I want to have a ListenForEvent function within my character .lua file that looks for a certain keypress and when that keypress is done it runs a function, only problem is I have no idea what the keypress event should look like. Example of what I mean below.inst:ListenForEvent( [A KEYPRESS EVENT?], function() testfn(inst) end, GetWorld() ) Thanks for any help and pointers!EDIT: Nevermind, I was able to figure it out after staring at the code for a few hours. Dunno how to delete this though. Edited October 10, 2014 by Loken Link to comment https://forums.kleientertainment.com/forums/topic/41789-how-do-i-creating-a-toggle-button/ Share on other sites More sharing options...
Mobbstar Posted October 10, 2014 Share Posted October 10, 2014 Would you mind leaving a hint to your solution? Just for future reference, and other people who struggle with this. Link to comment https://forums.kleientertainment.com/forums/topic/41789-how-do-i-creating-a-toggle-button/#findComment-548571 Share on other sites More sharing options...
Loken Posted October 11, 2014 Author Share Posted October 11, 2014 I figured using a ListenForEvent for a keypress isn't possible (I may be wrong, but no amount of fiddling helped figure it out for me), so I created a keypress handler in the modmain.lua. Only problem is that all I can make it do is a print function since I don't fully understand how the handlers in the input.lua and event.lua work and every time I try to make my keypress call a function it crashes the game.If you want an example of a keypress function you can look at the Too Many Items mod (http://steamcommunity.com/sharedfiles/filedetails/?id=257781035&searchtext=too+many+items), I got the code for what functions in the input.lua to use from it. Though they are clearly not the ones I actually want to use since my keypress function doesn't work how I intend it to... Link to comment https://forums.kleientertainment.com/forums/topic/41789-how-do-i-creating-a-toggle-button/#findComment-549257 Share on other sites More sharing options...
Fidooop Posted October 12, 2014 Share Posted October 12, 2014 (edited) Here's a good example for you, strait out of my Wilford Warfstache character from the Markiplier mod! this is in his fn: TheInput:AddKeyDownHandler(KEY_X, function()if not IsPaused() and not TheInput:IsKeyDown(KEY_CTRL) and not TheInput:IsKeyDown(KEY_SHIFT) and not TheInput:IsKeyDown(KEY_ALT) thenSwitchPowers(inst)endend) and these functions run everytime you press 'x' (switch powers will call to the other functions)it's a nifty little bit of code that allows wilford to buff if his stache badge has a value over 100 and it even has a cooldown! local function MightyWilford(inst)inst:AddTag("mighty")inst:RemoveTag("average")inst.components.health:StartRegen(1, 5)inst.components.hunger:SetRate(TUNING.WILSON_HUNGER_RATE * 6)inst.components.sanity.dapperness = (TUNING.DAPPERNESS_MED * 0.5)inst.components.sanity.neg_aura_mult = (0.6) inst.components.sanity.night_drain_mult = (0.6)inst.components.health.absorb = (0.4)inst.components.combat.damagemultiplier = (1.8)inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1.2)inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.2)inst.Transform:SetScale(1.15,1.15,1.15)inst.components.eater:SetCarnivore()inst.AnimState:SetBuild("mightywilford")inst.components.stache:SetRate(TUNING.WILSON_HUNGER_RATE * 35)end local function AverageWilford(inst)inst:AddTag("average")inst:RemoveTag("mighty")inst.components.health:StartRegen(0, 5)inst.components.hunger:SetRate(TUNING.WILSON_HUNGER_RATE * 1.1)inst.components.sanity.dapperness = (TUNING.DAPPERNESS_MED * 0.1)inst.components.sanity.neg_aura_mult = (0.8) inst.components.sanity.night_drain_mult = (0.8)inst.components.health.absorb = (0)inst.components.combat.damagemultiplier = (1.2)inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1.1)inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.1)inst.Transform:SetScale(1.1,1.1,1.1)inst.components.eater:SetOmnivore()inst.AnimState:SetBuild("wilford")inst.components.stache:SetRate(TUNING.WILSON_HUNGER_RATE * 0.8)end local function PauseTheStache(inst)inst.StacheTimerGo = inst:DoPeriodicTask(1, function()inst.StacheTimer = inst.StacheTimer - 1if inst.StacheTimer == 0 theninst.StacheTimerGo:Cancel()inst.StacheTimerGo = nilendend)end local function SwitchPowers(inst)if inst.components.stache.current >= 100 thenif inst:HasTag("average") thenif inst.StacheTimer == 0 thenMightyWilford(inst)SpawnPrefab("shirtrip").Transform:SetPosition(inst:GetPosition():Get())if inst:HasTag("swear") theninst.components.talker:Say(GetRandomItem(WarfstacheQuotes))elseif inst:HasTag("slang") theninst.components.talker:Say(GetRandomItem(WarfstacheQuotesSlang))endelseif inst.StacheTimer >= 21 theninst.components.talker:Say("I need to wait before I do that again...")elseif inst.StacheTimer >= 11 and inst.StacheTimer <= 20 theninst.components.talker:Say("I still need to rest.")elseif inst.StacheTimer >= 1 and inst.StacheTimer <= 10 theninst.components.talker:Say("I'm almost done resting!")endendelseAverageWilford(inst)inst.StacheTimer = inst.StacheTimer + 30PauseTheStache(inst)endelseAverageWilford(inst)endend so in conclusion... copy what I said I put in wilf's fn and change it to what key(s) you want to be pressed! and make it run off to a function that does what you want it too! Edited October 12, 2014 by Fidooop Link to comment https://forums.kleientertainment.com/forums/topic/41789-how-do-i-creating-a-toggle-button/#findComment-549943 Share on other sites More sharing options...
Loken Posted October 12, 2014 Author Share Posted October 12, 2014 (edited) Thank you so much! That worked perfectly. I guess I was too stuck on the notion of putting the key hander in the modmain.lua. Couldn't see past it.Thanks again! Edited October 12, 2014 by Loken Link to comment https://forums.kleientertainment.com/forums/topic/41789-how-do-i-creating-a-toggle-button/#findComment-549964 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