From The Depths - Forum
Detect key pressed - Printable Version

+- From The Depths - Forum (
+-- Forum: Alpha (/forumdisplay.php?fid=9)
+--- Forum: LUA Scripting (/forumdisplay.php?fid=129)
+--- Thread: Detect key pressed (/showthread.php?tid=30137)

Detect key pressed - jcordeiro - 2017-09-11 06:17 PM

My Lua scripts have evolved well, but now i find my self needing to give user input to them. Here are some examples.

Key to detonate already launched missiles if target is standing down (to allow capture).
Keys to change desired altitude on my air ship. I have a script that controls all my thrusters and maintains altitude relative to water or terrain, i want a key to change that value.

So, how do i detect that a key is pressed?
Please only LUA related solutions.

RE: Detect key pressed - ZerothAngel - 2017-09-12 05:27 AM

I haven't dug into 2.0 much yet, but I don't believe there's a direct way to get keypresses. However, there are a number of roundabout ways to go about creating a "switch" you can trigger with a key. The component API has access to a number of devices, which Lua can both read from/write to: balloons, pumps, tractor beams, etc. Look at the Component_* methods, specifically.

Set up a complex controller, bind keys to the device, read the state from Lua. Since you access most things in Lua via their index, distinguishing between which ones are your control and which ones aren't is left as an exercise. Wink (I typically use the direction that the block is facing...)

As for manual altitude control, I think the best option is the drive maintainer. Because:
  1. It's analog and lets you go smoothly from -1 to 1
  2. You can reset it to 0 by hitting your increase/decrease keys simultaneously (just like throttle on water craft)
  3. You can display the current value on the HUD by enabling it in options (primary, secondary, tertiary drive).
Pretty much all of my aircraft & submarine scripts allow optional manual control via drive maintainer. It's really nice.