Skip to content

Basc Flash Integration Info

Mido edited this page Sep 14, 2010 · 3 revisions

Note: this article is quick and dirty, it needs refinement (and things will probably change as the engine develops more)
Note 2: I messed up the title of this article and I don’t see how to fix it! Aiieee

Flash well integrated into the NS2 engine, enough so that it shows up in multiple places. The targeting crosshair and the basic timing information at the top left of your in-game screen, both belong to the exact same flash file.

Overview

Let’s take a look at the single frame of code that drives the hud.swf(Natural Selection 2\ns2\ui\hud.swf). This is the simplest flash file I found, since there is only one frame and no animations.

function doFrame()
{
    crosshair._x = Math.floor(Stage.width / 2);
    crosshair._y = Math.floor(Stage.height / 2);
    if (lua != undefined)
    {
        score.text = lua("PlayerUI_GetScore");
		var _health = int(lua("PlayerUI_GetHealth"));
        var _loc1 = int(lua("PlayerUI_GetGameTime"));
		healthtext.text = "Health: " + String(_health) + "%";
        if (_loc1 < 0)
        {
            if (_loc1 != -1)
            {
                time.text = "Game starting in " + -_loc1 + " seconds";
            }
            else
            {
                time.text = "Game starting in 1 second";
            } // end else if
        }
        else
        {
            var _loc2 = _loc1 % 60;
            var _loc3 = int(_loc1 / 60);
            if (_loc2 < 10)
            {
                _loc2 = "0" + _loc2;
            } // end if
            time.text = _loc3 + ":" + _loc2;
        } // end else if
    }
    else
    {
        score.text = "";
        time.text = "";
		healthtext.text = "";
    } // end else if
    scoreShadow.text = score.text;
    timeShadow.text = time.text;
	healthtextShadow.text = healthtext.text;
} // End of the function
doFrame();
onEnterFrame = function ()
{
    doFrame();
};

The most important pieces of code in this, are the very last 4 lines.

doFrame();
onEnterFrame = function ()
{
    doFrame();
};

When the NS2 engine instantiates this flash menu, it’ll run the defined code once (with doFrame();) then it registers a basic event to call every time this .swf instance is ticked, which creates a little function object, which simply calls the doFrame() logic.

The all important lua( arg ) function

From inside of the doFrame we can see an important piece of Flash’s integration into all of this:
score.text = lua("PlayerUI_GetScore");.

This simply calls the Lua function “PlayerUI_GetScore”, and there appears to be casts setup to handle the primary data types you can return from Lua, like arrays or ints.

We can expose just about anything we want to, and as long as it’s in scope when hud.swf is ticking, it SHOULD evaluate and optionally return something. An example on how you might handle something like, passing chat to the hud.swf

Lua:

 local chat_cache = { 
                       "Bob: this is some example chat",
                       "Joe: Hello fellow chatters!",
                       "Bob: chit chat",
                       "Frank: ^^ retarded" }
function PlayerUI_PollChat()
    -- make a temporary copy of the cache on this function's stack
    local retval = chat_cache
    -- clear the cache after something pulls it
    chat_cache = { }
    -- pass it back to whoever
    return retval
end

Actionscript 3.0:

function handleChat(incoming_chat)
{ 
    // blah blah 
}
function doFrame()
{
    var newchat = lua("PlayerUI_PollChat");
    if (newchat.length() > 0)
        handleChat(newchat);
Clone this wiki locally