When writing a script, output helps you see what the script is doing.
Use output to:
The most useful function is `print(…)`.
Use `print(…)` to write a line of output.
print('Probing started'); print('Tool change finished');
`print(…)` writes a newline after the values.
Pass more than one value to `print(…)` when you want a label and a value.
tool = 3; diameter = 6; print('tool ', tool); print('diameter ', diameter);
Labels make output easier to read than printing only the value.
Good:
print('safe_z ', safe_z);
Harder to understand later:
print(safe_z);
When a calculation is wrong, print the intermediate values.
diameter = 6; radius = diameter / 2; area = pi() * radius ** 2; print('diameter ', diameter); print('radius ', radius); print('area ', area);
This helps you find which value became wrong first.
Add prints inside branches while testing logic.
probe_error = 0.07; if(probe_error <= 0.05) { print('branch: ok'); } else { print('branch: warning'); };
When the logic is correct, remove the extra debug prints or keep only useful status messages.
Print counters and important values inside loops.
total = 0; for(i = 1; i <= 3; i += 1) { total += i; print('i ', i, ', total ', total); }; print('final total ', total);
Loop output is useful, but too much output can become hard to read. For long loops, print only important events or every few iterations.
Print values at the start and end of a function when debugging.
function FeedForMaterial(material) { print('FeedForMaterial material=', material); if(material == 'aluminum') { print('FeedForMaterial result=900'); return 900; }; if(material == 'steel') { print('FeedForMaterial result=300'); return 300; }; print('FeedForMaterial result=500'); return 500; } feed = FeedForMaterial('steel');
This is useful while writing the function. After the function works, remove prints that are only for debugging.
Use `sprint(…)` when you want to build a text value without writing output.
tool = 3; diameter = 6; message = sprint('Tool ', tool, ', diameter ', diameter); message;
This is useful when the text should be returned, stored, shown in a dialog, or passed to another function.
Use `sprintf(…)` to build formatted text with `{}` placeholders.
x = 10; y = 20; message = sprintf('X={}, Y={}', x, y); message;
The final result is `'X=10, Y=20'`.
Use `printf(…)` when you want the formatted text printed directly.
printf('X={}, Y={}', x, y);
For simple output, `print(…)` is usually enough.
Use `error(…)` when the script should stop because something is wrong.
tool = 0; if(tool <= 0) { error('Invalid tool number: ', tool); }; print('this line is not reached when tool is invalid');
A clear error message is better than allowing the script to continue with invalid data.
`clear()` clears console output when the host output window supports it.
clear(); print('Starting test');
Use it at the start of small tests when old output would be confusing.
In MDI, start with `=` to evaluate Expr.
=diameter=6; radius=diameter/2; print('diameter ', diameter); print('radius ', radius); radius;
This prints the intermediate values and returns the final value.
When a script does not work, reduce the problem.
Instead of testing the whole script at once, test small pieces:
=diameter=6; diameter/2;
Then add the next step:
=diameter=6; radius=diameter/2; area=pi()*radius**2; area;
Then add conditions, loops, functions, or object code after the basic values are correct.
print(a); print(b); print(c);
This becomes hard to read. Prefer labels:
print('a ', a); print('b ', b); print('c ', c);
Debug prints are useful while writing a script. Too many debug prints can make normal script output noisy.
Keep prints that are useful to the operator. Remove prints that were only used to find a bug.
If a required value is missing or invalid, stop early with `error(…)`.
if(tool_diameter.is_none()) { error('Tool diameter is missing'); };
`print(…)` writes output.
`sprint(…)` builds and returns a string.
print('hello'); // writes output text = sprint('hello'); // stores text
In MDI, try:
=clear(); material='steel'; feed = if(material == 'steel') { 300; } else { 600; }; print('material ', material); print('feed ', feed); feed;
The final result is `300`.