2151
Mach4 General Discussion / Re: when to use rc = ?
« on: August 07, 2019, 08:56:19 PM »
Hi,
ideally you would use rc (return code) on EVERY API call.
Lets take a typical Lua API:
hsig,rc=mc.mcSignalGetHandle(inst,mc.OSIG_OPUTPUT23)
This API requires two parameters (or inputs if you prefer the term) namely an instance number and a number identifying the output
signal for which you want a handle. Note that mc.OSIG_OUTPUT23 actually evaluates to a number.....the text string is called an 'enumeration'
and is done that way because humans remember names better than numbers.
This API has two returns, one is the handle we wanted but also a return code.
When the Lua function, in this case mcSignalGetHandle(), is returned it returns values in the form of a stack. The stack is read from the lefthand most
entry.
Thus:
hsig=mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT23) is perfectly OK. The variable hisg will hold the correct value after the call has been executed.
the variable rc will be on the stack but if you don't read it thats OK.
However:
hsig,rc=mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT23) is also perfectly OK. The variable hsig will have the required handle AND rc will have a return code.
Most of us don't bother with return codes and we consequently get a lot of code errors without understanding why. Expert programmers read return codes EVERY
API call and they have far fewer crashes.
I myself are somewhere in between, certain APIs which give trouble I test completely, others I don't bother.
Also if you write code for a newcomer to Lua then including all the extra code just to read return codes often confuses the newcomer which is definitely
not helpful..........but tends to start a bad habit, namely that of not reading return codes.
Craig
ideally you would use rc (return code) on EVERY API call.
Lets take a typical Lua API:
hsig,rc=mc.mcSignalGetHandle(inst,mc.OSIG_OPUTPUT23)
This API requires two parameters (or inputs if you prefer the term) namely an instance number and a number identifying the output
signal for which you want a handle. Note that mc.OSIG_OUTPUT23 actually evaluates to a number.....the text string is called an 'enumeration'
and is done that way because humans remember names better than numbers.
This API has two returns, one is the handle we wanted but also a return code.
When the Lua function, in this case mcSignalGetHandle(), is returned it returns values in the form of a stack. The stack is read from the lefthand most
entry.
Thus:
hsig=mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT23) is perfectly OK. The variable hisg will hold the correct value after the call has been executed.
the variable rc will be on the stack but if you don't read it thats OK.
However:
hsig,rc=mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT23) is also perfectly OK. The variable hsig will have the required handle AND rc will have a return code.
Most of us don't bother with return codes and we consequently get a lot of code errors without understanding why. Expert programmers read return codes EVERY
API call and they have far fewer crashes.
I myself are somewhere in between, certain APIs which give trouble I test completely, others I don't bother.
Also if you write code for a newcomer to Lua then including all the extra code just to read return codes often confuses the newcomer which is definitely
not helpful..........but tends to start a bad habit, namely that of not reading return codes.
Craig