problem with local variable and table

Hi,
 
I’m having the following problem:
 
I have a variable “nav1frequency” which takes his value from the concatenation of two values from two tables. I put this variable in a function. I say: if the variable=11590 then nav1=baliza. Then inside the fuction I put a print to check if it is working properly. The “nav1frequency” changes properly when i change the counters, but nav1 always remain nil (even though 11590 and 11230 are displayed in “nav1frequency”.
 
Here is the code:
 

local nav1 local function n1f() nav1frequency=t1[counter1]..t2[counter2] if nav1frequency==11590 then nav1=baliza end if nav1frequency==11230 then nav1=baliza2 end print(nav1frequency,nav1) return n1f end Runtime:addEventListener("enterFrame", n1f)  

Thanks in advance

What are baliza and baliza2? Are these variables that have been set earlier in the code?

If you just want nav1 to be called “baliza”, you must enclose the word in quotes as I have done there to indicate it is a string. Otherwise it will make nav = baliza, and since baliza does not exist as a variable and is nil, nav will be nil also.

Thanks for the pront reply.

I see your point. Actually baliza and baliza2 are two display objects (variables already defined in the code). I want to get the distance to those two points, and I want the distance returned depending on which frequency is selected. 11590 for baliza, 11230 for baliza2. If the user changes the frequency, it will be selecting wich point to use as a reference, baliza or baliza2. 

There’s a function further on the code wich calculates distance to variable nav1. Nav1 has to be equal to either baliza, baliza2 or nil. 

 

The botton line is that when 11590 is selected nav1 has to be equal to baliza(reference that display object), when 11230 is selected, nav1 has to be equal to baliza2.

 

One thing though, if I set the value of nav1 to baliza or baliza2 when I declare the variable, the code works fine, but the print function show : “10800 (nav1frequency) table: 02BD56E0” , instead of the expected “10800 baliza”

This is the code of this particular case:

 

local nav1=baliza local function n1f() nav1frequency=t1[counter1]..t2[counter2] if nav1frequency==11590 then nav1="baliza" end if nav1frequency==11230 then nav1="baliza2" end print(nav1frequency,nav1) return n1f end Runtime:addEventListener("enterFrame", n1f)

The print statement shows a table because it can only print the contents of a variable. So when you say

local nav1=baliza

…the newest variable (nav1) is pointing to the same thing as the older variable (baliza1), which is a table (in this case, a displayObject, which is basically a fancy table) There’s no way to actually print a variable name short of faking it, afaik.

Thanks Richard,

But we are drifting off topic. The real problem is that the IF statement is not working, to prove the point I coded the following:

local nav1 local function n1f() nav1frequency=t1[counter1]..t2[counter2] if nav1frequency==11590 then nav1="chocolate" end if nav1frequency==11230 then nav1="vainilla" end print(nav1frequency,nav1) return n1f end Runtime:addEventListener("enterFrame", n1f)

And, when NAV1FREQUENCY is set to 11590, nav1 is still nill, the same with 11230.

What is wrong with the IF statement? or is the N1F function the problem?

I have something crossing my mind, that maybe, for corona (or lua), 115"concatenated to"90 is not the same as 11590. Is that possible?

The problem is the concatenation, is there no way to solve it? 

I modified the code as follows and it works fine (but is a pain in the ass to code):

local nav1 local function n1f() nav1frequency=t1[counter1]..t2[counter2] if nav1frequency==t1[8]..t2[19] then nav1=baliza end if nav1frequency==t1[5]..t2[7] then nav1=baliza2 end print(nav1frequency,nav1) return n1f end Runtime:addEventListener("enterFrame", n1f)

It is more complicated to me to code this because I have to calculate the concatenated value’s place in the respective table instead of just placing the frequency number. Any ideas?

Change 11590 to “11590” and 11230 to “11230” in your code.

That did it, thanks a lot Thodah

glad I could help :slight_smile:

What are baliza and baliza2? Are these variables that have been set earlier in the code?

If you just want nav1 to be called “baliza”, you must enclose the word in quotes as I have done there to indicate it is a string. Otherwise it will make nav = baliza, and since baliza does not exist as a variable and is nil, nav will be nil also.

Thanks for the pront reply.

I see your point. Actually baliza and baliza2 are two display objects (variables already defined in the code). I want to get the distance to those two points, and I want the distance returned depending on which frequency is selected. 11590 for baliza, 11230 for baliza2. If the user changes the frequency, it will be selecting wich point to use as a reference, baliza or baliza2. 

There’s a function further on the code wich calculates distance to variable nav1. Nav1 has to be equal to either baliza, baliza2 or nil. 

 

The botton line is that when 11590 is selected nav1 has to be equal to baliza(reference that display object), when 11230 is selected, nav1 has to be equal to baliza2.

 

One thing though, if I set the value of nav1 to baliza or baliza2 when I declare the variable, the code works fine, but the print function show : “10800 (nav1frequency) table: 02BD56E0” , instead of the expected “10800 baliza”

This is the code of this particular case:

 

local nav1=baliza local function n1f() nav1frequency=t1[counter1]..t2[counter2] if nav1frequency==11590 then nav1="baliza" end if nav1frequency==11230 then nav1="baliza2" end print(nav1frequency,nav1) return n1f end Runtime:addEventListener("enterFrame", n1f)

The print statement shows a table because it can only print the contents of a variable. So when you say

local nav1=baliza

…the newest variable (nav1) is pointing to the same thing as the older variable (baliza1), which is a table (in this case, a displayObject, which is basically a fancy table) There’s no way to actually print a variable name short of faking it, afaik.

Thanks Richard,

But we are drifting off topic. The real problem is that the IF statement is not working, to prove the point I coded the following:

local nav1 local function n1f() nav1frequency=t1[counter1]..t2[counter2] if nav1frequency==11590 then nav1="chocolate" end if nav1frequency==11230 then nav1="vainilla" end print(nav1frequency,nav1) return n1f end Runtime:addEventListener("enterFrame", n1f)

And, when NAV1FREQUENCY is set to 11590, nav1 is still nill, the same with 11230.

What is wrong with the IF statement? or is the N1F function the problem?

I have something crossing my mind, that maybe, for corona (or lua), 115"concatenated to"90 is not the same as 11590. Is that possible?

The problem is the concatenation, is there no way to solve it? 

I modified the code as follows and it works fine (but is a pain in the ass to code):

local nav1 local function n1f() nav1frequency=t1[counter1]..t2[counter2] if nav1frequency==t1[8]..t2[19] then nav1=baliza end if nav1frequency==t1[5]..t2[7] then nav1=baliza2 end print(nav1frequency,nav1) return n1f end Runtime:addEventListener("enterFrame", n1f)

It is more complicated to me to code this because I have to calculate the concatenated value’s place in the respective table instead of just placing the frequency number. Any ideas?

Change 11590 to “11590” and 11230 to “11230” in your code.

That did it, thanks a lot Thodah

glad I could help :slight_smile: