The strangest code ever. "The ghost code".

So here I am a noob from nowhere. So I´ve got a kod which is 100 % correct but it will not react. Okay maybe it is not 100 % correct but from the knowledge I have it is.

local function helloWorld ()

if(1+1 == 2) then

print(“Good job!”) 

else

print(“Bad job”)

end

end

So as I have this simple if-statement inside this function the Corona simulator does not response. But as soon as I delete the function and just running the if statement it will work. So why in the world does it work like that because I´ve been working with a lot of functions with if statements inside of it. So can anyone please explain me this ghost code for me?

you need to call the function you named ‘helloWorld’ …

just add this to the end of what you already have

helloWorld()

But why havent i done that on my other functions…? That´s so strange. I need not need to clan my other functions?

It’s not strange at all. I don’t know what happens in your other code, but basically your code here just says:

"If someone tells me to do 'HelloWorld' then I will print 'good job' or 'bad job'!

But then after that, nobody tells your application to actually DO ‘HelloWorld’ so nothing happens.

This is a another part of my code. Where in the world does this code get called?

local function clearAnswerEvent(event)

userAnswer=""

if(randomPlusMinus==1) then

screenPlus.text=randomDigits…"+"…randomDigits1… " = ";

else

screenMinus.text=randomDigits…"-"…randomDigits1… " = ";

end

end

You are confusing me. We are saying your function ‘HelloWorld’ is NOT called. Are you saying it is, or are you talking about the function ‘clearAnswer’ ?

In the last code you posted, the function ‘clearAnswer’ is not called either. It is only declared (or described) but not executed. At least not in the code you posted.

Functions are like sections of code that can be reused. When you write

local doSomethingNice = function() print("Cleaning out the garbage...") end

You’re making a block of code, as thomas6 says, that says "when the computer tells me to doSomethingNice, I’ll print “Cleaning out the garbage…” and be done.

The problem is, you haven’t ever told the computer to do it. You have to call the function for it to run:

doSomethingNice()

The parentheses mean “do this function”. So when you write that, the computer obliges and prints “Cleaning out the garbage…”.

Some functions are called internally by Corona. You never actually call them in your code:

scene.show = function(self, event) print(event.phase) end

In this case, the function seems to run by itself because Corona actually calls it behind the scenes (pun intended, heh heh heh). It doesn’t, however; somewhere back in the depths of the Corona engine code, you’ll find the line (or the equivalent in C => Lua).

scene:show(event)

I recommend that you get very comfortable with Lua (and programming in general) before diving into Corona. Without an understanding of the language Corona is built on, you won’t be very productive with Corona itself. Corona provides a pretty good tutorial on Lua here: http://docs.coronalabs.com/guide/start/introLua/index.html.

Bottom line is, if you don’t call it, it won’t run. So, if a function ends up running, somebody somewhere called it. If you didn’t call it, it must have been Corona. Or maybe some tiny (tiny, tiny, tiny) tapir who lives inside your processor.

[EDIT:] Function notation changed in response to comment from @thomas6.

  • Caleb

I actually prefer this notation because it is clearer to me:

local mySuperFunction = function() print("This text prints when the function is executed") end

This just tells Corona that the local variable called mySuperFunction is a function that prints some text. It is like describing a process.

But describing a process is not the same as DOING a process, as executing it. If you want to execute a process, you don’t describe it, you just sort of order Corona to do it, like this:

mySuperFunction() -- this tells Lua (or Corona) to perform the function called mySuperFunction

May I ask, what type of app are you trying to write? Maybe we can help!

Josh,

In the last example you posted, the reason you did not have to call that function  ‘clearAnswerEvent()’, like the first example ‘hellowWorld()’, is because this second example you posted appears to be called by an event-listener.  

That ‘clearAnswerEvent’ function you show in your example is handled by a line of code you have somewhere in your code. 

Somewhere in your code you have something like   '… addEventListener(“touch”, clearAnswerEvent)   

That event listener code takes care of calling the function for you… this is all handled behind the scenes by Corona’s code.

One thing that might help is to search the internet for some basic lua tutorial examples.  Try to look at a lot of examples of code and get a good understanding of tables and functions in lua, and that will help a lot.

Good luck.

sorry, I see now Caleb covers this same information.

Yeah that´s true…I´ve got to do that. But I understand. 

You may want to buy a book on Lua programming (or any introductory programming book, because these are fundamental concepts). There’s one called “Beginning Lua Programming” that seems well-regarded.

you need to call the function you named ‘helloWorld’ …

just add this to the end of what you already have

helloWorld()

But why havent i done that on my other functions…? That´s so strange. I need not need to clan my other functions?

It’s not strange at all. I don’t know what happens in your other code, but basically your code here just says:

"If someone tells me to do 'HelloWorld' then I will print 'good job' or 'bad job'!

But then after that, nobody tells your application to actually DO ‘HelloWorld’ so nothing happens.

This is a another part of my code. Where in the world does this code get called?

local function clearAnswerEvent(event)

userAnswer=""

if(randomPlusMinus==1) then

screenPlus.text=randomDigits…"+"…randomDigits1… " = ";

else

screenMinus.text=randomDigits…"-"…randomDigits1… " = ";

end

end

You are confusing me. We are saying your function ‘HelloWorld’ is NOT called. Are you saying it is, or are you talking about the function ‘clearAnswer’ ?

In the last code you posted, the function ‘clearAnswer’ is not called either. It is only declared (or described) but not executed. At least not in the code you posted.

Functions are like sections of code that can be reused. When you write

local doSomethingNice = function() print("Cleaning out the garbage...") end

You’re making a block of code, as thomas6 says, that says "when the computer tells me to doSomethingNice, I’ll print “Cleaning out the garbage…” and be done.

The problem is, you haven’t ever told the computer to do it. You have to call the function for it to run:

doSomethingNice()

The parentheses mean “do this function”. So when you write that, the computer obliges and prints “Cleaning out the garbage…”.

Some functions are called internally by Corona. You never actually call them in your code:

scene.show = function(self, event) print(event.phase) end

In this case, the function seems to run by itself because Corona actually calls it behind the scenes (pun intended, heh heh heh). It doesn’t, however; somewhere back in the depths of the Corona engine code, you’ll find the line (or the equivalent in C => Lua).

scene:show(event)

I recommend that you get very comfortable with Lua (and programming in general) before diving into Corona. Without an understanding of the language Corona is built on, you won’t be very productive with Corona itself. Corona provides a pretty good tutorial on Lua here: http://docs.coronalabs.com/guide/start/introLua/index.html.

Bottom line is, if you don’t call it, it won’t run. So, if a function ends up running, somebody somewhere called it. If you didn’t call it, it must have been Corona. Or maybe some tiny (tiny, tiny, tiny) tapir who lives inside your processor.

[EDIT:] Function notation changed in response to comment from @thomas6.

  • Caleb

I actually prefer this notation because it is clearer to me:

local mySuperFunction = function() print("This text prints when the function is executed") end

This just tells Corona that the local variable called mySuperFunction is a function that prints some text. It is like describing a process.

But describing a process is not the same as DOING a process, as executing it. If you want to execute a process, you don’t describe it, you just sort of order Corona to do it, like this:

mySuperFunction() -- this tells Lua (or Corona) to perform the function called mySuperFunction

May I ask, what type of app are you trying to write? Maybe we can help!

Josh,

In the last example you posted, the reason you did not have to call that function  ‘clearAnswerEvent()’, like the first example ‘hellowWorld()’, is because this second example you posted appears to be called by an event-listener.  

That ‘clearAnswerEvent’ function you show in your example is handled by a line of code you have somewhere in your code. 

Somewhere in your code you have something like   '… addEventListener(“touch”, clearAnswerEvent)   

That event listener code takes care of calling the function for you… this is all handled behind the scenes by Corona’s code.

One thing that might help is to search the internet for some basic lua tutorial examples.  Try to look at a lot of examples of code and get a good understanding of tables and functions in lua, and that will help a lot.

Good luck.

sorry, I see now Caleb covers this same information.

Yeah that´s true…I´ve got to do that. But I understand.