*** Core is in early developement and may change at any time *** This tutorial will teach you the SMOD:2D scripting language, Core.
About
Core is the new SMS (Smod script). It has many new features such as: pointers, Hexadecimal and Octal conversion. It is very simple. It is mainly used to send info to SMOD about maps and files.
Functions have been greatly improved. No longer is there a limit of them they can be added easily through Lua. I will cover that later.
Part 1: Identifiers
I will show you how to create identifiers (or variables). They are declared with =
- Code:
-
a = 8
That will make the variable a with a value of 8.
You can easily make a constant value (one that can not be changed). Just prefix the variable with ~.
- Code:
-
~a = 8
- Code:
-
a = 1
a = 2
a will equal 2
- Code:
-
a = 12
b = a
b equals 12
- Code:
-
value of pi = 3.14159265358979
This is legal, and "value of pi" equals 3.14159265358979
Comments
Comments begin with //
Pointers
First pointers in Core are useless. Well, except for "Global Pointers" which point to identifiers in a different script.
To declare something as a pointer you must have an identifier.
- Code:
-
a = 100
P-> a
P points to a.
- Code:
-
a = 100
P-> a
P = 0
a = 0
Global pointers
Global pointers, as I said, are the only good use for pointers in Core. They can retrieve and modify values from other scripts that have been loaded
before the current.
Lets say we have two scripts they have both been loaded.
script1 - Code:
-
a = 010 // a = 8 (Octal conversion)
script2 - Code:
-
P-> [ 1 : a ]
P = 0xA // a (from the first script is now 10 (Hexadecimal conversion))
The 1 in the brackets is the script ID number and a is the value you are trying to get.
Calling functions
You can call functions with CALL. One of the default functions in Core is printf ( the other defaults are arithmetic functions and one string function).
Here is an example.
- Code:
-
CALL printf( "hello, world" )
Will print
hello, world to the console.
Functions can also be called (or give their returned values to identifiers) bu calling them when setting variable values.
Ex:
- Code:
-
a = printf("hello, world")
Will also write
hello, world, but give a the value 0 (because printf returns nothing)
Arithmetic
The math functions are:
ADD ( v, ... ) // Addition
MULT ( v, ... ) // Multiplication
DIV ( v, ... ) // Division
MOD ( v, ... ) //Modulo
POW (v, ...) // Exponentiation
COS (n) // Get cosine of n
SIN (n) // get sine of n
FAC(n) // factorial of n
Example:
- Code:
-
a = 100
a = ADD( a, -100 ) // a = 0
a = ADD ( a, 5 ) // a = 5
a = POW ( a, 2 ) // a = 25
a = MULT ( a, 4, 2 ) // a = 200 ( a = 25; a * 4 = 100, a * 2 = 200)
a = SIN(1) // a = 0.8414709848079
Deleting variables
To delete a variable use the FREE
- Code:
-
a = 0x10 // a = 16 (hex conversion)
FREE a // a no longer exists
Macros
To format a string use the function
FormatStrn ( string, format...)This commands uses all of Lua's % formats plus any registered macros (see below)
Then returns that string.
Ex
- Code:
-
a = FormatStrn("I am more than %d days old", 5110) // a = "I am more than 5110 days old"
Adding functions from Lua, global variables, and macros
Writing functions is Core is currently impossible, but you can "register" functions from Lua.
- Code:
-
-- This is a Lua file
dofile("Core.luac")
Core.Register(0, "math.random", "rand")
The first parameter,0 , tells Core which script can use the function (0 means a Global function). The second parameter tells Core which function it is, and three gives the Core name of the function.
--
To declare a global variable (a variable that will be given to each script ) use
- Code:
-
-- Lua source
Core.RegisterIdentifier( "name", value, constant )
The first parameter gives the name of the identifier. The second gives the value, and the last (which must be of Boolean value) will tell COre if it is constant or not.
--
To give a special format to strings use the command
Core.Macro(macro, returns)macro = the value that will replaced
returns = what the macrois replaced with
Ex:
- Code:
-
-- Lua source
Core.Macro("bestCS2DMod", "smod:2d")
Now when FormatStrn ( or printf ) is used it will replace as:
- Code:
-
s = FormatStrn("Have you played $bestCS2DMod?") // s = "Have you played smod:2d?"
But if you used
- Code:
-
s = FormatStrn("Have you played bestCS2DMod?")
s = would equal "Have you played bestCS2DMod?"
NOTE: Macros are only detected in strings!
***
Whitespace problems ***
For an unknown reason sometimes Core will not detect a whitespace and replace it with '' (nothing)
To fix this I have added a default macro, S.
Ex:
- Code:
-
strn = FormatStrn("hello,$Sworld")
s would equal:
hello, world