Jump to content

Recommended Posts

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)				end

and in my character config I added a listener under the master_postinit

inst:ListenForEvent("DragonClawClick", DragonClawClick)

and then a simple function

local function DragonClawClick(inst)    inst.components.talker:Say("You Clicked Me!")end

The "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

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?

 

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.

@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 by

local 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 by gregdwilson

@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 by DarkXero

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...