[SOLVED] Rate of fire problem

Hello!

I just started scripting on Defold and I ran into a problem with bullets, I can’t figure out how to make it so the gun will fire one bullet per click(even when the button is held down). I searched in the forums and documentation but I didn’t find anything that could help me.

Here is a gif of said problem:
https://gyazo.com/b92efe33f7bd676a437895e0eb287c45

This is the script for the mouse input

function update(self, dt)
if click == true then
msg.post(“/bulletplayer#script”,“shoot”)
click = false
end
end

function on_input(self, action_id, action)

if action_id == hash(“mouseleft”) then

if gui.pick_node(gui.get_node(“HUD2”), action.x, action.y) and action.released then
print(“node”)
else

if click == false then
click = true
print(“aaa”)
end
end

end
– Add input-handling code here
– Remove this function if not needed
end

And this is the factory script:

function on_message(self, message_id, message, sender)

if message_id == hash("shoot") and debounce == false then


 dir = 1

if go.get_rotation("char") == vmath.vector3(0,180,0) then
  dir = -1


end

local plrpos = go.get_position("char")

b = factory.create(“#factory”,vmath.vector3(plrpos.x,plrpos.y,-0.5), nil, {dmg = 15},vmath.vector3(4,4,40))

end

end

I hope that I gave you enough information to understand my problem, and thank you in advance!

Your on_input function is continuously being called while the mouse button is being held down and so setting click to true each time. Try replacing your on_input function with the following and you should get 1 bullet per click:

function on_input(self, action_id, action)
    if action_id == hash("mouseleft") and action.pressed then
        if gui.pick_node(gui.get_node("HUD2"), action.x, action.y) then
            click = true
        end
    end
end
3 Likes

Thank you for your reply, the code you gave me didn’t solve the problem, but it gave an idea of how to solve it.

For anyone who might have this problem in the future this is how I solved it (you also need to lower the repeat_delay in the game project settings):

if not action.repeated then
click = true
end

Thanks for the feedback :smiley:

2 Likes