? Limit Variable to Range ● SmileBASIC Source

Sign In

Register
*Usernames are case-sensitive
Forgot my password
Another contest? You asked for it!

Limit Variable to Range

Code like:IF X>maximum THEN X=maximum IF X<minimum THEN X=minimumIs used way too often. It takes up way too much space and looks terrible. Some people have made functions to do this, but there is a simpler way! Instead, you can use:

<variable>=MIN(MAX(<variable>, minimum), maximum)

It's not only short, but also pretty easy to understand! Example: X=MIN(MAX(X,0),400) Y=MIN(MAX(Y,0),240) Limits X and Y to the screen limits. Use this instead of: IF X<0 THEN X=0 IF X>399 THEN X=399 IF Y<0 THEN Y=0 IF Y>239 THEN Y=239 I mean you should probably just have checks when you modify your variables
Author
12Me21
Updated
Rating
3 votes
Categories
Keywords
3 Comment(s) 12Me21 12Me21 Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself I thought I was so clever when I "discovered" this, but it turns out that it's pretty well known in other languages too... randomous randomous Power User Robot Hidden Easter Eggs Second Year My account is over 2 years old Website Drawing I like to draw! Hobbies You can't use italics in a [code] segment. 12Me21 12Me21 Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Fixed.