GetEnv$ ( env_var$ )
Parameters
| env_var$ - name of the environment variable to read |
Description
|
Reads an environment variable and returns its value as a string. Environment variables are the little name/value pairs Windows hands to every program it starts - USERNAME, TEMP, OS, PATH and so on. GetEnv fetches one by name. Because the value is just a string, it is the easiest way for a game to pick up something from outside itself without inventing a config file format. Typical uses: find the user's temp folder for a scratch file, read a launcher-supplied setting such as a chosen server address, or check for your own debug switch so a build behaves differently on your machine than on a player's. Together with SetEnv it also gives you a channel to a helper program you start with ExecFile - set a variable, launch the tool, and the tool inherits it. A variable that does not exist comes back as an empty string, not as an error, so test with Len() or compare against "" rather than expecting a failure. Names are not case sensitive on Windows. The value is returned exactly as stored, including any spaces or trailing separators - trim it yourself if that matters. For engine and machine details that are not in the environment - the application directory, the OS name, the CPU, the Direct3D 12 device - use SystemProperty instead. For the arguments your program was started with, use CommandLine. See also: SetEnv, SystemProperty, CommandLine, ExecFile. |
Example
; GetEnv Example ; -------------- ; GetEnv reads a variable out of this program's environment block. ; Windows sets these three for every process, so they always exist. Print "USERNAME = "+GetEnv$("USERNAME") Print "OS = "+GetEnv$("OS") Print "CPU count = "+GetEnv$("NUMBER_OF_PROCESSORS") Print "" ; A variable that is not set comes back as an empty string rather ; than an error, so Len() is the way to test for "not there" missing$=GetEnv$("BLITZ3D_NO_SUCH_VARIABLE") Print "Unset variable returned "+Len(missing)+" characters" Print "" ; SetEnv writes one and GetEnv reads it straight back - handy for ; passing a level name or difficulty on to a program you ExecFile SetEnv "BLITZ3D_DEMO_LEVEL","forest_ruins" Print "After SetEnv: "+GetEnv$("BLITZ3D_DEMO_LEVEL") ; Tidy up - an empty value removes the variable again SetEnv "BLITZ3D_DEMO_LEVEL","" Print "After clearing: ["+GetEnv$("BLITZ3D_DEMO_LEVEL")+"]" Print "" Print "Press any key to close the example" WaitKey End
Index