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]