Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] - Basic Numeric String Formatting

Author
Message
Chris Tate
DBPro Master
14
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 22nd Nov 2011 18:21 Edited at: 21st Jan 2012 14:08
Shorten your source code by a few thousand characters using concise string formatting functions instead of lengthy concatenation assignments. Works well with user interfaces and error message boxes.

Code is self explanitory and can be easily modified.

Def$( "", "Default string" ) // returns "Default string"
Def$( "1", "2" ) // returns "1"
Def$( a$, b$ ) // returns the value of b$ if a$ is ""; otherwise the value of a$ is returned

Numbers sometimes represent things specified by the user, or is dynamically set to mean something by a script, file or macro. DefStr$ functions prefix the meaning of the number if there is a meaning, otherwise a default meaning is placed to the left of the number. A certain amount of digits are also to be specified in the last parameter.

DefStr$( "", "Item Cost: £", 123.3230, 2 ) // returns "Item Cost: £123.32"
DefStr$( a$, "Item Cost: £", 123.3230, 2 ) // returns "Item Cost: £123.32"
DefStr$( "Bag Cost: £", "Item Cost: £", 123.3230, 2 ) // outputs "Bag Cost: £123.32"
iEntityCount = 2 : DefStr$( a$, "Unknown entity count: ", iEntityCount, 0 ) // returns "Unknown entity count: 2"
type$ = "Box size: " : DefStr$( type$, "Unknown entity count: ", iEntityCount, 1 ) // returns "Box size: 2.0"

Without needing to join numerous Str$(), number to string conversions using the plus sign, you need only write one function call with the number of parameters you have. Str2$() will join 2 numbers with 2 strings, Str3$() will do the same with three pairs; and DecStr3$() is the equivalent which will also place a certain amount of decimal strings on each number.

Str3$( "Location X: ", 1, " | Y: ", 10, " | Z: ", 0 ) // returns "Location X: 1 | Y: 10 | Z: 0"
DecStr3$( "Location X: ", 1, " | Y: ", 10, " | Z: ", 0, 2 ) // returns "Location X: 1.00 | Y: 10.00 | Z: 0.00"

When a number represents a change in form, quantity or behaviour; a + or - sign can be used to reflect the change, when numbers below zero indicate a reduction, and vise verse, numbers above zero indicate addition.

PosNegStr$( "Bonus: ", 12.013, 2) // returns "Bonus: +12.01"
PosNegStr$( "Bonus: ", -22.23, 1) // returns "Bonus: -12.2"
PosNegStr$( "Bonus: ", 0, 1) // returns "Value bonus: 0.0"


If desired modify the following to work with your own message box functions



Add spaces inbetween formatted value strings using the following:



More snippets

Chris Tate
DBPro Master
14
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 20th Jan 2012 20:24 Edited at: 21st Jan 2012 14:14
A few more small functions used often in large functions to go with the ones above:

CSV$() Functions will return a vector string out of a number of values:

CSV3$( 100, 200, 300, 0 ) = "100,200,300"
CSV3$( 100, 200, 300, 2 ) = "100.00,200.00,300.00"

The functions ending with underscore $ (_$) will place a space after each comma.

CSV2_$( 100, 200 ) = "100, 200"


When keeping track of where 3d objects are, you often need to convert their location into a formatted string. Something along the lines of: Text x, y, Str$(Object Position X(id), 2) + "," + Str$(Object Position Y(id), 2) + "," + Str$(Object Position Z(id),2). This is quite lengthy for something rather simple; and lengthy code tends to increase the chances of typing errors occuring. The following functions are shortcuts for this action. Text X, Y, ObjPos$(id,2) is all that would have needed to be written.

If an object's position is at 10,10,10; the following will be so

ObjPos$( id , 0) = "10,10,10"
ObjPos$( id , 3) = "10.000,10.000,10.000"

The same is true for CamPos$() for cameras and LimbPos$() for limbs. Angle functions are also available in the snippet.

Print CamPos$( id, 0 ) // Outputs camera position with no decimals
Print LimbDirection$( object, limb, 1 ) // Outputs limb direction with 1 decimal place
Print ObjAng$( object, 0 ) // Outputs the angle of the object with no decimal places

If any of the following variables are true, "Yes" is returned, otherwise, "No" is returned.

YesNo$( 1 ) = "Yes"
YesNo$( 1 > 40 ) = "No"
YesNo$( Object Position Y(Id) = 10 ) = "Yes"
YesNo$( "a" = "b" ) = "No"
YesNo$( "Egg" = "Egg" ) = "Yes"
YesNo$( Array Count( Boxes() ) = Array Count( Boxes() ) ) = "Yes"
YesNo$( YesNo$( 1 ) = "Yes" ) = "Yes"
Print "Is a key pressed? > "; YesNo$( Scancode() ) // outputs "Is a key pressed? > Yes" if a key is pressed

The same is true with the TrueFalse$() Function

TrueFalse$( 10 ) = "True"
TrueFalse$( 10 = 5 ) = "False"

The same is also true with the Result$() Function; the difference is the fact that you must specify what the true and false return strings are. These strings can be messages for the user, parameters for functions or variables for string expressions.

Result$( 10 > 0, "Ok", "Not Ok" ) = "Ok"
Result$( 10 < 5, "Lower", "Higher" ) = "Higher"
Result$( Object Position Y(Id) = 10, "Is Ten", "Is Not Ten" ) = "Is Ten"
Result$( MouseClick() && 1 , "Left Mouse Button Down", "Left Mouse Button Up" ) = "Left Mouse Button Down"
[b]Open To Write FileId, Result$( File Exist( File$ ) , File$, "Default.txt" )
- opens file$ if it exists, otherwise it opens "Default.txt"



Chris Tate
DBPro Master
14
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 20th Jan 2012 20:48 Edited at: 20th Jan 2012 22:18
The keyboard is a primary means of user input, and you may need to let the user know what key they are pressing by its name, rather than its code.

By default, keys such as Left, Right, Home and Insert cannot be returned by name. It is often necessary to store key mappings for users to define the keys by their names. A key control editor program can use the following string functions to display what key is assigned to what control:

Key$( 200 ) = "Up"
Key$( 202 ) = "Down"
Key$( 57 ) = "Spacebar"
Key$( 221 ) = "Context Menu"
Key$( 78 ) = "Numpad +"
Key$( 17 ) = "W"
Key$( 30 ) = "A"
Key$( 31 ) = "S"
Key$( 32 ) = "D"

Print Key$( Scancode() ) // Outputs the name of the last key currently being pressed

Print Scankey$() = // Outputs the same for convinience

Function Code:


Login to post a reply

Server time is: 2022-11-30 13:34:29
Your offset time is: 2022-11-30 13:34:29