I have a lingo script which runs some data processing for a Flash movie. I can call my Lingo functions from Flash by putting the following inside one of my methods:
getURL("Lingo: myMethod");
and I can pass parameters from flash to lingo as follows:
getURL("Lingo: myMethod param");
However, if myMethod returns a value, I can't seem to send it back to ActionScript. How do I code the following:
var myVar = getURL("Lingo: myMethod");
where myMethod is defined as:
on myMethod
--do something
return 5
end myMethod
We are using Flash 9 with CS 3.
You should be able to access Lingo via ExternalInterface assuming you're in Flash 8 or greater:
import flash.system.ExternalInterface;
var valueFromLingo = ExternalInterface.call("myMethod");
trace(valueFromLingo); // -> 5
#wulong:
the package is flash.external., not flash.system.
Related
I'm trying to change the behavior of a Lua function, by adding code to the start or end of it. Because this is a mod for a game, I can't edit the function directly, so I have to override it instead. I accomplish this by storing a reference to the original function in a local variable, then redefining the function with my own, which calls the original one along with any prefix or postfix code I need to add, like so:
local base_exampleFunction = ExampleBaseGameClass.exampleFunction
function ExampleBaseGameClass.exampleFunction(param1, param2)
--Prefix code goes here
base_exampleFunction(param1, param2);
--Postfix code goes here
end
This works fine for functions defined with the ClassName.functionName syntax, but some functions use ClassName:functionName instead, which from what I understand, are functions that pass a reference to the class instance as the first parameter. I can't figure out how to prefix/postfix these functions, as I get the following error when declaring a variable to hold the original function if I try the same approach:
attempted index: exampleFunction of non-table: null
Is there a way to make this work?
: functions are just scary ways of saying "the first argument is self".
So, ExampleBaseGameClass:exampleFunction(param2) is equivelent to ExampleBaseGameClass:exampleFunction(ExampleBaseGameClass, param2)! It's just got self at the beginning, and functions declared with : will have an invisible self variable appear out of nowhere.
local a = {}
function a.b(self)
print(self)
end
function a:c()
print(self)
end
-- a.c(a) == a:c()
-- a:b() == a.b(a)
-- a:b() == a:c()
Using this idea, we can simply prepend an argument (it does not have to be called "self", it just has to be the first argument).
This should work, unless there is a part of your Lua environment (eg, funky metatables) which would prevent such a thing:
local base_exampleFunction = ExampleBaseGameClass.exampleFunction
function ExampleBaseGameClass.exampleFunction(self, param1, param2)
--Prefix code goes here
base_exampleFunction(self, param1, param2);
--Postfix code goes here
end
The lua library takes advantage of the first argument being the calling object for it's string library. Notice how ("hello"):gsub() works--by passing the string itself as the first argument!
I would need to call a function that is declared as a string
i.e local abc = "function x() print('math.sqrt(25)') end"
i've tried load(abc) that gives me no errors but nothing is printed and print(load(abc)) that returns function: 0000000000ebcea0
I've made another tries but nothing seems to suit my case (or probably i'm just bad)
Please read the manual...
If there are no syntactic errors, load returns the compiled chunk as a
function
If you want that function to be called, call it.
local abc = "function x() print('math.sqrt(25)') end"
local loadedFunction = load(abc)
loadedFunction()
or simply
load(abc)()
Of course you should check wether load actually succeeded.
If you want something to happen you probably should also call the function that has been defined by the function you loaded.
load(abc)()
x()
Let's say that I call some functions indirectly, via a variable. For example:
obj = {
on_init = function()
print "hello."
end,
on_destroy = function()
print "bye."
end,
on_do_something = function()
print "doing something."
error("Hi de hi, hi de ho!")
end,
}
local event = "do_something"
local func = obj["on_" .. event]
func()
All works fine.
However, the problem is that when the called function raises an exception (as in the code above) the error message isn't quite clear. It is thus:
lua: test.lua:13: Hi de hi, hi de ho!
stack traceback:
[C]: in function 'error'
test.lua:13: in function 'func'
test.lua:20: in main chunk
It says "in function 'func'". I'd prefer it to say "in function 'on_do_something'" instead.
I'd imagine this scenario to be very common. Is there a solution for this?
I tried calling the function thus:
obj["on_" .. event]()
But then the error message says "in function '?'", which isn't helpful either.
(I tried this code on Lua 5.1, 5.2 and LuaJIT without notable differences.)
This is a limitation of the heuristics used by Lua to provide names for functions.
In Lua, all functions are anonymous. A given function can be the value of several variables: global, local, and table fields. The Lua debug system, which is used in error handling, tries to find a reasonable name for a value based on where it came from by looking into the bytecode being executed.
See
Why is 'name' nil for debug.getinfo(1).
You have a few options. The debug module will try to produce something useful. For instance, you might be able to get the file name and line number where it was defined, if this is your own code. See
http://www.lua.org/pil/23.1.html for the list of what is available via debug module. Or, you might be able to define the functions in the module, then add them to the table:
-- module something
function a()
...
end
tt = {
fn1 = a,
...
}
Depending on where you trap the error (error handler installed via debug hook?), you could check if filename is the module where your table of functions is defined, and if it is, then use the debug module to print appropriate info based on your table structure etc; if it is not, just print the default traceback etc.
I am trying to call a private static method of a class from inside a closure inside an other method of this same class, and I can't find the right way to do it...
even using use referencing the private method...I am able to reference a private variable and pass it, but no way to reference the private method...
$refMethod = array('App','_onEvent'); with call_user_func($refMethod) will throw the method is private...
I tried with ReflectionClass with PHP 5.4 version also, (WAMP 32bits), but it says that the getClosure method doesn't exists on the instance :(
class App(){
static public function start(){
new Form('myform', array('submit'=>function($form) use($someVar){
if($anyCondition){
// want to call private self::_onEvent here : any suggestion ?
}
}));
}
static private function _onEvent(){
// this is my very private part
}
}
Well I know quite closure has no scope but so...any way to pass the private context (because the closure is inside the class) for accomplish something like this ? Thanks for any lights !
EDIT : I exactly want to do this answer but this just throw the great
Cannot access self:: when no class scope is active
Ok dudes,
it works better with PHP 5.4, my upgrade was not ok, I finally get rid of boring complications when upgrading php version, and now the straight solution works :
calling directly self::privateMethod() won't throw the 'self is nothing in closure...' anymore
Appart from that, did you know that from now (5.4), (0 == 'anystring') is TRUE now, duh ! need to use strict equal everywhere now, wonderfull upgrate :-s
In the application I'm developing in Java SE I use Luaj to implement functionality (this is a data collector application). The Java app reads a COM port of a device and gives the data to Lua event handlers which are written by the user of the application. Part of the user interface is also constructed from Lua, however, I'm having problems adding ActionListener objects (implemented in Lua as well) to Swing components, like JButton.
The code I'm currenty stuck at:
button = luajava.newInstance("javax.swing.JButton","test")
visuals:getPanel():add(button)
This creates a JButton object and puts it on a JPanel component. I'd like to define the action listener for this button in Lua as well.
Any idea how I can do that?
I tried the following, but it obviously does not work.
al = {}
function al.actionPerformed(ev)
print("test")
end
button.addActionListener(al)
I come a bit late, but for the reference, the swingapp.lua script shows how to handle listeners:
button:addActionListener(luajava.createProxy('java.awt.event.ActionListener',
{
actionPerformed = function (e)
print('Action', e)
end,
}))
Tested with Luaj-jse 3.0-alpha1