gregdwilson Posted May 20, 2015 Share Posted May 20, 2015 I'm trying to make a button that when pressed allows me to run a function/push an event for my character. I have created and placed the button and It is working up to the point where I can run functions that affect the character. Here is the code I have.function DragonClawPostConstruct(self) local offset = 0 local width = 1 local value = 75 self.DragonClaw = self:AddChild(ImageButton("images/hud/DragonClaw.xml", "DragonClaw.tex"), self.owner) self.DragonClaw:SetScale(1,1,1) self.DragonClaw:SetPosition(-296,190,0) self.DragonClaw:Show() self.DragonClaw:SetClickable(true) width = value/100 offset = 29 - width * 29 self.DragonClaw.DragonClaw_cd = self.DragonClaw:AddChild(Image("images/hud/DragonClaw_cd.xml", "DragonClaw_cd.tex"), self.owner) self.DragonClaw.DragonClaw_cd:SetScale(width,1,1) self.DragonClaw.DragonClaw_cd:SetPosition(offset, 0, 0) self.DragonClaw.DragonClaw_cd:Show() function self.DragonClaw.DragonClaw_cd:SetValue(value) --out of 100 width = value/100 offset = 29 - width * 29 self:SetScale(width,1,1) self:SetPosition(offset, 0, 0) end self.DragonClaw:SetOnClick(function() self.DragonClaw.DragonClaw_cd:SetValue(math.random(100)) self.inst:PushEvent("DragonClawClick") end) endand in my character config I added a listener under the master_postinitinst:ListenForEvent("DragonClawClick", DragonClawClick)and then a simple functionlocal function DragonClawClick(inst) inst.components.talker:Say("You Clicked Me!")endThe "DragonClawClick" event doesn't get recognized by my character though. I can see the value change when I click it but not the message. I eventually want to make this server friendly but first I just want to figure out how to make it work. Any help would be appreciated. Link to comment https://forums.kleientertainment.com/forums/topic/54201-hud-custom-button/ Share on other sites More sharing options...
DarkXero Posted May 20, 2015 Share Posted May 20, 2015 It's not self.inst, it's self.owner, in the button.In your character it is inst:ListenForEvent("DragonClawClick", DragonClawClick). Link to comment https://forums.kleientertainment.com/forums/topic/54201-hud-custom-button/#findComment-639121 Share on other sites More sharing options...
gregdwilson Posted May 20, 2015 Author Share Posted May 20, 2015 Thank you, that worked perfect. Link to comment https://forums.kleientertainment.com/forums/topic/54201-hud-custom-button/#findComment-639189 Share on other sites More sharing options...
gregdwilson Posted May 20, 2015 Author Share Posted May 20, 2015 So I ran this in a server and the event would no longer push. If I put the code inside the SetOnClick function instead of pushing the event and changed inst to self.owner then it worked but only sporadically. Is there something special that I need to do for it to work with host/clients? Link to comment https://forums.kleientertainment.com/forums/topic/54201-hud-custom-button/#findComment-639200 Share on other sites More sharing options...
DarkXero Posted May 21, 2015 Share Posted May 21, 2015 AddModRPCHandler(modname, "DCC", function(player) player:PushEvent("DragonClawClick")end)self.DragonClaw:SetOnClick(function() self.DragonClaw.DragonClaw_cd:SetValue(math.random(100)) SendModRPCToServer(MOD_RPC[modname]["DCC"])end)Get those two in modmain.self.DragonClaw inside the PostConstruct. Link to comment https://forums.kleientertainment.com/forums/topic/54201-hud-custom-button/#findComment-639296 Share on other sites More sharing options...
gregdwilson Posted May 21, 2015 Author Share Posted May 21, 2015 (edited) @DarkXero you are a genie. I got it working and the bugs worked out now I have a cosmetic thing I'm trying to fix. I make the character perform a custom action defined in modmain bylocal DRAGONCLAW = AddAction("DRAGONCLAW", "Dragon Claw", function(act) local target = act.target local attacker = act.doer local old_damage = attacker.components.combat.damagemultiplier attacker.components.combat.damagemultiplier = old_damage*5 attacker.components.combat:DoAttack(target) attacker.SoundEmitter:PlaySound("dontstarve/common/destroy_wood") GLOBAL.SpawnPrefab("mining_fx").Transform:SetPosition(target:GetPosition():Get()) attacker:PushEvent("DragonClawCooldown") attacker.components.combat.damagemultiplier = old_damage return trueend)DRAGONCLAW.rmb = trueAddStategraphActionHandler("wilson", GLOBAL.ActionHandler(GLOBAL.ACTIONS.DRAGONCLAW, function(inst, action) inst.sg.mem.localchainattack = not action.forced or nil if not inst.components.health:IsDead() and not inst.sg:HasStateTag("attack") then return "attack" end end))which works and causes the action to occur however when playing as the client the action happens but no animation occurs. How do I push the animation client side? Edited May 21, 2015 by gregdwilson Link to comment https://forums.kleientertainment.com/forums/topic/54201-hud-custom-button/#findComment-639339 Share on other sites More sharing options...
DarkXero Posted May 21, 2015 Share Posted May 21, 2015 (edited) @gregdwilson, try making the client do ThePlayer:EnableMovementPrediction(false), or add the handler to the "wilson_client" stategraph. You may want to switch the inst.components.health:IsDead() into inst.replica.health:IsDead(), since clients don't have all the components. It's odd, you can be a client and get the yawn state, for example, despite it being invalid for clients, only valid in the normal stategraph. Not sure what is happening here. Edited May 21, 2015 by DarkXero Link to comment https://forums.kleientertainment.com/forums/topic/54201-hud-custom-button/#findComment-639347 Share on other sites More sharing options...
gregdwilson Posted May 21, 2015 Author Share Posted May 21, 2015 @DarkXero, I duplicated the stategraph for wilson_client but switched to inst.replica.health and it worked like a charm. Thank you again. Link to comment https://forums.kleientertainment.com/forums/topic/54201-hud-custom-button/#findComment-639355 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