Lua/Celx Pause-A-Script function

All about writing scripts for Celestia in Lua and the .cel system
Topic author
don
Posts: 1709
Joined: 12.07.2003
With us: 20 years 10 months
Location: Colorado, USA (7000 ft)

Lua/Celx Pause-A-Script function

Post #1by don » 05.03.2004, 10:01

This function pauses the script and waits for the user to press the spacebar (includes an example). Enjoy!

This file is available on my Celestia web page (http://www.donandcarla.com/Celestia/).

-Don G.

Code: Select all

--***************************************************************************
--                 Don G's Celx/Lua Pause-a-Script function                 *
--                              (version 1.0)                               *
--                                                                          *
-- This function pauses the script and waits for the user to press the      *
-- spacebar.                                                                *
--                                                                          *
--***************************************************************************


--***************************************************************************
--                         Keyboard Input Callback                          *
--                                                                          *
-- This function is called automatically by Celestia when celestia:         *
-- requestkeyboard() is set to true.                                        *
--                                                                          *
--***************************************************************************
  function celestia_keyboard_callback(keypress)
    rtnKeypress = keypress
    return true  -- Tell Celestia we will handle this keypress
  end
   

--***************************************************************************
--       Pause the script and wait for user to press the spacebar
--***************************************************************************
  function pause()
    origTimeScale = celestia:gettimescale()  -- Get the current time-scale
    celestia:settimescale(0)                 -- Pause time
    rtnKeypress = ""
    celestia:requestkeyboard(true)           -- Enable keyboard input
   
    while rtnKeypress ~= " " do  --Loop until we get the spacebar
      -- (Yes, the print duration time is longer than the wait duration time.
      -- this is to stop the prompt from "flickering".)
      celestia:print("Press the spacebar to continue ..." , 1, -1, -1, 1, 4)
      wait(0.5)
    end

    celestia:requestkeyboard(false)       -- Disable keyboard input
    celestia:settimescale(origTimeScale)  -- Reset the time scale
  end
 

--***************************************************************************
--                               Example
--***************************************************************************
  obs = celestia:getobserver()

  celestia:print("Going to Earth ..." , 5, -1, -1, 1, 4)
  myObject = celestia:find("Sol/Earth")
  celestia:select(myObject)
  obs:center(myObject)
  obs:follow(myObject)
  obs:gotodistance(myObject, (25000 + myObject:radius()), 5)
  wait(5)
 
  pause()  -- Pause the script here

  celestia:print("Going to Mars ..." , 5, -1, -1, 1, 4)
  myObject = celestia:find("Sol/Mars")
  celestia:select(myObject)
  obs:center(myObject)
  obs:follow(myObject)
  obs:gotodistance(myObject, (25000 + myObject:radius()), 5)
  wait(5)
 

--***************************************************************************
--                           End of script
--***************************************************************************
  celestia:print("This script is now finished.", 2, -1, -1, 1, 10)
  wait(2)
 

Bob Hegwood
Posts: 1048
Joined: 19.10.2003
With us: 20 years 7 months
Location: Germantown, Ohio - USA

Post #2by Bob Hegwood » 09.03.2004, 05:49

Hey Don,

Anyway I could use this function with a cel script? Can it be called somehow? Just curious. I know, I know... I'm supposed to be writing all of my scripts in celx now.

Fact is, I just don't like celx scripts. The only ones which have actually worked like they're supposed to on my machine are the scripts you've created. Every other script I've tried either screws up my graphics, or it doesn't do anything useful. :roll:

Thanks, Bob

PS - Before you go jumping on me Harald, I know your Nine-Planets script works okay, but it also causes my graphics to go Blooey!
Bob Hegwood
Windows XP-SP2, 256Meg 1024x768 Resolution
Intel Celeron 1400 MHz CPU
Intel 82815 Graphics Controller
OpenGL Version: 1.1.2 - Build 4.13.01.3196
Celestia 1.4.0 Pre6 FT1

Topic author
don
Posts: 1709
Joined: 12.07.2003
With us: 20 years 10 months
Location: Colorado, USA (7000 ft)

Post #3by don » 09.03.2004, 07:02

Bob Hegwood wrote:The only ones which have actually worked like they're supposed to on my machine are the scripts you've created.

But mine, so far, don't DO anything, which is probably why they work <laughing>. They are merely input functions that I have created while playing with the keyboard callback function.

Currently, it is not possible to call a celx script from a cel script. The ability of celx to run another celx script is built-into Lua <sigh>. However, in another thread, I did ask Harald if this would be possible. We'll see. There is always hope! :D

-Don G.

Harry
Posts: 559
Joined: 05.09.2003
With us: 20 years 8 months
Location: Germany

Post #4by Harry » 09.03.2004, 12:08

don wrote:Currently, it is not possible to call a celx script from a cel script.

This could be added, but is IMHO less useful than the other way around. One of the things that make Lua more powerful than CEL-scripting is this: the script can remember things.

If you would call a Lua-script from a CEL-script, you would lose the state of the Lua-script once it's finished. But if you call a CEL-script from Lua, you aren't losing anything after it's finished, because there was no state to lose.

To get back to using a "wait until keypress" problem. This could be solved like this:
- execute first part of CEL-script from Lua
- wait for keypress
- execute second part of CEL-script

Harald

Bob Hegwood
Posts: 1048
Joined: 19.10.2003
With us: 20 years 7 months
Location: Germantown, Ohio - USA

Post #5by Bob Hegwood » 09.03.2004, 17:35

If I may Harry...

Why is it so difficult to add a "detect space-bar and wait" functionality to cel?

To get back to using a "wait until keypress" problem. This could be solved like this:
- execute first part of CEL-script from Lua
- wait for keypress
- execute second part of CEL-script


This doesn't solve the problem. Your forcing wait-states into places where they are not wanted. In other words, the script is still proceeding to execute until you tell it to wait. What we need is a background routine which will detect when the spacebar - or whatever key you'd like to use - is pressed, whenever it is pressed. In other words, we need a way for the user to be able to pause the script. If the keypress is detected, the routine needs to go to a wait-state immediately. It should then wait until the key is pressed again before it resumes execution.

If one were to set up wait-states the way you've described them, there's no need for celx. You can do the same thing in cel can't you. Just execute a potion of the code, and then execute a wait? Same thing ain't it?

Take care, Bob
Last edited by Bob Hegwood on 09.03.2004, 18:42, edited 1 time in total.
Bob Hegwood

Windows XP-SP2, 256Meg 1024x768 Resolution

Intel Celeron 1400 MHz CPU

Intel 82815 Graphics Controller

OpenGL Version: 1.1.2 - Build 4.13.01.3196

Celestia 1.4.0 Pre6 FT1

Topic author
don
Posts: 1709
Joined: 12.07.2003
With us: 20 years 10 months
Location: Colorado, USA (7000 ft)

Post #6by don » 09.03.2004, 18:36

Bob and Harald,

Good news ... Chris just authorized my code change that adds a keystroke "Pause Script" function to Celestia. It will pause both Cel and Celx scripts.

My initial definition was to leave the spacebar as a "Pause Time" function, and add a new keystroke (Shift+spacebar) to Pause BOTH Time and Scripts.

Some have argued to reverse the functionality (spacebar = pause both).

Any comments here in the forum for either way?

I will wait to implement the code changes until I get some feedback

-Don G.

Topic author
don
Posts: 1709
Joined: 12.07.2003
With us: 20 years 10 months
Location: Colorado, USA (7000 ft)

Post #7by don » 09.03.2004, 18:46

Harry wrote:If you would call a Lua-script from a CEL-script, you would lose the state of the Lua-script once it's finished.

Not a problem. The idea would be to allow Cel scripters to utilize functions that celx gurus created. That is, functions that Start, Run, and Complete in a stand-alone fashion (no need to retain state).

However, it would be nice to be able to pass parameters to the celx script and receive a return code, and/or text message back from the celx script.

Yes, it is more obvious to call cel from celx, but I would also like to provide the means for less-experienced programmers to be able to use functions created by celx gurus, such as yourself :D, in the less-capable, but easier to learn, world of Cel scripting.

Cheers,

-Don G.

Bob Hegwood
Posts: 1048
Joined: 19.10.2003
With us: 20 years 7 months
Location: Germantown, Ohio - USA

Post #8by Bob Hegwood » 09.03.2004, 18:48

don wrote:Good news ... Chris just authorized my code change that adds a keystroke "Pause Script" function to Celestia. It will pause both Cel and Celx scripts.
Wait a minute...

Thanks, just had an orgasm. :lol:

My initial definition was to leave the spacebar as a "Pause Time" function, and add a new keystroke (Shift+spacebar) to Pause BOTH Time and Scripts.


Just my opinion, Don. Hit the space bar to stop/pause everything so you have only to hit the spacebar again in order to resume from where you left off. Why would you want to not pause the time also? That would just add more confusion that doesn't need to be there. Again, just my opinion.

Either way, it'd be a vast improvement over what we have now. :wink:

Thanks, Bob
Bob Hegwood

Windows XP-SP2, 256Meg 1024x768 Resolution

Intel Celeron 1400 MHz CPU

Intel 82815 Graphics Controller

OpenGL Version: 1.1.2 - Build 4.13.01.3196

Celestia 1.4.0 Pre6 FT1

Harry
Posts: 559
Joined: 05.09.2003
With us: 20 years 8 months
Location: Germany

Post #9by Harry » 09.03.2004, 19:54

Bob Hegwood wrote:[...]This doesn't solve the problem. Your forcing wait-states into places where they are not wanted. In other words, the script is still proceeding to execute until you tell it to wait. What we need is a background routine which will detect when the spacebar - or whatever key you'd like to use - is pressed, whenever it is pressed.
But this is just what Don's script does - pause where the scriptwriter tell's it to. And I assume this could come handy in a few cases, so I wanted to point out this simple application of a CEL/CELX-mix. I knew already of the discussion about a "stop everything" via the spacebar, so I didn't think about THAT anymore.

BTW, Don: spacebar stopping everything sounds good, IMHO that makes most sense.
If one were to set up wait-states the way you've described them, there's no need for celx. You can do the same thing in cel can't you. Just execute a potion of the code, and then execute a wait? Same thing ain't it?

If you mean it could be done with Lua available from CEL - yes. But it can't be done in CEL alone (wait 5s, but not wait for key), that's what I wanted to point out. Of course a wait-for-keypress command could be added, but that would just be a duplication of the work already done.

Harald

Harry
Posts: 559
Joined: 05.09.2003
With us: 20 years 8 months
Location: Germany

Post #10by Harry » 09.03.2004, 21:03

don wrote:Not a problem. The idea would be to allow Cel scripters to utilize functions that celx gurus created. That is, functions that Start, Run, and Complete in a stand-alone fashion (no need to retain state).
I just wanted to explain why IMHO it makes more sense to do include CEL to CELX. And you really don't have to understand much Lua to use this, even the little code which is necessary can be put in a function, so you would simply use:

Code: Select all

CEL([[
{
   Enter CEL-code here
}
]])

However, it would be nice to be able to pass parameters to the celx script and receive a return code, and/or text message back from the celx script.
Ok, but what could you do with a return code, or a text message? Nothing. Any parameter would need to be fixed, so you could as well simply write it directly into the CELX-source.
Yes, it is more obvious to call cel from celx, but I would also like to provide the means for less-experienced programmers to be able to use functions created by celx gurus, such as yourself :D, in the less-capable, but easier to learn, world of Cel scripting.

If you look at the code above, don't you think this is easy enough? :D Just copy and paste some mysterious "CEL-function" at the top of your script, enclose all your CEL-statements within such blocks, and everything should work fine :) Of course we will have to wait and see if there are any problems in practice.
But I have to repeat this: there are many possible issues with doing such a mixed script, and for example there is no good way to actually use something like MultiViews from within CEL :(

Harald

maxim
Posts: 1036
Joined: 13.11.2003
With us: 20 years 6 months
Location: N?rnberg, Germany

Post #11by maxim » 09.03.2004, 22:19

I don't now if things are clear?
As I understand Bob, he want's a user controlled stop of the script.
Press pause - receive call/open door/goto toilet - press resume.

Another thing I would find (very) usefull is the posibillity to open an http-URL via script. That one that is stored with an objects info-menupoint or any other one. Is this hard to implement?

maxim :)

Harry
Posts: 559
Joined: 05.09.2003
With us: 20 years 8 months
Location: Germany

Post #12by Harry » 09.03.2004, 22:53

maxim wrote:I don't now if things are clear?
As I understand Bob, he want's a user controlled stop of the script.
Press pause - receive call/open door/goto toilet - press resume.
Clear to me :) I proposed a possible solution how a scriptwriter can pause a script until keypress (that's what don's script does), and the change don has written about (changing the behaviour of the spacebar) is what Bob apparently wants ;)
Another thing I would find (very) usefull is the posibillity to open an http-URL via script. That one that is stored with an objects info-menupoint or any other one. Is this hard to implement?

This is very UI-specific, which makes things much more complicated...

Harald

maxim
Posts: 1036
Joined: 13.11.2003
With us: 20 years 6 months
Location: N?rnberg, Germany

Post #13by maxim » 09.03.2004, 23:01

Harry wrote:This is very UI-specific, which makes things much more complicated...


You are saying that how Windows handles the Right-Mouse-Button-Menu -> Info is different code as how Linux does? No common wrapper function?

maxim :?:

Bob Hegwood
Posts: 1048
Joined: 19.10.2003
With us: 20 years 7 months
Location: Germantown, Ohio - USA

Post #14by Bob Hegwood » 10.03.2004, 06:39

Harry wrote:I proposed a possible solution how a scriptwriter can pause a script until keypress (that's what don's script does), and the change don has written about (changing the behaviour of the spacebar) is what Bob apparently wants ;)
I thought that this was the same thing you wanted too.
BTW, Don: spacebar stopping everything sounds good, IMHO that makes most sense.

This isn't you? :?:

Take care, Bob

Harry
Posts: 559
Joined: 05.09.2003
With us: 20 years 8 months
Location: Germany

Post #15by Harry » 10.03.2004, 06:46

maxim wrote:You are saying that how Windows handles the Right-Mouse-Button-Menu -> Info is different code as how Linux does? No common wrapper function?

Everything which has to do with actually showing the context menu (or any menu) on screen is UI-specific...

Harald

Bob Hegwood
Posts: 1048
Joined: 19.10.2003
With us: 20 years 7 months
Location: Germantown, Ohio - USA

Post #16by Bob Hegwood » 10.03.2004, 06:49

Don, Harry...

Consider the following also posted in this forum...

Raud, Don, Bob --
Teachers using Celestia scripts in class need "external" pausing of the script -- despite one's best efforts at timing, with wait commands or duration of orbiting, etc., questions will come up, and it's always best to answer them at once. Can't do it now (that I'm aware of), so this feature would be very helpful.
Parker


This is where I'm coming from. :wink:

Take care, Bob
Bob Hegwood

Windows XP-SP2, 256Meg 1024x768 Resolution

Intel Celeron 1400 MHz CPU

Intel 82815 Graphics Controller

OpenGL Version: 1.1.2 - Build 4.13.01.3196

Celestia 1.4.0 Pre6 FT1

Harry
Posts: 559
Joined: 05.09.2003
With us: 20 years 8 months
Location: Germany

Post #17by Harry » 10.03.2004, 07:07

Bob Hegwood wrote:
Harry wrote:I proposed a possible solution how a scriptwriter can pause a script until keypress (that's what don's script does), and the change don has written about (changing the behaviour of the spacebar) is what Bob apparently wants ;)
I thought that this was the same thing you wanted too.

I want both, the new spacebar AND a scripted pause. And more. :D

I was just confused you were interested in using Don's script/function, but then you suddenly weren't happy anymore when I offered a way how to use it (in future). But I guess it's just too easy to miss the point of somebody else's post, written conversation is simply much more ambiguous than face-to-face conversation :P

Harald

Topic author
don
Posts: 1709
Joined: 12.07.2003
With us: 20 years 10 months
Location: Colorado, USA (7000 ft)

Post #18by don » 10.03.2004, 07:52

Harry wrote:I just wanted to explain why IMHO it makes more sense to do include CEL to CELX.
(Also see my reply in the other thread.)

Calling a celx script from cel would allow cel scripters to use scripts written by celx/Lua programmers, without having to learn Lua/celx. The celx scripts would be self-contained, from start to finish (moving in some manner, or whatever celx does better than cel, or things cel doesn't do at all). Remember, not everyone is capable of learning celx -- whereas cel is quite easy to learn in comparison.


Harry wrote:Ok, but what could you do with a return code, or a text message? Nothing.
Yep, you're right. No vars in cel. Sorry! :oops:


Harry wrote:If you look at the code above, don't you think this is easy enough?
Yes it is, but only when you manually copy existing cel script code, or write new cel code, into a new celx script. What I would really like to do, in this particular direction (run cel from celx), is use my Run-A-Script celx script, to run existing, external cel scripts (from the disk), like it is currently able to run external celx scripts.

The opposite direction (running a celx script from cel) is because (see first paragraph), plus ... Celx has more functionality than cel. Not everyone can learn celx. It would be nice to allow Cel script-writers to use celx scripts (individual functions) written by celx OO programmers.


Harry wrote:But I have to repeat this: there are many possible issues with doing such a mixed script,
Issues that would need to be coded for, in the celx.cpp code I presume. Along with making several decisions, such as running only one script, etc.


Harry wrote:... and for example there is no good way to actually use something like MultiViews from within CEL

Not from within cel directly ... but from within a cel script that runs a celx script. For a moment, think "library" here.

For example, you write a celx script that takes three parameters (strings) that define three object names. The cel script sends these three object names TO the celx script. The celx script gets them, sets-up a MultiView display, does some rotations (or whatever), displays some text, pauses for a few seconds, then ends ... returning control to the cel script.

This celx script could be built into a library of MANY "generic" functions that can be used by all cel script writers, without ever having to learn celx. Am I making any sense yet? :D

Cheers,

-Don G.

Topic author
don
Posts: 1709
Joined: 12.07.2003
With us: 20 years 10 months
Location: Colorado, USA (7000 ft)

Post #19by don » 10.03.2004, 08:00

Just to clarify ...

I wrote the celx Pause-A-Script function before Chris authorized the addition of the Celestia source code changes that allow a Celestia key to pause a script.

You already have the celx script version, and soon you will have the Celestia built-in version too. :D

-Don G.


Return to “Scripting”