DaVince scripting tutorial/The basics on functions

From Spheriki

Jump to: navigation, search

Contents

The basics on functions

Functions

This tutorial will make sure that you can make your own game in Sphere. A simple, but working game.

A function is a thing you give an identifiable name, put some code in, and then you can run that code any time you reference the function by its name. For example, you can make a function called TextBox that would make a box displaying text appear, and then you can show this text box whenever you like by calling the function TextBox elsewhere. It's very useful to make your code manageable, and it prevents you from doing bad things like copying the same block of code over and over again in different places. The bad thing about doing things like that is: if you make a change to the code you'll need to change it everywhere else, too. If you have a function, you only need to change it once in that function. It also makes your code take up much fewer lines (because your six lines of code is now replaced by one - the call to the function containing those six lines).


The structure of a function

As you should know by now, every Sphere game must have a main script file, containing the main function called game. If you don't know or remember, please read chapter 1 again.

  • A function always starts with the word function so both Sphere and the reader of the script knows we're going to make a function. You call such a word a keyword because JavaScript uses it internally to identify what you're trying to do. In this case, we want to define a function.
  • After the word function comes the name of the function. When giving it a name, keep in mind only letters, numbers and the underscore _ are allowed. It is also not possible to start the name with a number. Examples: FunctionName, funcname1, function_name.
  • After the name come two parentheses: an opening parenthesis and a closing parenthesis (). Things can go inbetween these parentheses, but I'll explain that later.
  • After that, you put an opening brace { which indicates that the code coming after the brace will belong to your function.
  • After the brace, you put the code that would be run when the function is run.
  • When you're done with your code, you close down the functions with the closing brace }.

Example of a function named 'game':

function game()
{
  //code comes here.
}

That's what a function looks like, basically. game() is, as mentioned before, the starting function for a Sphere game.

Still, if you try to run this code, nothing will happen. Sphere will open and then immediately close. That's because we didn't put any code in yet. Let's add one very simple command, with a comment in front of it:

function game()
{
  //This function will open the map map.rmp.
  MapEngine("map.rmp", 60);
}

This piece of code WILL do something, namely it will open the map engine with the file map.rmp, with a framerate of 60 frames per second. Still, if you want this code to work, make sure that the file map.rmp actually exists (and is inside the maps/ directory of your game)!

The above code also demonstrates what calling a function looks like. MapEngine() is a function inside Sphere itself, but if it were coded in JS by a normal user it would have the same structure as the game() function:

function MapEngine(map, framerate)  //map, framerate will be explained in the next chapter.
{
  //Code to make the map engine to work here.
}

The original function actually consists of internal Sphere code, not scripted in JS, but in the programming language Sphere itself has been written in, C++.


Example of some simple Sphere functions

Okay, now for some more difficult stuff. I'm going to show you and explain a few of the most used functions in Sphere. Every line of code will be explained.

function game()
{
  CreatePerson("my_name", "character.rss", false);
  //Creates a new person. gives him the name my_name and loads the spriteset file character.rss for his looks.
  //'false' indicates he should NOT be destroyed when the game switches maps. That means he'll keep existing even on other maps.
 
  AttachCamera("my_name");
  //Makes sure that the camera is always pointed at the person called my_name.
 
  AttachInput("my_name");
  //Bind the default input to the person my_name. This means that when you press an arrow key, he'll move, stuff like that.
 
  MapEngine("map.rmp", 60);
  //We'll open map.rmp with a framerate of 60 frames per second (fps).
}

The functions:


A list of common functions

There are a lot more common Sphere functions, another bunch explained below. You can look up all the other Sphere functions in the doc_functions.txt file that comes with Sphere (it's in the docs directory). You should also check out the Functions documentation on this website, it's a lot more comprehensive and offers examples for each function.

NOTE BEFORE YOU USE THESE: Some functions have a dot . in them. I'll talk about what this means in detail later. For now, just put the appropiate function that loads a resource in front. Example LoadFont("file").drawText(1, 1, "text");

Function Description
FlipScreen() Puts everything you have drawn so far on the screen. When using a draw or blit function or anything like that, it doesn't get drawn on the screen but on the backbuffer. Things drawn on the backbuffer will show up on the screen when using FlipScreen(). After that the backbuffer is made empty again. ALWAYS PUT THIS AFTER YOU HAVE DRAWN SOME STUFF!
LoadFont("font_file") Loads a font file. Only the Sphere font format .rfn is supported, so make a Sphere font first.
Font.drawText(x, y, text) Write text on the screen with the assigned Font and on the assigned x/y coordinates (in pixels). Don't forget to replace 'Font' with the font file you wish to use, like this:
LoadFont("font_file.rfn").drawText(10, 10, "Good day, world!");

This is not the optimal way to do it because you load the font file over and over every time you want to draw text, but it will do for now.

Font.drawTextBox(x, y, width, height, offset, text) Write text on the screen with the assigned font and on the assigned x/y coordinates. This is a bit different from Font.drawText: it has borders you can assign using the width and height arguments. When the text hits the maximum width, it continues writing the rest of the text on the next line until the maximum height is reached too (in pixels). The offset adds an extra bit to the Y position of your text.
LoadWindowStyle("windowstyle_file") Loads a windowstyle file in memory. Make sure you have a windowstyle file first, of course.
WindowStyle.drawWindow(x, y, width, height) Draw the windowstyle on coordinates x,y with the assigned width and height.
LoadImage("image_file") Load an image file (bmp, png, gif, jpg, pcx, tga are valid formats, png is recommended for its size/quality ratio).
Image.blit(x, y) Draws the image on the assignes x,y coordinates. Blit is an old English word for draw.
Image.zoomBlit(x, y, zoom) Draws the image on the assigned x,y coordinates, but resized according to the zoom argument. Zooming is 1:1, so when inserting a value like 1.5 the image will be drawn 1.5 its original size.
LoadSound("sound_file") Load a sound file into memory. wav, mp3, ogg, MIDI, modules (IT, MOD, XM) are supported sound formats.
Sound.play(repeat true/false) Play the loaded sound. The argument is tru or false, depending on if you want the sound to repeat. true = repeat, false = do not repeat.
Sound.stop() Stop the sound. This function has no arguments.
RenderMap() If you have the map engine open and you want to draw something on screen, the map will not be shown when you FlipScreen() it. This is done for flexibility purposes. With RenderMap() you tell Sphere to put an image of the map engine in the backbuffer, so all the stuff you draw after it will be displayed on an image of the map. Put this before all other draw stuff, because it will be drawn in front of the rest if you don't.
GetKey() Pauses the script until you press on a key (ANY key). Useful for intros. can also be used in combination with an if condition. You can find more information about if in chapter 4.
ChangeMap("map_file") If the map engine is open, you can switch maps with this function.
Exit() Exits the game.
Personal tools