Official SMOD2D Developer Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Official SMOD2D Developer Forum

We've moved FORUMS. Also, do remember that the MOD is dead but we're working on the REAL GAME NOW.
 
HomePortalLatest imagesSearchRegisterLog in

 

 Core 2.0.5 Tutorial

Go down 
AuthorMessage
Dark Byte

Dark Byte


Posts : 88
Join date : 2010-06-05
Location : USA

Core 2.0.5 Tutorial Empty
PostSubject: Core 2.0.5 Tutorial   Core 2.0.5 Tutorial Icon_minitimeMon Jul 26, 2010 5:20 am

*** Core is in early developement and may change at any time ***


This tutorial will teach you the SMOD:2D scripting language, Core.

Like a Star @ heaven 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.

Like a Star @ heaven 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

Like a Star @ heaven Comments
Comments begin with //

Like a Star @ heaven 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

Like a Star @ heaven 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.

Like a Star @ heaven 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)

Like a Star @ heaven 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


Like a Star @ heaven Deleting variables
To delete a variable use the FREE
Code:

a = 0x10 // a = 16 (hex conversion)
FREE a // a no longer exists

Like a Star @ heaven 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"

Like a Star @ heaven 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
Back to top Go down
 
Core 2.0.5 Tutorial
Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
Official SMOD2D Developer Forum :: SMOD2D Project Related :: SMOD2D General-
Jump to: