Flying cooldown aint working (SOLVED)

So I have tried making 2 cooldowns for a character 1 for cooldown activation of the flight and 2 to send him back right to the ground, what it does it ignores the astart(activation cooldown) and astop(to put the character on the ground) here the code

local gravity = -20
local jump_takeoff_speed = 900

local astart = 4
local astop = 10

local function flight(self)
	if self.action == true then
		if now > (self.astart + astart) then
			gravity = 0
			go.animate(".", "position.y", go.PLAYBACK_ONCE_FORWARD, 399.1721, go.EASING_LINEAR, 1)
			if now > (self.astop + astop) then
				gravity = -20
				self.action = false
			end
		end
	end
end

function init(self)
	msg.post(".", "acquire_input_focus")
	self.position = go.get_position()
	local orgp = go.get_position()
	self.lives = go.get("#", "lives")
	self.astart = 0
	self.astop = 0
	msg.post("#", "reset")
end

function update(self, dt)
	local gravity = vmath.vector3(0, gravity, 0)
	self.position = go.get_position()

	if not self.ground_contact then
		self.velocity = self.velocity + gravity
	end

	go.set_position(go.get_position() + self.velocity * dt)


	self.correction = vmath.vector3()
	self.ground_contact = false	

	flight(self)
end

function on_input(self, action_id, action)
	if action_id == hash("jump")then
		jump(self)
	elseif action_id == hash("action") then
		now = socket.gettime()
		self.action = true
	end
end

My post has been edited?

Yes, It was moved to the questions category.

2 Likes

A very easy way of dealing with cooldowns is to use our native timer functionality. Manually tracking timers or countdowns is not very efficient.

1 Like

Why does this delay the input by only like 5 seconds? Btw can you give me an idea of a better input delay? because this one is too messy

function on_input(self, action_id, action)
	self.timer = false
	if action_id == hash("jump")then
		jump(self)
	elseif action_id == hash("action") then
		if self.timer == false then
			self.action = true
			self.timer = true
		elseif self.timer == true then
			timer.delay(10, false, function(self, id)
				self.action = true
				self.timer = false
			end)
		end
	end
end

Ok so I have removed the delay from the input but it still delaying the output

elseif action_id == hash("action") then
		self.action = true
	end

local function flight(self)
	if self.action == true then
		gravity = 0
		go.animate(".", "position.y", go.PLAYBACK_ONCE_FORWARD, 399.1721, go.EASING_LINEAR, 1)
	timer.delay(4, false, function(self,id) 
		gravity = -20
		self.action = false
	end)
	end
end

(Update)Ok so it looks like it delaying the flight to 4 seconds like for enabling the gravity, but why?

Try (from the top of my head):

function on_input(self, action_id, action)
	if action_id == hash("jump") then
		jump(self)
	elseif action_id == hash("action") and not self.action_cooldown then
		self.action = true
		self.action_cooldown = true
		timer.delay(10, false, function(self, id)
			self.action_cooldown = false
		end)
	end
end
2 Likes