Key handling and rawset

All about writing scripts for Celestia in Lua and the .cel system
Topic author
BillC
Posts: 19
Joined: 09.07.2003
With us: 21 years 4 months
Location: Woonsocket RI

Key handling and rawset

Post #1by BillC » 13.10.2008, 20:04

I was experimenting with key handling (see http://www.shatters.net/forum/viewtopic.php?f=6&t=12653&st=0&sk=t&sd=a&start=45#p109115) and I wanted to post something over here on this forum.

Following Chris's example (http://www.shatters.net/forum/viewtopic.php?f=9&t=9813&start=0&st=0&sk=t&sd=a&hilit=key+handler), let's say you have a collection of key assignments contained in a table called "keyhandlers":

Code: Select all

keyhandlers =
{
  G = fast_goto,
  ["1"] = toggle_ecliptic_grid,
  etc,
  etc
}

and a function that handles the key input:

Code: Select all

function handlekey(k)
  handler = keyhandlers[k.char]
  if (handler ~= nil) then
    handler()
    return true
  else
    return false
  end
end

and a command that ties the "key" event to the "handlekey" function:

Code: Select all

celestia:registereventhandler("key", handlekey)

Now if you've already run this script (in start.celx, say), is there a way to add another key assignment to the table, or add another table to the function, by running another script?

Adding another key assignment is pretty simple - just run a script with this command:

Code: Select all

rawset(keyhandlers, "A", toggle_plates)

(Of course, if "A" was assigned to a different function originally, this command will reassign it.)

You can also have the script look for the existence of the "keyhandlers" table, and if it doesn't find it, create it:

Code: Select all

if (keyhandlers == nil) then
  keyhandlers = { ["A"] = toggle_plates }
  celestia:registereventhandler("key", handlekey)
else
  rawset(keyhandlers, "A", toggle_plates)
end

But to make it work, you will have to add the "handlekey" function to the script as well:

Code: Select all

function handlekey(k)
  handler = keyhandlers[k.char]
  if (handler ~= nil) then
    handler()
    return true
  else
    return false
  end
end

This will overwrite any previous declaration of "handlekey", which won't be a problem if it's identical.

Now let's say your script creates a new table called "keyhandlers2". Can you include this table in the "handlekey" function along with the original key handling table?

Use this variation of the key handling function:

Code: Select all

function handlekey(k)
  handler = keyhandlers[k.char]
  handler2 = keyhandlers2[k.char]
  if (handler ~= nil) then
    handler()
    return true
  elseif (handler2 ~= nil) then
    handler2()
   return true
  else
    return false
  end
end

When you run your script, the old function "handlekey" will be overwritten with this version, but the original "keyhandlers" table will still work.
BillC

Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Re: Key handling and rawset

Post #2by Vincent » 13.10.2008, 20:55

BillC wrote:Now if you've already run this script (in start.celx, say), is there a way to add another key assignment to the table, or add another table to the function, by running another script?
Bill,

You should be able to do this by adding this single line:

Code: Select all

keyhandlers["A"] = toggle_plates
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

Topic author
BillC
Posts: 19
Joined: 09.07.2003
With us: 21 years 4 months
Location: Woonsocket RI

Re: Key handling and rawset

Post #3by BillC » 15.10.2008, 01:08

Thanks Vincent!

Ha ha, I missed the obvious!

I've also used this format:

Code: Select all

keyhandlers = { ["A"] = toggle_plates }

But I think yours is more elegant.
BillC

Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Re: Key handling and rawset

Post #4by Vincent » 15.10.2008, 11:50

BillC wrote:Thanks Vincent!
You're welcome, Bob.

BillC wrote:I've also used this format:

Code: Select all

keyhandlers = { ["A"] = toggle_plates }

But I think yours is more elegant.
It's not only a question of elegance. The problem with your code is that it defines
a fresh new keyhandlers table, and thus, it deletes the previous keys assignments.
Whereas my code simply adds the new key assignment on top of the existing ones.
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

BobHegwood
Posts: 1803
Joined: 12.10.2007
With us: 17 years 1 month

Re: Key handling and rawset

Post #5by BobHegwood » 15.10.2008, 14:58

Vincent wrote:You're welcome, Bob.

Methinks you meant to say "Bill."
Brain-Dead Geezer Bob is now using...
Windows Vista Home Premium, 64-bit on a
Gateway Pentium Dual-Core CPU E5200, 2.5GHz
7 GB RAM, 500 GB hard disk, Nvidia GeForce 7100
Nvidia nForce 630i, 1680x1050 screen, Latest SVN

Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Re: Key handling and rawset

Post #6by Vincent » 15.10.2008, 16:15

BobHegwood wrote:
Vincent wrote:You're welcome, Bob.
Methinks you meant to say "Bill."
Oops, sorry Bill.
And thanks... Bob!
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3


Return to “Scripting”