RFC: frame callbacks

Discussion and information relevant to creating special missions, new ships, skins etc.

Moderators: winston, another_commander

Post Reply
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

RFC: frame callbacks

Post by JensAyton »

A while back, there were complaints that the limited granularity of JavaScript timers meant they couldn’t be use for detailed animations. At the time, I suggested it would be better to use a completely separate mechanism, since doing something every frame is conceptually distinct from regularly-occuring timers. I’ve now implemented about half of this idea, so it’s time for some feedback-seeking.

The callback timer API will consist of two methods. (I’m not sure where to put them; I don’t want to stick them on Timer, as that would confuse the distinction I’m trying to make, and making them global is a bit untidy.) addFrameCallback() will take a function, and return a tracking ID. removeFrameCallback() will take a tracking ID and remove the corresponding frame timer. (The nature of tracking IDs is unspecified; it’s just some value that you stash until you want to remove your frame callback.)

Frame callback will be invoked once per frame, with the time difference since the last frame (in [EliteWiki] game real time seconds) as a parameter. I haven’t decided whether it will be called (with a delta of 0) while paused; this depends in part on how easy it is to hook it up properly. For minimum overhead, there will be no way to specify the “this” value of the callback (so hey, we have a use for Function.prototype.bind() already), but it will have access to the variables in its declaration scope as usual.

Frame callbacks will be invoked after all entity updates, but before drawing, which seems the sensible place for custom animation. (For historical reasons, timers and tickle events fire during the player’s updates, which occur before every other entity’s.)

To support one of the obvious cases for custom animation, we’ll provide a way to get a reference to the model on a running mission screen. There are probably ways to mess this up horribly, but hey, Cabal_Common_Library already has a nasty hack to achieve this anyway.

As a minor note, since I just had to mess about with it, adding and removing frame callbacks from within a running frame callback will be possible, but somewhat inefficient, and changes will take effect in the next frame. (This last bit shouldn’t matter since order of execution will be undefined anyway.)
Last edited by JensAyton on Sat Jan 08, 2011 1:46 am, edited 1 time in total.
Reason: Noted order of execution is undefined.
another_commander
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 6681
Joined: Wed Feb 28, 2007 7:54 am

Re: RFC: frame callbacks

Post by another_commander »

Ahruman wrote:
The callback timer API will consist of two methods. (I’m not sure where to put them; I don’t want to stick them on Timer, as that would confuse the distinction I’m trying to make, and making them global is a bit untidy.)
Since the frame drawing is handled inside Universe and this is going to be essentially a frame-related timer, would it make sense to implement it inside Universe as well? Or this is what you refer to as making it global?
User avatar
Griff
Oolite 2 Art Director
Oolite 2 Art Director
Posts: 2483
Joined: Fri Jul 14, 2006 12:29 pm
Location: Probably hugging his Air Fryer

Re: RFC: frame callbacks

Post by Griff »

Wow, this sounds exciting! What sort of animations will we be able to do? will we be able to rotate subentities from one position to another depending on certain events happening? eg like a star wars X-Fighter ship opening its wings from - to x when it goes hostile, or docking bay doors opening/closing on a space station?
User avatar
Cody
Sharp Shooter Spam Assassin
Sharp Shooter Spam Assassin
Posts: 16081
Joined: Sat Jul 04, 2009 9:31 pm
Location: The Lizard's Claw
Contact:

Re: RFC: frame callbacks

Post by Cody »

Griff wrote:
Wow, this sounds exciting! What sort of animations will we be able to do? will we be able to rotate subentities from one position to another depending on certain events happening? eg like a star wars X-Fighter ship opening its wings from - to x when it goes hostile, or docking bay doors opening/closing on a space station?
Those docking-bay doors inside Griff’s rock hermits… I’d like to see those open and shut.
I would advise stilts for the quagmires, and camels for the snowy hills
And any survivors, their debts I will certainly pay. There's always a way!
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5528
Joined: Thu Jun 12, 2008 6:55 pm

Re: RFC: frame callbacks

Post by Thargoid »

Griff wrote:
Wow, this sounds exciting! What sort of animations will we be able to do? will we be able to rotate subentities from one position to another depending on certain events happening? eg like a star wars X-Fighter ship opening its wings from - to x when it goes hostile, or docking bay doors opening/closing on a space station?
We can do that anyway, that's simple. The problem is that as timers fire at a smallest rate of 0.25s, you essentially end up with 4FPS if you try and "animate" things using them. For simple things (e.g. my Stellar Serpents or Butterflies) it works ok, but for other things it looks lousy (especially if you try and move things rather than just rotate them).

Looking forward to having a play with this, as I have a couple of test projects on the go at the moment which I was a little disappointed with due to the 4FPS. This may re-open their possibilities somewhat, which could be fun.
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: RFC: frame callbacks

Post by JensAyton »

another_commander wrote:
Ahruman wrote:
The callback timer API will consist of two methods. (I’m not sure where to put them; I don’t want to stick them on Timer, as that would confuse the distinction I’m trying to make, and making them global is a bit untidy.)
Since the frame drawing is handled inside Universe and this is going to be essentially a frame-related timer, would it make sense to implement it inside Universe as well? Or this is what you refer to as making it global?
I meant in terms of the JS API. There’s no Universe in JS; it mostly corresponds to system, but the frame callbacks won’t be tied to the current system in any way.

Internally, they’ll be handled by a new singleton, although I might change it into (gasp) C functions.
Griff wrote:
Wow, this sounds exciting! What sort of animations will we be able to do? will we be able to rotate subentities from one position to another depending on certain events happening? eg like a star wars X-Fighter ship opening its wings from - to x when it goes hostile, or docking bay doors opening/closing on a space station?
As Thargoid says, they won’t change what you can do, only how often you can do it. Moving subentities around is possible already, but the ship’s collision geometry doesn’t update when you do it (and I’m not going to make major changes to that before MNSR), so geometry changes need to stay roughly within the same volume.
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: RFC: frame callbacks

Post by JensAyton »

One other thing: timers created in ship scripts are sneakily associated with the ship, and are automatically stopped when the ship disappears. Frame timers won’t be, they’ll need to be manually managed (except when the game resets, of course).
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: RFC: frame callbacks

Post by JensAyton »

Frame callbacks are now in, as described above. addFrameCallback and removeFrameCallback are global methods, and there’s also an isValidFrameCallback (which you shouldn’t need, but it’s helpful for writing unit tests).
User avatar
TGHC
---- E L I T E ----
---- E L I T E ----
Posts: 2157
Joined: Mon Jan 31, 2005 4:16 pm
Location: Berkshire, UK

Re: RFC: frame callbacks

Post by TGHC »

Griff wrote:
Wow, this sounds exciting! What sort of animations will we be able to do?
I don't know about the other guys, but when you get excited, the whole galaxy moves!
The Grey Haired Commander has spoken!
OK so I'm a PC user - "you know whats scary? Out of billions of sperm I was the fastest"
Post Reply