Safely making a call to a library that may not exist

C Scripting questions and answers
Post Reply
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4586
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Safely making a call to a library that may not exist

Post by ArtF »

Hi All:

  This topic is here for archival purposes... at some point in scripting,
such as can be seen in the GCode libraries, you may wish to make a call
to a function which may or may not exist.

  When one turns on the GCode library, youll find up to now, you would fail
a Gcode run unless you also had a spindle library turned on,  this is because in the
SetSpindleState() library call, which is made by the system when it encounters
an M3 in the file, a call was made to

SetSpindleOn();

  Now if a library, (such as the Spindle-RelayOnly library was turned on, then
it has a function called SetSpindeOn(), so the system would see no error
and the program would run, but if the spindle library (one of them) wasn't
turned on, then no such call for SetSpindleOn exists, and this would cause
a GCode script to fail, thus stopping the run.

  TO fix this , I have added a way to check if a function exists, so this call
should be used if one isnt sure if such a function exists to be called.

  This statement, which you ll find in future versions of the Gcode library
looks like this...

if( lookup("SpindleOff") { SpindleOff(); };

  It first checks to see if there is such a call, if so, it calls it. If not, no error
occurs, and the G code in this case would run , it just wouldn't turn on a spindle,
which is to be expected if the program hasn't a library turned on telling it what a spindle
does. :)

  For expert scripters, a shorter form of that would be

lookup("SetSpindleOn")();

  as the lookup function actually returns the functions pointer  or a NULL, and its legal to
call a NULL function..

So, Just a brief explanation of how to avoid calling a function that doesnt exist. Take note
there are times you may WANT it to error out, as that will stop the Gcode run if an
important even didnt happen as you wish. In which case...as you were...

Thx
Art




Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests