(Corona SDK) Call function once from an "enterFrame" function - function

I'm wondering how it would be possible to call a function once from an "enterFrame" function in Corona SDK.
Let's say i have:
local function funa()
i = i+1
funb()
end
Runtime:addEventListener("enterFrame", funa)
Besides wanting 'i' to be incremented each frame, I also want to run "funb", but only one time instead of one time for each frame, but I can't find out how to do that.
Any ideas?

Besides the simpler and more obvious solution of using a global variable to keep track of this, you can use a closure. Example:
local
function funa()
local run_already = false
local i = 0
return function()
i = i+1
if not run_already then
funb()
run_already = true
end
end
end
funa = funa()
funa()
funa()

local run_flag = false
local function funa()
i = i+1
if not run_flag then
funb()
run _flag = true
end
end
Runtime:addEventListener("enterFrame", funa)
Now i will incremented each frame , but function will be called once.

Related

Lua rewrite function

Hey is it possible to rewrite a function (add code inside it)
I really want to execute my code in the function that the user is executing.
Without an other argument
Like this:
function exampel(fnct)
fnct( function()
print('code thad i want to add')
end)
end
exampel( function() -- function user is executing
print('ok')
end)
You can run code before and after the function runs:
function wrap(f,b,a)
return function (...) b(...) f(...) a(...) end
end
function before()
print"before"
end
function after()
print"after"
end
function user()
print"user code"
end
wrap(user,before,after)()
here is a global function load(lua5.4),you can use it to dynamic execute a lua code.
local str = [[
local a = 3
local b = 4
return a+b
]]
print(load(str)())--7

How to restart a function when an if condition is met

How do I "restart" my function when an if condition is or isn't met.
In this pseudocode, if the if condition is met, I would like to stop what the function was doing and restart again.
function example()
.
.
.
for i in 1:N
for j in 1:N
if Mat[i,j] > 1
Mat[i,j] += restart? # Here the process should restart,
end
end
end
Rather than adding additional branching and complicating a function, return values offer a great ability for control flow in I.M.H.O. much cleaner fashion.
Adding a little separate function is simple
function do_thing_once!(mat)
# Returns true if successful
for i in 1:N
for j in 1:N
if mat[i,j] > 1
mat[i,j] += 123
return false
end
end
end
return true
end
function do_all_things!(mat)
success = false
while !success
success = do_thing_once!(mat)
end
end

Is it impossible to assign output argument after completion of recursive loop in the function of MATLAB?

When the below function is executed, I get the following error message "Output argument "max_idx" (and maybe others) not assigned during call to "find_node"."
I think the problem is in the fact that some output is produced after recursive loop. How can I solve this?
function max_idx = find_node(wt)
persistent all_idx_max;
node_degree=sum(wt,2);
maxval = max(node_degree);
all_nodes_with_max_val = ismember(node_degree,maxval);
idx_max = find(all_nodes_with_max_val);
if isempty(all_idx_max)
all_idx_max=1:8;
end
if size(idx_max,1)==3
max_idx=all_idx_max(idx_max(1))
elseif size(idx_max,1)>1
all_idx_max=idx_max;
find_node(wt(idx_max,idx_max)); <--problem happens in this path
else
max_idx=all_idx_max(idx_max)
end
I forgot to add output to the recursive part
function max_idx = find_node(wt)
persistent all_idx_max;
node_degree=sum(wt,2);
maxval = max(node_degree);
all_nodes_with_max_val = ismember(node_degree,maxval);
idx_max = find(all_nodes_with_max_val);
if isempty(all_idx_max)
all_idx_max=1:8;
end
if size(idx_max,1)==3
max_idx=all_idx_max(idx_max(1))
elseif size(idx_max,1)>1
all_idx_max=idx_max;
***max_idx =*** find_node(wt(idx_max,idx_max)); <--problem happens in this path
else
max_idx=all_idx_max(idx_max)
end

LÖVE crashes, when using a function in ipairs()

I was running an experiment on whether or not I am able to use autogenerating seeds in LÖVE, but I'm running into a problem. It crashes, when I try to add tiles into the game through a table using ipairs.
Can anybody see the problem with this code?:
world = {}
function world.generate()
for i = 1, 100 do
world.addTile(i, love.math.random(1, 3), 1)
end
local tempWorld = world
for i,v in ipairs(tempWorld) do
world.addTile(v.x, v.y+1, 1)
end
end
function world.addTile(x, y, id)
for i,v in ipairs(tile) do
if v.id == id then
table.insert(world, {id = id, x = x*tile.w, y = y*tile.h})
else
print("The following id was not recognised: "..id)
end
end
end
function world.draw()
for i,v in ipairs(world) do
love.graphics.draw(tile.getImage(v.id), v.x, v.y)
end
end
You have an infinite loop.
local tempWorld = world does not copy world, it just creates another reference to it. So when world has another item added by world.addTile the for loop:
for i,v in ipairs(tempWorld) do
world.addTile(v.x, v.y+1, 1)
end
has a new stopping point since ipairs has one more item to iterate. This repeats until you run out of memory. You may want to save the size of the old list instead:
local oldsize = #world
for i=1, oldsize do
local v = world[i]
world.addTile(v.x, v.y+1, 1)
end
And now it wont iterate more than oldsize times.

Tips for function inside while loop and i=i+1, Matlab

I have a problem with a function in matlab. This specific function is for filtering light signals. As you can see below I added the coding I’ve used in the function and in the while loop itself. The code is written for a NXT Lego robot.
Is there any tip how to get the count variable ( i = i + 1 ) to work in the function, so we can plot Light(i)? Because we’re getting a bunch of error messages when we try different codes to make it work.
function [light] = filter_func( i)
lightI(i) = GetLight(SENSOR_3);
if i==1
light(i)=lightI(i)
elseif i==2
light(i) = 0.55*lightI(i) + 0.45*lightI(i-1)
else
light(i) = 0.4*lightI(i) + 0.3*lightI(i-1) + 0.3*lightI(i-2);
end
end
i=1
while true
lightI(i) = GetLight(SENSOR_3); % Get’s a lightvalue between 0 and 1024.
if i>2
light =filter_func(i)
light=round(light);
else
light(i) = GetLight(SENSOR_3);;
end
i=1+i
plot(light(end-90:end), 'r-');
title('Lightvalue')
axis([0 100 0 1023]) ;
end
You probably mainly get errors because you are not allowed to mix script and functions like this in MATLAB (like you are in Python).
Your filter function is only used when i>2 so why are you doing the first 2 tests? It seems like you want lightI as a global variable, but that is not what you have done. The lightI inside the function is not the same as the one in the while loop.
Since your while loop runs forever, maybe you don't need to worry about updating the plot the first two times. In that case you can do this:
filter = [0.4 0.3 0.3]';
latest_filtered_light = nan(90,1);
lightI = [];
p = plot(latest_filtered_light, 'r-');
title('Lightvalue')
axis([0 100 0 1023]) ;
while True
lightI(end+1,1) = rand*1024; % Get’s a lightvalue between 0 and 1024.
if i>=3
new_val = lightI(end-2:end,1)'*filter;
latest_filtered_light = [latest_filtered_light(2:end);...
new_val];
set(p, 'ydata', latest_filtered_light)
drawnow
end
end
I think it is an important point to not call plot every time - at least if you are the least concerned about performance.