Celestia/Celx Scripting/Q&A

From Wikibooks, open books for an open world
Jump to: navigation, search

Introduction[edit]

This page is intended to be a repository of practical questions and answers (Q&A) on the subject of CELX Scripting. There are two ways to get something listed here:

- Just list your general questions in the Q&A section below, and someone will post an answer to them. Please post discussions about a specific answer (e.g. requests for clarification) to that answer's discussion page. Unfortunately, however, few, if any, CELX programming experts peruse this WikiBook on a regular basis.

or (preferred)

- Post your question on the Celestia Scripting Forum. Once the answer is determined to your satisfaction there, please transcribe the question and summarise the final answer here so that we can build up a knowledge base in this WikiBook.

Q&A[edit]

How do I calculate the angle between two objects?[edit]

Answer

How do I assign a CELX function to a particular key on the keyboard?[edit]

Answer


The questions & answers below have been moved here from one of the "discussion" pages. Those pages are for discussing issues related to editing WikiBook pages, not for providing content for a WikiBook.

If there's something that isn't clear, ask about it here.

Just click on the "edit this page" button immediately above and start typing. Or click on one of the [edit] buttons in this page and insert a new question or answer at its bottom.


Test Scripts and Questions[edit]

Using the very first, very simple CELX example script here, to wit:

celestia:flash("Hello, Universe!")
wait(2.0)

The "flash" function is defined within the object "Celestia." When this script is executed, I expected it to do nothing more than display the text "Hello Universe!"

Prior to executing this script, I had centered the Earth in my Celestia window, and I was in the "Follow" mode. When I execute this script, the viewpoint changes so that the Earth gets moved to one side or the other prior to seeing the "Hello Universe!" message.

Am I correct in thinking that this movement is caused by Celestia's definition of the "flash" function? In other words, the "flash" function in Celestia itself probably includes some movement function to be executed. Yes?

No. I don't see the earth move when I try this. How are you running the script?



After investigating the properties of the "flash" and "print" functions in Celestia, I tried the following script to center the message on my display. This also causes movement of the Earth, even though I thought I centered everything on the screen since I'm using 0 (zero) in the parameters list. Can anyone explain what's going on here?

celestia:print("Hello Universe!", 5, 0, 0, 0, 0)
wait (5)

I don't see this either. May be same problem as above.
Mystery explained... Open Scripts dialog and I double-clicked on the celx script in order to open it. If I simply use the dialogs provided by Celestia to open the script, the movement does not occur. Thanks.
In other words, do NOT double-click on the file name. Simply click on the OPEN dialog box.
One other note... If I do this using a CEL script, there is no apparent effect on the CEL script.

How do I execute celx scripts on Debian GNU/Linux (celestia 1.6.1)? I attempt with $ celestia --url test.celx and I greeted with invalid option -- '-'

Celestia on linux currently does not support the --url parameter. Celx scripts can be executed with the parameter "-f /path/to/script.celx". Note the script path must be the fully explicit path and not the relative path. See debian bug #682258

Test script from the BrainDead[edit]

  flashRadius = function (bodyName)
  body = celestia:find(bodyName) -- finds object 
  radius = body:radius() -- gets radius
  celestia:flash("The radius of " .. body:name() .. " is " .. radius .. " km.")
  wait(2.0)
  end
     
  flashRadius("Mercury")
  flashRadius("Venus")
  flashRadius("Earth")
  flashRadius("Mars")
  flashRadius("Jupiter")
  flashRadius("Saturn")
  flashRadius("Uranus")
  flashRadius("Neptune")
  flashRadius("Pluto")
  flashRadius("Charon")

  function flash(message)
  celestia:flash(message)
  wait(2.0)
  end

  message = "End of this test script"
  flash = celestia:flash(message,5)
Important point in the above... The variable "message" must be defined prior
to execution of the "flash" function or the script causes an error which says
that it expected a string. This makes sense. Still learning here.
I'm really confused by this though. How is it that this script shows the
text "End of this test script" if the flash function has been defined,
but it has NOT been called? In other words, why don't I need a line
which calls the function? i.e. - flash(message)
You didn't call "flash", but you did call "celestia:flash". The statement:
  flash = celestia:flash(message,5)

calls the "flash" method of the "celestia" object (that's what the right side of the assignment says to do) and then assigns the result (nil) to the variable "flash", redefining it. Probably not what you wanted. You only do an assignment to the "flash" variable when you define the function. Remember that:
  function flash(message)
  celestia:flash(message)
  wait(2.0)
  end

is equivalent to:
flash =  function (message)
  celestia:flash(message)
  wait(2.0)
  end

The function name "flash" is just another variable. HankR 17:59, 18 October 2005 (UTC)
Okay, I think I understand what you mean. Appreciate the help.--BrainDead 22:45, 18 October 2005 (UTC)

Another test script[edit]

  camera = celestia:getobserver()
  tethys = celestia:find("Sol/Saturn/Tethys") -- finds object named Tethys.
  celestia:select(tethys) 
  camera:center(tethys,5)
  wait(5)
  camera:follow(tethys)
  cameraPosition = camera:getposition() -- gets position of observer.
  tethysPosition = tethys:getposition() -- gets position of Tethys.
  distance = tethysPosition:distanceto(cameraPosition) -- gets distance from position of Tethys to observer.
  celestia:flash("Current distance to Tethys is "..distance.. " km",5)
  wait(5)
  celestia:flash("Let's have a look...",5)
  wait(5)
  obs=celestia:getobserver()
  obs:gotodistance(tethys,10000,5)
  wait(5)
  celestia:flash("Here it is!",5)
  wait(5)
  newPosition = obs:getposition() -- gets NEW position of the observer.
  newdistance = tethysPosition:distanceto(newPosition) -- gets distance from position of Tethys to new observer position.
  celestia:flash("Current distance to Tethys is "..newdistance.. " km",5)
  wait(5)
Okay, this works, but I have questions again... First, thanks to Malenfant for the idea here.
How do I round the values presented by tethysPosition and newdistance?
Use "Current distance to Tethys is "..string.format("%i",newdistance).." km"
Why does the newdistance value not show either 10000km or the value shown on Celestia's display?
Celestia's display shows the distance to the surface, you calculated the distance center to center. Plus, Tethys has moved since the time when you got its position. You calculated the distance to its old position, not its current position. You need to get the position again. Anytime you call wait(), time will pass and things will move. HankR 03:31, 24 October 2005 (UTC)
I'm still trying here. --BrainDead
Thanks again for the help, Hank. I understand this one now.--BrainDead 10:46, 24 October 2005 (UTC)

Corrected Script[edit]

  myobject = celestia:find("Sol/Saturn/Tethys") -- finds object named Tethys.
  celestia:select(myobject) 
  celestia:center(myobject,5)
  wait(5)
  celestia:follow(myobject)
  camera = celestia:getobserver()
  cameraPosition = camera:getposition() -- gets position of observer.
  tethysPosition = myobject:getposition() -- gets position of Tethys.
  distance = tethysPosition:distanceto(cameraPosition) -- gets distance from  position of Tethys to observer.
  celestia:flash("Current distance to Tethys is "..string.format("%i",distance).." km",5)
  wait(5)
  celestia:flash("Let's have a look...",5)
  wait(5)
  celestia:gotodistance(tethys,10000,5)
  wait(5)
  celestia:flash("Here it is!",5)
  wait(5)
  obs=celestia:getobserver()
  newPosition = obs:getposition() -- gets NEW position of the observer.
  newdistance = tethysPosition:distanceto(newPosition) -- gets distance from position of Tethys to   new observer position.
  celestia:flash("Current distance to Tethys is "..string.format("%i",newdistance).." km",5)
  wait(5)
This revision of the script corrects the errors I had earlier, and it reflects an integer value.
Can I ask though, HOW you knew about the integer correction? I can find that nowhere in any of
the documentation I have.
It's in the Lua 5.0 reference manual.
Where did you find "..string.format("%i").." and can I display, say two decimal places? Perhaps
using something like "..string.format("%2")???? --BrainDead 00:33, 25 October 2005 (UTC)
string.format uses a C-style ouput format control string. Use "%.2f" for two decimal places.
PS - Am I okay with the format here? Should I NOT post so much script? Should I use different
headers? Sorry, just me... Confused ol' Bob
It might be helpful if this material were reformatted as a FAQ page for CELX. Put each question in a separate section with the question as the section title. This would make it more useful for others.HankR 01:05, 25 October 2005 (UTC)