Gw Temp

Menu

Tutorial - 'Sphere: The basics of scripting' by polski

An item about Sphere posted on

Blurb

An introduction to scripting in Sphere, learn the basics of functions and how they can be used to make your life easier.

Body

So you've just got into Sphere but have no clue how to script in it? Well then this article should get you started! In it I will discuss the main function of every game, creating your own functions and linking functions to maps.

First thing you're going to want to do is create a new script, go to the File Menu, Click on New and Select Script, now you have a blank window so let's type something in.

function game() {
// This is our main function
}

This "game" function is the main function, it is the first function to be executed on the games launch and without one you will get errors. To create your own function you simply do something like so:

function my_function(variables_to_pass_through) {
// Code here
}

Now that may look a bit confusing so allow me to expliain. First we are creating a function called "my_function" the name can be anything you want but it is best to be descriptive, an example of a good function name would be "Trig_Treasure12D" So why did I call my function that? Because first it's a Trigger on a map, It's a Treasure Chest Trigger, Located at tile 1,2 (X,Y Coordinates) and is in the Dungeon map, D for dungeon. Function naming is an essential skill, it will help you in the long run. Moving on now, You see the word "Variables_to_pass_through" in the brackets, this is just if you want to pass through any variables for that function to use, more into that in a second!

So now we have created a function but how do we call it? Calling a function is as easy as...pie (I guess) simply type the following:

my_function();

Replacing my_function with the actual name of your function. Now that you know the true basics, let's create a fully working function with code. Let's start with declaring the function:

function message(text);
{
GetSystemFont().drawTextString(0,0,text);
FlipScreen(); // This just has to be used since we are drawing something
}

Woah! Shall I slow down there? What I did here was create a function called message and it accepts a variable that it will know as "text", It then writes whatever you set as text to the screen at coordinates 0,0. To call this function we do:

message("Hello, Sphere is cool eh?");

This passes the text string "Hello, Sphere is cool eh?" into the function and the function then displays the passed string onto the screen.

Alright, I've went through the basics, taught you how to create your own functions and call them so I guess you're now all set to start some scripting! I will leave you with this tiny game code basically putting all we learned in this tutorial into one big script

/*
*******************************
* *
*Script file: myfirstgame.js *
* *
*******************************
*/

function game() {
intro(); // We call the intro for our game
}

function intro() {
// We now call the message function and display our text at the
// coordinates 100,100 (x,y)
message("This is my crappy intro with no pictures..",100,100);
// After that we may want to load a map or something but that
// will be looked at in another tutorial
}

// We don't always want to keep on typing this huge command to
// put text to the screen so we create simple message function
// to do it for us!

function message(text,x,y) {
GetSystemFont().drawTextString(x,y,text);
}

// End of file

Any questions, suggestions or feedback can be posted below using the comment system.