attempt to index global 'display' <a fimction value> (display.newLine)

newbie question - why would a simple:

local a=display.newLine(0,240,160,240)

throw up the following error:

attempt to index global ‘display’

? If I move the command BEFORE my functions it works. If I place it after my functions, it doesn’t ?
[import]uid: 97524 topic_id: 19074 reply_id: 319074[/import]

Post plug and play, please? :slight_smile: [import]uid: 52491 topic_id: 19074 reply_id: 73688[/import]

peach, this is weird. I’m a newbie with Lua and am wondering where I place the functions in my code and where I place the rest.

For example if I place the

local star = display.newLine(0,220,100,220) BEFORE my functions it works

if I place the line above AFTER my functions it doesn’t work. I’m not calling any of my functions, and I’ve commented the rest of my code out out.

Do I take it, functions should be placed at the end of my main.lua ? [import]uid: 97524 topic_id: 19074 reply_id: 73762[/import]

Hey again,

No, functions don’t have to be placed at the end of your main.lua file.

How many lines of code have you got in your file?

If it isn’t ridiculously long I can take a look and try to see what is up for you - there shouldn’t be any issue regardless of where you place that line of code unless something else is interfering somehow.

Peach :slight_smile: [import]uid: 52491 topic_id: 19074 reply_id: 73859[/import]

here you go Peach. Like I say I’m only just learning Lua now, so it’s probably really obvious! I tried to pin point the line where this error appears, so worked back over and it begins with this bit of code.

The error I get is "attempt to call gloabl “bytes_to_s16”

-- open file  
mypath = system.pathForFile( "file.raw", system.ResourceDirectory )  
f = assert(io.open(mypath, "rb"))  
  
while true do  
 --read 2 bytes of file at a time  
 local bytes = f:read(2)  
 if not bytes then break end  
 local high\_byte, low\_byte = string.byte(bytes, 1, 2)  
  
 --convert the 2 bytes  
 sample = bytes\_to\_s16 (low\_byte,high\_byte)  
  
 -- save  
 table.insert(samples\_s16, sample)   
end  
  
-- close the file  
io.close (f)  
  
-- convert  
function bytes\_to\_s16 (hb, lb)  
 local u16 = hb \* 256 + lb  
 return (hb \> 127) and (u16 - 65536) or u16  
end  

The file is 350KB incidentally.

Is this error simply because the function always has to go first ?

Beacuse if I place the function in a file say “myFunctions.lua” and require it at the beginning of the program it works fine.

BUT, let’s forget that code above and instead if I move to the bigger version of my program, and I move all six of my functins into a separate “myFunctions.lua” file. In my main program all I have is the require “myFunctions” and a single line:

local x = display.newLine(0,240,200,240)

I get the error "attempt to index global ‘display’ (a function value)

Remeber, I’m not calling any of those functions, so why is this error appearing ?

[import]uid: 97524 topic_id: 19074 reply_id: 73935[/import]

Arrrgggghhh!!! I’ve sorted it!

Had my own function called display() and it was messing with Corona.

I’m an idiot! (but learning all the while)

Thanks Peach [import]uid: 97524 topic_id: 19074 reply_id: 73940[/import]

Haha, you aren’t an idiot - you’re just still learning :slight_smile:

Also careful when you post, if you do a "
Peach :slight_smile: [import]uid: 52491 topic_id: 19074 reply_id: 74112[/import]