Page 1 of 1

[solved] luahookscript doesn't work

Posted: 19.02.2009, 12:00
by Stuffer
hey,

I tried to create my own luahook script by the help of hank's old thread:

hank wrote:First, you would add the following line to your celestia.cfg file:

Code: Select all

   LuaHook "luahooktest.lua"

Then you would create the file luahooktest.lua in your Celestia resources directory. It could look like this:

Code: Select all

hooktest = {};

hooktest.luaoverlay =
   function(self)
      local time = celestia:gettime();
      celestia:flash("Time "..time,1);
   end;

celestia:setluahook(hooktest);

In this very simple example, a Lua object (table) called hooktest is created and a method luaoverylay is defined for it. Then the object is attached to Celestia with a call to celestia:setluahook. The luaoverlay method of the hooktest object will be called from the CelestiaCore draw method each time Celestia redraws the display. The luaoverlay method gets the current simulation time and flashes it on the screen (nominally for one second, but actually the message is updated with a new time almost instantly by the next call to luaoverlay).
- Hank
but it didn't work. Since this thread is not that new I was wondering if anything has changed in the celestia source code so this example doesn't work anymore.
I also tried to get along with the "lua_edu_tools"which worked but I still wasn't able to create my own script.

what I did was making an entry in celestia.cfg:
LuaHook "luahookinit.lua"

my luahookinit.lua looked like this:

Code: Select all

require "client"

celestia:setluahook("client")

then my client.lua looked like:

Code: Select all

printtesttext =
       function(self)
              celestia:print("text",1)
end


I also tried to do it like the lua_edu_tools:
then my client.lua was:

Code: Select all

client =
{
printtesttext =
       function(self)
              celestia:print("text",1)
end
}

but that didn't work either.
I did not get any output on the screen.

Is there sth. about the event handler included into the lua_edu_tools.lua?

If I Did not wrap the celestia:print()-command int o a function it's done but only once.

Nevertheless I don't get why I do not get an output when just copying hank's example.

hope somebody can figure out what's goin wrong here.

Thanks

Stuffer

Re: luahookscript doesn't work

Posted: 19.02.2009, 14:09
by Stuffer
Finally I did manage to get a hook script running, but until now it's only running once, at celestia's start-up.

What do I have to include to have it run more than once, or to be more specific several times a second?

Re: luahookscript doesn't work

Posted: 19.02.2009, 16:28
by chris
Stuffer wrote:Finally I did manage to get a hook script running, but until now it's only running once, at celestia's start-up.

What do I have to include to have it run more than once, or to be more specific several times a second?

You need to add an event handler for tick events:

Code: Select all

hooks = {
    tick = function(self, dt)
        -- Do something (like read from a socket with a zero timeout)
    end
}

celestia:setluahook(hooks)


The tick event is called once per frame rendered.

--Chris

Re: luahookscript doesn't work

Posted: 19.02.2009, 17:38
by Stuffer
Chris,

thanks, the hook script is working now.

I want to provide the values read by the hookscript to a scriptedorbit function but my s/c is not included when celestia starts.

I tried to figure it out by your rotator-addon, but obviously I don't get it. Since I want to set up a scriptedrotation function afterwards I try to think it through, please correct me where I'm wrong.

You create a new table kbrotations in the hook.
Then the fct kbrotations(sscvals) (called from celestia core) puts some values to begin with in kbrotations[name of s/c] by cel:newrot...
Actually I don't get the setmetatable-function which is perhaps why I don't get it all.
After that the user changes the values in kbrotations by hitting the number keys and activating a call to a "change rotation" function.
then celestia calls kbrotationproto:orientation and from there you call the array kbrotations[name] and return the q.values to celestia.


I thought it was enough to create a global table in the hook script and put the needed values in it by the function that is repeated in the hook.
Then the scripted orbit function just needs to make a call to the table and needs to assign the x, y, z values of the table and return them in the orbit:position function.

orbit.lua

Code: Select all

function lissajous(t)
   -- Create a new table
   orbit = {};

   -- Save the parameter list
   orbit.params = t;

   -- Set the required fields boundingRadius and position; note that position is actually a function

  orbit.boundingRadius = 6877.661

   local ncall = 1 --> count the number of how often Celestia calls orbit:position()

   -- The position function will be called whenever Celestia needs the position of the object
   function orbitdata:position(tjd)

      --------------------------------------------------------------------------------
      --ORBIT CALCULATIONS
      --------------------------------------------------------------------------------
      local x = orbitdata.x / 1000 <--dividing because I get the values in m not in km
      local y = orbitdata.y / 1000
      local z = orbitdata.z / 1000

      ncall = ncall + 1
      local s = "Number of Calls = " .. ncall .. "\n" .. "x = "..x..", y = "..y..", z = "..z
      celestia:print(s)
      return x, y, z
    end
   return orbit
end


and in the hook

Code: Select all

function getvalues()
...
        orbitdata.x = tonumber(string.sub(recdata, 0, i-1))
        orbitdata.y = tonumber(string.sub(recdata, i+1, k-1))
   orbitdata.z = tonumber(string.sub(recdata, k+1))
end

orbitdata = {}
calculate =
{
              tick-->getvalues()
}
setluahook(calculate)

Re: luahookscript doesn't work

Posted: 19.02.2009, 19:09
by chris
Stuffer wrote:Chris,

thanks, the hook script is working now.

I want to provide the values read by the hookscript to a scriptedorbit function but my s/c is not included when celestia starts.

Do you have your celestia.cfg file modified so that ScriptSystemAccessPolicy is set to "allow"? This is required in order for the LuaHook context to share data with the ScriptedOrbit/ScriptedRotation context. Otherwise, orbit and rotation scripts run in a secure "sandbox" context.

--Chris

Re: luahookscript doesn't work

Posted: 19.02.2009, 21:50
by Stuffer
No, the systemaccesspolicy can't be the problem.
I also thought that it's because of they are running in different "stacks"
But since trying the socket-access and scripteorbit with a file it is changed to "allow"

Also the connection to the socket causes rude buckling when accessing with every frame. But only when writing on the socket (as server) from a different computer. When having the server on the same machine there's no buckling.
Do you have an idea why this can be?