Corona Lua call external function - function

I have block_basic.lua that I want to call another function in touch_input.lua
block_basic.lua does:
local screen_touch = require( "touch_input")
local onScreenTouch = screen_touch.onScreenTouch
local function touched( event )
-- 'self' parameter exists via the ':' in function definition
print(onScreenTouch, screen_touch, event)
end
From what I am seeing event seems to be correct (a table), screen_touch is also correct. But the function (screen_touch.onScreenTouch) is always nil and I don't know why
In touch_input.lua I simply have:
local function onScreenTouch( event )
-- no 'self' parameter
etc. etc.
Why is it nil? Why can't I call it?

You don't show what you return in touch_input.lua, but if you expect the first two lines of your script to work, it needs to be something like this:
local function onScreenTouch( event )
...
return {
onScreenTouch = onScreenTouch
}
Since you don't get a run-time error on line 2, you may be returning a table already, but you need to make sure that onScreenTouch field of that table points to onScreenTouch function.

Here is how your files should be:
touch_input.lua:
local M = {}
M.onScreenTouch = function( event )
--some code here
end
return M
block_basic.lua:
local screen_touch = require( "touch_input")
local onScreenTouch = screen_touch.onScreenTouch
print(onScreenTouch, screen_touch, event)
I tested it. It works 100%.
More info:
http://www.coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/
http://www.coronalabs.com/blog/2011/09/05/a-better-approach-to-external-modules/
http://developer.coronalabs.com/content/modules
http://www.coronalabs.com/blog/2011/07/06/using-external-modules-in-corona/

Related

Error : 'x' undefined

I got a problem with running Octave function (ODE), I've tried already present solutions for this problem but nothing is working. I've also tried by saving my filename as egzamin.m but it too not worked.
Code from octave :
function dx=egzamin(x,t)
dx=zeros(4,1);
b=0;
g=9.81;
x1=x(1);
y1=x(2);
Vx=x(3);
Vy=x(4);
dx(1)=Vx;
dx(2)=Vy;
dx(3)=-b*Vx*sqrt(Vx.^2+Vy.^2);
dx(4)=-b*Vy*sqrt(Vx.^2+Vy.^2)-g;
endfunction
N=mod(291813,100);
x1=0;
y1=0;
Vx=20+N;
Vy=20+N;
t=0:0.01:500;
sol=lsode("egzamin",[x1,y1,Vx,Vy],t);
plot(sol(:,1),sol(:,2))
The error is :
error: 'x' undefined near line 5 column 4
error: called from
egzamin at line 5 column 3
Since the file starts with function, it is not a script file,
as explained in the doc:
Unlike a function file, a script file must not begin with the keyword
function
Add any statement (even dummy like 1;) before the function line to get a script file.
# dummy statement to get a script file instead of a function file
1;
function dx=egzamin(x,t)
g = 9.81;
Vx = x(3);
Vy = x(4);
dx = [Vx, Vy, 0, -g];
endfunction
N=mod(291813,100);
x1=0;
y1=0;
Vx=20+N;
Vy=20+N;
t=0:0.01:500;
sol=lsode("egzamin",[x1,y1,Vx,Vy],t);
plot(sol(:,1),sol(:,2))
A very clear explanation of what's going on is given here.
You need to save the function (thus from function to endfunction and naught else) as egzamin.m, and then execute the rest of the code in a script or at the command line. Alternatively, provided Octave does that the same as what MATLAB does nowadays, first put your script (N=(..) to plot()) and then the function.
This is necessary since you are defining your function first, so it doesn't have any inputs yet, as you don't define them until later. The function needs to have its inputs defined before it executes, hence you need to save your function separately.
You can of course save your "script" bit, thus everything which is currently below your function declaration, as a function as well, simply don't give it in- and outputs, or, set all the input parameters here as well. (Which I wouldn't do as it's the same as your
egzamin then.) e.g.
function []=MyFunc()
N=mod(291813,100);
x1=0;
y1=0;
Vx=20+N;
Vy=20+N;
t=0:0.01:500;
sol=lsode("egzamin",[x1,y1,Vx,Vy],t);
plot(sol(:,1),sol(:,2))
endfunction

Mysterious stackoverflow in function call?

I'm creating a Lua program and I tried to use something like inheritance, but I got a stack overflow when I called the super of the function.
function Parent()
local self = {}
self.println = function(text) print(text) end
return self
end
function Child()
local super = Parent()
local this = {}
this.println = function(text)
super.println(text) -- According to the debugger, here is the problem.
print("Child")
end
for k, v in pairs(this) do
super[k] = v
end
return this
end
local a = Child()
a.println("Hello!")
I know there are other ways to do this, but I must use this kind of "OOP". Can anyone recommend me anything?
Your problem is with the loop that makes all println functions be the same println function that redirects the call to another printnl function, which is actually the same print function, creating a loop.
for k, v in pairs(this) do
super[k] = v
end
Removing that loop will make the code work, but I am not sure if this makes it behave as you wanted.
Maybe you should take a look at lua metatables if you want to inherit methods.
That for k, v in pairs(this) do loop seems backwards to me.
You are pushing the this copies of the functions into the super table (overwriting the ones that are already there).
So you push this.println into super and then call super.println inside it and you end up calling yourself repeatedly.
Did you mean to do that the other way around? Copy the super functions into this? Or what was the point with that loop in the first place?

Can a local function replace\override itself in Lua

In Lua is it possible to replace a local function from within itself? For example:
local function emptyFunction(arg)
return
end
local function exampleFunction(arg)
local result, err = io.open("/tmp/BadFile","w")
if result == nil then
print(err)
exampleFunction = emptyFunction
return
end
io.close(result)
end
To redefine a function within the function itself, you need a reference to the function in a variable that has already been declared before the variable is assigned a reference to the function.
First example -- how not to do it -- does not work because "fx" is not in scope inside fx, as intuition suggests (to me) it would not be.
Frankly, I don't know how to precisely describe what "fx" inside the function represents -- but it seems to be a global variable that is in a scope that is somehow "more global than a 'global local' declaration."
In the global space, I always assumed these two were equivalent:
foo = 'bar';
local foo = 'bar';
Precisely speaking, they apparently are not genuinely equivalent, if you have a way of accessing both. This first example does exactly that, and here's why:
When you do this...
local myfunc = function () ...
... what are you actually doing?
You're first declaring an anonymous function, and second declaring "myfunc" and third setting "myfunc" as a reference to the anonymous function you just created. Until the moment the function begins to exist, "local myfunc" does not yet exist, because the statement that declares it hasn't actually been executed yet. (At least, that's how I perceive it.)
Here's the non-working version that illustrates this:
local fx = function ()
print(fx);
print("inside the original fx")
fx = function() print "I was redefined" end
end
function f2()
print("f2")
f2 = fx
end
f = f2
print(fx);
fx(); fx(); f2(); f2() f();
function: 0x21a6140 -- original "fx" ref is in "global local fx"
nil -- inner "fx" -- NOT set on first iteration, so it
inside the original fx -- can't be the same varliable
function: 0x21a6510 -- now it's set, but to new function,
inside the original fx -- but the "global local fx" does not contain our new function
f2
function: 0x21a6510
inside the original fx
f2
Second example, we declare local fx first, and then set it to a reference to the function. There are no other changes.
local fx; -- declare before assignment
fx = function ()
print(fx);
print("inside the original fx")
fx = function() print "I was redefined" end
end
function f2()
print("f2")
f2 = fx
end
f = f2
print(fx);
fx(); fx(); f2(); f2() f();
function: 0x2188e50 -- original function
function: 0x2188e50 -- same reference, so this one *is* in scope
inside the original fx
I was redefined -- and redefining it works exactly as expected
f2
I was redefined
f2
So, yes, there is a way. You need a global local variable that is declared, and subsequently you need to set that to a reference to the function so that it is in scope in the function that tries to redefine it.
In a similar way, the f2 function can't redefine itself. It only redefines what f2 means, inside f2 while f2() is running.
Nothing prevents you from doing so. Local or not function name in Lua is actually variable name which points to function itself.
So there is no any difference between
local function f1()
...
end
and
local f1 = function()
...
end
In both cases f1 is in scope of function body.
However, such replace won't change no any external references
function f1()
print("f1")
end
function f2()
print("f2")
f2 = f1
end
f = f2
f1(); f2(); f2() f();
will lead to output
f1
f2
f1
f2
Note, that if you add local keyword to declarations - nothing changes

Why brunch compile everything it found when using function in joinTo

I don't understand why BrunchJS compile all files (in bower_components) when i use a function like this (CoffeeScript):
modules = ['i18n', 'pager', 'core', 'module-comment']
javascripts:
joinTo:
# Main
'/js/master.js': () ->
paths = ['bower_components/bootstrap/**/*', 'app/**/*']
for o in modules
fs.exists '../../../../workbench/dynamix/' + o, (exists) ->
if exists
paths.push '../../../../workbench/dynamix/' + o + '/public/public/**/*'
else
paths.push '../../../../vendor/dynamix/' + o + '/public/public/**/*'
return paths
I want to test if some path exist, if yes put the complete path in a variable to return it to joinTo. I have successfuly get files in workbench/vendor but it get some undesired files from bower_components (don't specified?!)
I would like to optimize this :
javascripts:
joinTo:
# Main
'/js/master.js':
'bower_components/bootstrap/**/*'
'../../../../workbench/dynamix/i18n/public/public/**/*'
'../../../../workbench/dynamix/pager/public/public/**/*'
'../../../../vendor/dynamix/core/public/public/**/*'
'../../../../workbench/dynamix/module-comment/public/public/**/*'
'../../../../workbench/dynamix/module-love-live-music/public/public/**/*'
'../../../../workbench/dynamix/module-rating/public/public/**/*'
'../../../../workbench/dynamix/module-registration/public/public/**/*'
'app/**/*'
I'm sorry i didn't find documentation to use function in joinTo.
Thanks
A function in a joinTo should take a file path as an argument and return true if the path should be included, false if not. This is described in the anymatch documentation.
Your function appears to always return a truthy value, meaning every path Brunch is watching will be included.
What you might have intended to do is use an IIFE, so the return value of the function (invoked during initial code evaluation) gets assigned to the joinTo. In coffeescript you can accomplish this easily using the do keyword, so instead of starting off your function definition with () -> it'd be do -> instead.
Thank you for your time and your answer.
I make the function to get files and test if the path exists and it work fine.
If this could help someone, I let my magic function here
javascripts:
joinTo:
# Main
'/js/master.js': [
'bower_components/bootstrap/**/*'
'bower_components/unveil/**/*'
'app/**/*'
(string) ->
response = false
modules = ['i18n', 'pager', 'core', 'module-comment']
for o in modules
exists = fs.existsSync unixify('../../../../workbench/dynamix/' + o)
if exists
if unixify(string).indexOf(unixify('../../../../workbench/dynamix/' + o + '/public/public/')) != -1
response = true
else
if unixify(string).indexOf(unixify('../../../../vendor/dynamix/' + o + '/public/public/')) != -1
response = true
return response
]

Passing variables into a function in Lua

I'm new to Lua, so (naturally) I got stuck at the first thing I tried to program. I'm working with an example script provided with the Corona Developer package. Here's a simplified version of the function (irrelevant material removed) I'm trying to call:
function new( imageSet, slideBackground, top, bottom )
function g:jumpToImage(num)
print(num)
local i = 0
print("jumpToImage")
print("#images", #images)
for i = 1, #images do
if i < num then
images[i].x = -screenW*.5;
elseif i > num then
images[i].x = screenW*1.5 + pad
else
images[i].x = screenW*.5 - pad
end
end
imgNum = num
initImage(imgNum)
end
end
If I try to call that function like this:
local test = slideView.new( myImages )
test.jumpToImage(2)
I get this error:
attempt to compare number with nil
at line 225. It would seem that "num" is not getting passed into the function. Why is this?
Where are you declaring g? You're adding a method to g, which doesn't exist (as a local). Then you're never returning g either. But most likely those were just copying errors or something. The real error is probably the notation that you're using to call test:jumpToImage.
You declare g:jumpToImage(num). That colon there means that the first argument should be treated as self. So really, your function is g.jumpToImage(self, num)
Later, you call it as test.jumpToImage(2). That makes the actual arguments of self be 2 and num be nil. What you want to do is test:jumpToImage(2). The colon there makes the expression expand to test.jumpToImage(test, 2)
Take a look at this page for an explanation of Lua's : syntax.