Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411487 Posts in 69371 Topics- by 58427 Members - Latest Member: shelton786

April 24, 2024, 12:11:11 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsCommunityTownhallForum IssuesArchived subforums (read only)TutorialsLua in flash
Pages: [1]
Print
Author Topic: Lua in flash  (Read 4039 times)
zamp
Level 1
*



View Profile
« on: November 10, 2011, 03:21:48 PM »

Lua in flash? Big Laff
No, seriously. Here we go!

First you need to fetch yourself Lua-Alchemy.
I recommend you grab the swc file.
I won't go into detail how to link the swc into other IDEs since I only know how to do it in FlashDevelop.
Place the swc (zip) into your project's lib directory. Change the file extension to swc.
Open the lib directory in your project viewer in FlashDevelop and right click on swc. Check Add To Library.

You can set up an automatic lua file embedding with lua alchemy but I won't explain how to do that. What's the point of embedding lua files since the reason to use lua is to not compile the app again and again?

Here's an example implementation; This uses AIR and you'll need to write an URLLoader method for loading the lua files if you want to use it outside of AIR.

Script loader:
Code:
// If you use the code as is you'll need to reference to the timer class once so it gets compiled.
Timer;

var stream:FileStream = new FileStream();
var scriptFile:File;

scriptFile = File.applicationDirectory.resolvePath("sleep.lua");
stream.open(scriptFile, FileMode.READ);
var scriptText:String = stream.readUTFBytes(stream.bytesAvailable);

scriptFile = File.applicationDirectory.resolvePath("theOtherLuaFile.lua");
stream.open(scriptFile, FileMode.READ);
scriptText += stream.readUTFBytes(stream.bytesAvailable);
If you're wondering why I'm merging two lua files I'll explain it later.

Using the now loaded script:
Code:
var lua:LuaAlchemy = new LuaAlchemy();
// set globals here
lua.setGlobal("script", this);

lua.doStringAsync(scriptText, callback);

You will also need to have a callback function for Lua to call back to in case of errors and when the file has completed.
You could do lua.doString() but that's deprecated and not suggested since it will suspend other activity for the duration of the execution.

Callback function:
Code:
private function callback(stack:Array):void 
{
// first value of stack is true if all went well, false if not
if (stack.shift() == false)
trace(stack.toString());
}

Why did I merge two files in the script load phase?
The reason is simple. For now Lua-Alchemy doesn't support dynamically loaded files. Which means require for dynamically loaded lua files is out of the question unless you've already set up (embedded) libraries yourself. Then you can call require("swf://file.lua"); but I've not tested this so don't quote me on that.

This is optional but it will help you if you want to have working coroutines in flash
sleep.lua
Code:
local main

local continue = as3.toas3(function()
print ("continuing coroutine")
assert(coroutine.resume(main))
end)

function sleep(milliseconds)
print("adding resume timer for ", milliseconds, "ms")
local timer = as3.class.flash.utils.Timer.new(milliseconds, 1)
timer.addEventListener(as3.class.flash.events.TimerEvent.TIMER, continue)
timer.start()

print("pausing coroutine")
coroutine.yield()
end

Usage example: (uses a coroutine with function calls to sleep.lua)
Code:
function routine()
print("foo")
sleep(1500)
print("bar")
end

main = coroutine.create(routine)
coroutine.resume(main)

Do keep in mind that you can only have one coroutine (main) that uses the sleep function with the example above.
You can of course write your own coroutine handling if you need more coroutines running simultaneously.

Hope you liked this tutorial.
« Last Edit: November 12, 2011, 02:10:15 AM by zamp » Logged
tametick
Level 3
***


Could take weeks, sir!


View Profile WWW
« Reply #1 on: December 05, 2011, 01:19:43 AM »

Looking good! Will have to give it a second look some time.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic