Difference between revisions of "Misc Functions"

From SolarStrike wiki
Jump to: navigation, search
(getTime)
(aLLzLQttJCoTmtCkUR)
Line 1: Line 1:
== args ==
+
IJWTS wow! Why can't I think of thngis like that?
 
 
The 'args' table holds command-line-like data that is passed to the script when it is executed.
 
 
 
The first argument (args[1]) will always be the script name as typed by the user, while the rest of the arguments are tokenized by spaces. That is, if you run the script "test.lua testing 123", args[1] contains the script name ("test.lua"), args[2] contains "testing" and args[3] contains the number 123.
 
 
 
 
 
'''Example'''
 
<source lang="lua">
 
-- Assume the user typed in: script.lua something
 
if( args[2] ) then
 
  -- They have added an argument to the script call.
 
  if( args[2] == "something" ) then
 
    -- The user wants us to do something!
 
    do_something();
 
  end
 
end
 
</source>
 
 
 
== rest ==
 
'''rest(time)'''
 
 
 
Causes the macro to pause for some 'time' milliseconds (1/1000th of a second).
 
 
 
 
 
'''Example'''
 
<source lang="lua">
 
rest( 1000 ); -- 1 second will elapse before continuing.
 
</source>
 
 
 
 
 
 
 
== setPriority ==
 
'''setPriority(priority)'''
 
 
 
Sets the macro interpreter's priority. You should pass in one of following variables: priority.high, priority.normal, or priority.low. Use this to overcome processes fighting over timeslice. If you don't understand what this does, you probably won't need it.
 
 
 
 
 
'''Example'''
 
<source lang="lua">
 
setPriority(priority.high);
 
</source>
 
 
 
 
 
== logMessage ==
 
'''logMessage(message)'''
 
 
 
Adds a formatted log message (string only) to the log file. Follows the format: "{time} : {message}\n", where {message} is a string containing the text you want logged. The newline marker is automatically appended.
 
 
 
If you would like to format your own messages, see [[Misc_Functions#logRaw|logRaw]]().
 
 
 
 
 
'''Example'''
 
<source lang="lua">
 
logMessage("This is an example logged message.");
 
</source>
 
 
 
 
 
This may at first seem restrictive as it only allows you to input a single string. If you would like to place strings, numbers, or other datas into the string that you want to log, you simply need to format it first using [[Library#sprintf|sprintf]]() or other string-formatting functions.
 
 
 
'''Example'''
 
<source lang="lua">
 
local myStr = sprintf("PI: %0.4f", 3.1415926);
 
logMessage(myStr);
 
</source>
 
 
 
== logRaw ==
 
'''logRaw(message)'''
 
 
 
Adds a message (string or number) directly to the log file without formatting. You may use this function to format your own messages.
 
 
 
 
 
'''Example'''
 
<source lang="lua">
 
logRaw("My favorite number is ");
 
logRaw(12);
 
logRaw("\n");
 
</source>
 
 
 
 
 
== getVersion ==
 
'''getVersion()'''
 
 
 
Returns MicroMacro's version number as an integer. A value of 100 would be equivilent to version 1.0, or a value of 95 would be equivilent to version 0.95. This can be used to check whether or not an end user's version of MicroMacro supports functions in a script you are designing. You can use division and modulo to separate the major and minor version numbers from the value returned, as done in the example.
 
 
 
 
 
'''Example'''
 
<source lang="lua">
 
version = getVersion();
 
major = math.floor(version / 100);
 
minor = version % 100;
 
io.write("MicroMacro v", major, ".", minor, "\n");
 
 
 
-- This will print (assume we are using micromacro 1.0):
 
-- MicroMacro v1.0
 
</source>
 
 
 
 
 
== setTextColor ==
 
'''setTextColor(cli.col)'''
 
 
 
Sets the color for output text. This will remain into effect until the text color is changed again using setTextColor, so you should probably use [[Library#cprintf|cprintf]]() to make your life easier. 'cli.col' must be an option from the cli module (lib/mods/cli.lua), and you may not use [[Process_Functions#makeColor|makeColor]]().
 
 
 
cli.lightgray is the default text color. If you decide to use this function, you should always return back to cli.lightgray when you are done outputting different colored text.
 
 
 
'''Example'''
 
<source lang="lua">
 
setTextColor(cli.green);
 
printf("Green text!\n");
 
setTextColor(cli.lightgray);
 
</source>
 
 
 
 
 
== showWarnings ==
 
'''showWarnings(bool)'''
 
 
 
This function enables or disables the showing of warnings such as memory read/write failures. Warnings should only be disabled on near-complete projects that have extensive error checking in place. By default, showing warnings is turned on (true).
 
 
 
'''Example'''
 
<source lang="lua">
 
local err;
 
showWarnings(false); -- hide warnings.
 
err = memoryWriteInt(proc, location, value);
 
showWarnings(true); -- turn them back on.
 
 
 
if( err ) then
 
  printf("Some error occured.\n");
 
end
 
</source>
 
 
 
 
 
== bitAnd ==
 
'''number bitAnd(val1, val2)'''
 
 
 
Applies a binary 'and' (which allows you to check if a given value is 'on' in a flag set) to the given values and returns the result as a boolean.
 
 
 
'''Example'''
 
<source lang="lua">
 
local FLAG_ONE = 0x1
 
local FLAG_TWO = 0x2
 
local FLAG_THREE = 0x4
 
local FLAG_FOUR = 0x8
 
 
 
local flagSet = bitOr(FLAG_ONE, FLAG_TWO); -- flagSet contains both FLAG_ONE and FLAG_TWO now
 
 
 
if( bitAnd(flagSet, FLAG_ONE) ) then -- Check if flagSet contains FLAG_ONE
 
  printf("flagSet contains FLAG_ONE\n");
 
end
 
</source>
 
 
 
 
 
== bitOr ==
 
'''number bitOr(val1, val2, ...)'''
 
 
 
Combines all the given values into a single flag. Use [[Misc_Functions#bitAnd|bitAnd]]() to check a flag set for a given value.
 
 
 
'''Example'''
 
<source lang="lua">
 
local FLAG_ONE = 0x1
 
local FLAG_TWO = 0x2
 
local FLAG_THREE = 0x4
 
local FLAG_FOUR = 0x8
 
 
 
local flagSet = bitOr(FLAG_ONE, FLAG_TWO, FLAG_FOUR);
 
-- flagSet contains FLAG_ONE, FLAG_TWO, and FLAG_FOUR, but not FLAG_THREE.
 
</source>
 
 
 
== bitLShift ==
 
'''number bitLShift(val, amt)'''
 
 
 
Applies a binary left shift on 'val' by 'amt' amount.
 
 
 
'''Example'''
 
<source lang="lua">
 
local newVal = bitLShift(0x10, 4);
 
-- newVal should now contain 0x100, or 256
 
</source>
 
 
 
 
 
== bitRShift ==
 
'''number bitRShift(val, amt)'''
 
 
 
Applies a binary right shift on 'val' by 'amt' amount.
 
 
 
'''Example'''
 
<source lang="lua">
 
local newVal = bitRShift(0x100, 8);
 
-- newVal should now contain 0x1, or 1
 
</source>
 
 
 
 
 
== getTime ==
 
'''table getTime()'''
 
 
 
Returns a high-resolution timestamp of the current time on the user's computer. This should be used in combination with [[Misc_Functions#deltaTime|deltaTime]]().
 
 
 
'''Example:'''
 
<source lang="lua">
 
local now = getTime();
 
</source>
 
 
 
 
 
== getTimerFrequency ==
 
'''table getTimerFrequency()'''
 
 
 
Returns a 64-byte integer (represented as a table) detailing how many processor cycles occur in a second. This varies across processors, and is generally not of much use to the average user.
 
 
 
<source lang="lua">
 
local freq = getTimerFrequency()
 
</source>
 
 
 
== deltaTime ==
 
'''number deltaTime(T2, T1)'''
 
 
 
Returns a floating-point integer that gives a precise amount of time, in milliseconds, that have elapsed between two timestamps. T2 should represent a 'newer' time than T1, such that T2 - T1 would be a positive value.
 
 
 
'''Example:'''
 
<source lang="lua">
 
local startTime = getTime();
 
yrest(1000);
 
local endTime = getTime();
 
 
 
printf("%d milliseconds have elapsed.", deltaTime(endTime, startTime));
 
-- The value displayed should be roughly 1,000.
 
</source>
 

Revision as of 04:43, 21 April 2011

IJWTS wow! Why can't I think of thngis like that?