Jump to content

How do I creating a toggle button?


Loken

Recommended Posts

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.

Link to comment
Share on other sites

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

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) then
SwitchPowers(inst)
end
end)
 
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 - 1
if inst.StacheTimer == 0 then
inst.StacheTimerGo:Cancel()
inst.StacheTimerGo = nil
end
end)
end
 
local function SwitchPowers(inst)
if inst.components.stache.current >= 100 then
if inst:HasTag("average") then
if inst.StacheTimer == 0 then
MightyWilford(inst)
SpawnPrefab("shirtrip").Transform:SetPosition(inst:GetPosition():Get())
if inst:HasTag("swear") then
inst.components.talker:Say(GetRandomItem(WarfstacheQuotes))
elseif inst:HasTag("slang") then
inst.components.talker:Say(GetRandomItem(WarfstacheQuotesSlang))
end
else
if inst.StacheTimer >= 21 then
inst.components.talker:Say("I need to wait before I do that again...")
elseif inst.StacheTimer >= 11 and inst.StacheTimer <= 20 then
inst.components.talker:Say("I still need to rest.")
elseif inst.StacheTimer >= 1 and inst.StacheTimer <= 10 then
inst.components.talker:Say("I'm almost done resting!")
end
end
else
AverageWilford(inst)
inst.StacheTimer = inst.StacheTimer + 30
PauseTheStache(inst)
end
else
AverageWilford(inst)
end
end
 
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! :D
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...