Categories
Uncategorized

OpenSCAD Local / Modules Variables

You learn something new every day… if you’re lucky!

I will start with the disclaimer that I’ve never held the title of “Software Developer” or even “Programmer” and my career has been built on being a “Hacker with Many Hats” kind of person. So I tend to go broad and not go deep on a lot of things. I’ll confess, I’ve used a fair amount of global variables in my code, probably some where I should not have. As an old-timer Perl Hacker, it’s… meh. My code was often quick & dirty to get the job done, not elegant and meant to last.

That said, even though I read an entire book on OpenSCAD I somehow had it in my mind that variables in OpenSCAD could not be changed during run-time, as it were. OpenSCAD is… weird, but I’m okay with that. Supposedly variables in OpenSCAD should be thought of as “override-able constants” rather than as variables in the traditional sense.

Also, I’ll admit that often I find examples of code either poorly written or too complex for me to immediately grasp. (I’ll let you decide which is which.) Here, you can read about scope in OpenSCAD User Manual/User-Defined Functions and Modules and OpenSCAD User Manual/General.

Or you can look at my example above… Because yes, you can change the value of a variable in a function / module! Variables are local within a module!

Here’s the demo code shown above. I am guessing 98% of software developers are saying “Duh!” right now… so this post is for me, and people like me, who may have missed this, may have been confused by this, or just needed a simple demo.

1 reply on “OpenSCAD Local / Modules Variables”

I did not realize OpenSCAD supported variables in this way!!! I was taking a break from a design while catching up on your block when I came across this. I normally work around the variable problem by passing variables into a function – but this is good to know.

Leave a Reply

Your email address will not be published. Required fields are marked *