Ttiki Posted March 12, 2021 Share Posted March 12, 2021 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. rotbuttondemo.mp4 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. Quote Just making games for fun. Currently working on T3-L4 Link to comment Share on other sites More sharing options...
GubbyBlips Posted March 12, 2021 Share Posted March 12, 2021 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 Quote Link to comment Share on other sites More sharing options...
GubbyBlips Posted March 12, 2021 Share Posted March 12, 2021 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. Quote Link to comment Share on other sites More sharing options...
Josh Posted March 13, 2021 Share Posted March 13, 2021 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. Quote 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 More sharing options...
Ttiki Posted March 13, 2021 Author Share Posted March 13, 2021 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. Quote Just making games for fun. Currently working on T3-L4 Link to comment Share on other sites More sharing options...
GorzenDev Posted March 30, 2021 Share Posted March 30, 2021 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 2 Quote Link to comment Share on other sites More sharing options...
Ttiki Posted March 30, 2021 Author Share Posted March 30, 2021 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.? 1 Quote Just making games for fun. Currently working on T3-L4 Link to comment Share on other sites More sharing options...
Recommended Posts
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.