[Resolved] Reserving and Playing an Audio Channel

I have a script where I want to control what channels are played.
see: http://developer.anscamobile.com/forum/2012/03/23/audio-questions-what-channel

But in trying to implement this I get a runtime error I don’t understand.

Here’s a simplified script to just show what I’m trying to do:

 audio.reserveChannels( 1 )  
 Sound1 = audio.loadSound( "HB\_sound\_clip1.mp3" )  
  
function playSound()  
 audio.play( Sound1 {channel = 1} )  
end  
  
playSound()  

The error:

Runtime error: … attempt to call global ‘Sound1’ (a userdata value)

If I take out {channel = 1} it works OK.
Making Sound1 a local variable doesn’t help. I still get a “userdata” error. What does this error mean? Is there any documentation on Corona SDK error messages?
Thanks
Eric

[import]uid: 128346 topic_id: 23847 reply_id: 323847[/import]

You’re missing a comma there, should be one right after Sound1. [import]uid: 52491 topic_id: 23847 reply_id: 96036[/import]

Oh my gosh. Isn’t that classic. The infamous missing comma.
Well here’s the syntax for anyone interested:

audio.play( audioHandle [, { [channel=c] [, loops=l] [, duration=d] [, fadein=f] [, onComplete=o] } ] )

I didn’t see that sneaky little comma after the bracket " [,"

I think once I get more used to the general form of the various methods, the pattern of syntax will get clearer.

Much appreciated.

So will there ever be any docs about errors?

Eric

[import]uid: 128346 topic_id: 23847 reply_id: 96037[/import]

Hey Eric,

Yeah, they can be sneaky little things but as you get used to Lua/Corona you will find that knowing what line an error is on you can just look at it and fix it.

I doubt there will ever be a great amount of documentation on errors simply because errors can be caused by so many different things and really, because over time you learn to read the code and see these errors on your own it isn’t that necessary I don’t think. (It might be helpful in theory but to understand the docs enough to know where to look for the error would only help if you knew how to fix it in our specific case.)

My advice is just to double check API page if you get an error in a line like this.

Or post here of course, we always do our best to help you troubleshoot.

Peach :slight_smile: [import]uid: 52491 topic_id: 23847 reply_id: 96047[/import]

That particular error message you see actually is pass through from Lua itself and is a standard Lua error.

You stumbled on a Lua syntactic sugar thing. You are allowed to omit parenthesis on function calls if there is only a single parameter and it is a table or string literal.

So this:
Sound1 {channel = 1}
can be written as a function call like:
Sound1({channel =1})
So the error means:

“attempt call”
means a Lua was trying to invoke a function call.

"global ‘Sound1’
means Lua was trying to invoke a global function named Sound1 e.g. it expected you to have defined a function like:
function Sound1(…)
– stuff here
end

“(a userdata value)”
Lua failed to invoke a function called Sound1 because the variable ‘Sound1’ is actually a ‘userdata’ type instead of a function type.
Because you forgot the comma, Lua thought you were trying to invoke a function called Sound1() with the table as a parameter.
[import]uid: 7563 topic_id: 23847 reply_id: 96051[/import]

That explain it. Thanks for the detailed response.

I looked up Lua errors on Google, and didn’t turn up much except that the Lua manual says:
“Because Lua is an embedded extension language, all Lua actions start from C code in the host program calling a function from the Lua library (see lua_pcall). Whenever an error occurs during Lua compilation or execution, control returns to C, which can take appropriate measures (such as printing an error message).”
(http://www.lua.org/manual/5.1/manual.html#2.7)

So I thought I could look up C errors, but didn’t have much luck.

Did find this page though, which maybe will be helpful at some point:
http://wiki.roblox.com/index.php/Lua_Errors [import]uid: 128346 topic_id: 23847 reply_id: 96078[/import]