Jump to content

Button which needs to stay pressed


Ttiki
 Share

Recommended Posts

Hello everyone.

 

After some frustrations, I've decided to give up and open a post here in hope I could get some help. 

I would like to make a button which needs to stay pressed for x sec / ms before calling its output. I don't know what my problem is, the script in itself is not complicated, but it seems I can't make it work...

If you don't understand what I mean I made a little video which I hope will help you see the goal.

Another video of this kind of action can be found in this video at around 57 sec:  

 

I don't really care for the rotation of the button at the moment. I know the math and could easily implement it later. What I'm really interested in is the delay before the output and the detection of the player pressing the button. I've tried using the default button script and the trigger delay script but with no success. I could give you a scrap of code I made if it can help.

Script.cleaningtime = 5000 --int "Cleaning time (ms)"

function Script:Start()	
	self.enable = true
	cleantimeleft=0
end

function Script:Use()
	cleantimeleft = Time:GetCurrent() + self.cleaningtime
	while(window:KeyDown(Key.E))do --while the player is holding its use key down		
		if Time:GetCurrent()>cleantimeleft and self.enable then
			System:Print("Clean!")
			self.component:CallOutputs("Used")
		end
	end
end

 

I don't know what I'm missing out.

Whenever I try this the game freezes until the player release the use key... 

 

Anyway, thank you very much for the help.

 

Just making games for fun. Currently working on T3-L4

Linka logo

Link to comment
Share on other sites

Always try to avoid using while. Of course it's stuck in there. Why not an if?

Start: activated = 0

if NOT pressed[e] then start = 0

else

start = 1

holdtime = currenttime +5000

end

if key pressed[e] and start = 1 then

print"holding for "..time/1000.." seconds"

if currenttime = holdtime then activated = 1

end

 

 

Link to comment
Share on other sites

BTW, obviously translate that very rudimentary script over to leadwerks--

I've come over from GameGuru, and also, that print statement has issues...

make it (holdtime - currenttime) / 1000

otherwise, let me know how it goes. I think it'd be a few weeks of study

before I could really get the jif of leadwerks syntax and methods. But I'd like to try.

It seems there's more here than there.

Link to comment
Share on other sites

This could actually be a good case for using a coroutine. Or just have a variable where the currently pressed button object is stored, and update it every frame in the UpdateWorld or UpdatePhysics function.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

9 hours ago, GubbyBlips said:

Always try to avoid using while. Of course it's stuck in there. Why not an if?

Start: activated = 0

if NOT pressed[e] then start = 0

else

start = 1

holdtime = currenttime +5000

end

if key pressed[e] and start = 1 then

print"holding for "..time/1000.." seconds"

if currenttime = holdtime then activated = 1

end

 

 

Yes, my use of while wasn't really a smart move from my part, but I began to be really desperate. 

9 hours ago, GubbyBlips said:

I've come over from GameGuru, and also, that print statement has issues...

make it (holdtime - currenttime) / 1000

 

And yeah, I didn't check the math for the print ?

 

 

2 hours ago, Josh said:

This could actually be a good case for using a coroutine. Or just have a variable where the currently pressed button object is stored, and update it every frame in the UpdateWorld or UpdatePhysics function.

That sound like a nice way to do this. I will search onto all of this today, hopefully I will understand where I went really wrong.

 

Thank you very much. 

Just making games for fun. Currently working on T3-L4

Linka logo

Link to comment
Share on other sites

  • 3 weeks later...

it has been a while for me but you'd probly do something like this: (untested)
 

Script.cleaningtime = 5000 --int "Cleaning time (ms)"

function Script:Start()	
	self.enable = true
  	self.startcleaning = false
  	self.holding = false
  	self.cleantimestart = 0
end

function Script:Use()
	self.startcleaning = true
end

function Script:UpdateWorld()
	if (window:KeyDown(Key.E) and self.startcleaning) then --while the player is holding its use key down	
		if self.holding == false then
			self.cleantimestart = Time:GetCurrent()
			self.holding = true
		end
		local timepast = Time:GetCurrent() - self.cleantimestart
		if timepast >= self.cleaningtime then
			--reset
			self.startcleaning = false
			self.holding = false
			--send signal
			System:Print("Clean!")
			self.component:CallOutputs("Used")
		end
	else
		self.startcleaning = false --precaution
		self.holding = false
	end
end

 

  • Thanks 2
Link to comment
Share on other sites

1 hour ago, GorzenDev said:

it has been a while for me but you'd probly do something like this: (untested)
 


Script.cleaningtime = 5000 --int "Cleaning time (ms)"

function Script:Start()	
	self.enable = true
  	self.startcleaning = false
  	self.holding = false
  	self.cleantimestart = 0
end

function Script:Use()
	self.startcleaning = true
end

function Script:UpdateWorld()
	if (window:KeyDown(Key.E) and self.startcleaning) then --while the player is holding its use key down	
		if self.holding == false then
			self.cleantimestart = Time:GetCurrent()
			self.holding = true
		end
		local timepast = Time:GetCurrent() - self.cleantimestart
		if timepast >= self.cleaningtime then
			--reset
			self.startcleaning = false
			self.holding = false
			--send signal
			System:Print("Clean!")
			self.component:CallOutputs("Used")
		end
	else
		self.startcleaning = false --precaution
		self.holding = false
	end
end

 

I've written a script like this one, and it works great. I forget to post my solution here. I just need to find a way to implement a kind of progress bar now(like the Left 4 Dead video example above). Sadly I've been crushed by work lately, so I wasn't able to experiment with my script.?

  • Like 1

Just making games for fun. Currently working on T3-L4

Linka logo

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...