Squirrel Scripts tutorial

Talk here about anything related to the Custom Interfaces. Tutorials and so on.
User avatar
GMSignPainter
Site Admin
Posts: 2434
Joined: Fri Oct 09, 2009 10:18 pm
Location: Lyon - France
Contact:

Squirrel Scripts tutorial

Postby GMSignPainter » Sat Apr 21, 2012 9:54 pm

Hello there, I will explain how to use the Squirrel Scripts in your Custom Interfaces.

Table of Content:

    1. What is Squirrel
    2. Where to find the documentation about Squirrel
    3. How the game runs the scripts
    4. Simple script implementation
    5. Pre-defined functions
    6. Access Widgets of an UI

User avatar
GMSignPainter
Site Admin
Posts: 2434
Joined: Fri Oct 09, 2009 10:18 pm
Location: Lyon - France
Contact:

Re: Squirrel Scripts tutorial

Postby GMSignPainter » Sat Apr 21, 2012 9:57 pm

1. What is Squirrel

Squirrel is a high level imperative, object-oriented programming language, designed to be a light-weight scripting language that fits in the size, memory bandwidth, and real-time requirements of applications like video games.
It is used in Dragon Raja in order to make scripts for the Interfaces.

If you'd like to learn more about Squirrel, visit their website : http://squirrel-lang.org/

User avatar
GMSignPainter
Site Admin
Posts: 2434
Joined: Fri Oct 09, 2009 10:18 pm
Location: Lyon - France
Contact:

Re: Squirrel Scripts tutorial

Postby GMSignPainter » Sat Apr 21, 2012 10:00 pm

2. Where to find the documentation about Squirrel

On their website, there : http://squirrel-lang.org/#documentation

User avatar
GMSignPainter
Site Admin
Posts: 2434
Joined: Fri Oct 09, 2009 10:18 pm
Location: Lyon - France
Contact:

Re: Squirrel Scripts tutorial

Postby GMSignPainter » Sat Apr 21, 2012 10:05 pm

3. How the game runs the scripts

The scripts are run only ingame. This means they aren't running if you aren't loged.
Each script of each UI is run 1 time per frame.
Also, the scripts are run only if the UI is visible.

There are three functions that must exist for a script to run properly:

main

This is the main function of the script and it is run one time per frame. Put all your code and function calls in this function.

OnOpen

This function is called when the UI containing the Script is opened. The function is only executed once per opening.

OnClose

This function is called when the UI containing the Script is closed. The function is only executed once per closing.

User avatar
GMSignPainter
Site Admin
Posts: 2434
Joined: Fri Oct 09, 2009 10:18 pm
Location: Lyon - France
Contact:

Re: Squirrel Scripts tutorial

Postby GMSignPainter » Sat Apr 21, 2012 10:12 pm

4. Simple script implementation

Let's make our first script !

First of all, you need to make a text file in the Data/UI/Scripts folder. Call it MyFirstScript.nut for example.
Then open it.

In order for the script to run properly, you need to add the three following functions which were described above:

Code: Select all

function OnOpen()
{

}

function OnClose()
{

}

function main()
{

}


Then for this script to be executed in an UI, open the .ui file you want this script to be run in and add the following line in the UI proprieties:

Code: Select all

UIName=MyFirstInterface
{
   ...
   script=Data/UI/Scripts/MyFirstScript.nut
   ...
}


Now run the game and open this UI.
Surprise ! Nothing happens !

Now let's add some content:

Add the following line in the main function:

Code: Select all

   Game.AddStatusMessage("I AM ALIVE !");


Now run the game and it should be spamming with white "I AM ALIVE" texts on the left.

Little exercice !
Add a "HELLO THERE !" message when you open the UI and a "BYE THERE !" message when you close the UI.

User avatar
GMSignPainter
Site Admin
Posts: 2434
Joined: Fri Oct 09, 2009 10:18 pm
Location: Lyon - France
Contact:

Re: Squirrel Scripts tutorial

Postby GMSignPainter » Sat Apr 21, 2012 10:35 pm

5. Pre-defined functions

Here a list of predefined functions:

Game.CreateGlobal(string name)
Creates a global variable to be used in any script.

Game.GetGlobal(string name)
Get the value of a global variable. If the variable doesn't exist, it is created.

Game.SetGlobal(string name, int value)
Set the value of a global variable. If the variable doesn't exist, it is created.

Game.RemoveGlobal(string name)
Deletes a global variable.

Game.AddStatusMessage(string message)
Displays a status message in white.

Game.AddStatusMessageRGB(int red, int green, int blue, string message)
Displays a status message in the given color.

Game.AddLanStatusMessage(int type, int no)
Displays a status message in white. The message is got from the en_lan.txt file in the Data folder.

Game.AddLanStatusMessageRGB(int red, int green, int blue, int type, int no)
Displays a status message in the given color. The message is got from the en_lan.txt file in the Data folder.

Game.GetCurrTime()
Returns the number of seconds elapsed since the 1st of January 1970.

Game.GetTicks()
Returns the number of milisecond elapsed since the game has been started.

Game.SendChat(string message)
Sends a normal chat message.

Game.SendGuildChat(string message)
Sends a guild chat message.

Game.SendPartyChat(string message)
Sends a party chat message.

Game.SendWhisperChat(string target, string message)
Sends a whisper chat message to the specified target.

Game.GetSpecialString(string name)
Get a special string. The special strings are defined in both the code and the en_strings.txt which is located in the Data/UI/Resources folder. The hardcoded strings are listed in the viewtopic.php?p=96118#p96118 topic.

Game.GetSpecialValue(string name)
Get a special value. The special values are all hardcoded and a list can be found in the following topic: viewtopic.php?p=96119#p96119

Game.HasUnreadMails()
Returns true if there are unread mails. Else, returns false.

Game.IsRandomArenaMatchReady()
Returns true if a random match is ready to be played. Else, returns false.

Game.AskQuestion(int question)
Asks a pre-defined question to the player.

Game.AnswerQuestion(bool answer)
Replies to the previously asked question.

Hero.IsDead()
Returns true if your main character is dead. Else, returns false.

UIManager.ShowUI(string name)
Shows the UI of the given name.

UIManager.HideUI(string name)
Hides the UI of the given name.

UIManager.ToggleUI(string name)
Toggles the UI of the given name.

UIManager.IsShown(string name)
Returns true if the UI of the given name is currently visible. Else, returns false.

User avatar
GMSignPainter
Site Admin
Posts: 2434
Joined: Fri Oct 09, 2009 10:18 pm
Location: Lyon - France
Contact:

Re: Squirrel Scripts tutorial

Postby GMSignPainter » Sat Apr 21, 2012 10:50 pm

6. Access Widgets of an UI

To access to an UI, it is very simple, use the function Get_UIName().

Example:

Code: Select all

local ui = Get_MyFirstUI();


Then you can modify its properties:

Code: Select all

ui.x = 200;


To access to the widget of a specific UI, use the Get_UIName_WidgetName() function.

Example:

Code: Select all

local widget = Get_MyFirstUI_MyWidget();
widget.relx = 100;


Return to “Custom Interfaces”

Who is online

Users browsing this forum: No registered users and 1 guest