The Dwarves of Glistenveld Wiki
Tag: Visual edit
No edit summary
Tag: Visual edit
 
Line 1: Line 1:
In the scripting interface, variables are declared in 2 ways, local and long-term.
+
In the scripting interface, variables are declared in 2 ways, local and remembered.
   
 
== Local Variables ==
 
== Local Variables ==
Line 6: Line 6:
 
if(foo == "foo")
 
if(foo == "foo")
 
foo += "bar";
 
foo += "bar";
The following code is the recommended method of identifying variable's values, but will only work for local variables if they are declared earlier within the same function:
+
The following code is how you debug a variable's value, but will only work for local variables if they are declared earlier within the same function:
 
print(foo); //displays "foobar"
 
print(foo); //displays "foobar"
  +
Note that once a local variable is declared, you can change its type at any time, by re-assigning it:
  +
foo = new Rectangle();
   
== Long-Term Variables ==
+
== Remembered Variables ==
 
Not only are these kind of variables Global, but they will also be saved and loaded automatically with a save-game. This is very important when you need to maintain quest states, or anything else that need to be retained between game sessions.
 
Not only are these kind of variables Global, but they will also be saved and loaded automatically with a save-game. This is very important when you need to maintain quest states, or anything else that need to be retained between game sessions.
   
The following code demonstrates how to create, query and then modify a Long-Term variable:
+
The following code demonstrates how to create, query and then modify a Remembered variable:
self.remember("foobar","foo");
+
game.remember("foobar","foo");
 
if(foobar == "foo")
 
if(foobar == "foo")
 
foobar += "bar";
 
foobar += "bar";
Note that after the initial long-winded declaration, the code remains the same, except that foo will now be able to be referenced and queried outside the function, even by other scripts.
+
Note that after the initial long-winded declaration, the code remains the same, except that foo will now be able to be referenced and queried outside the function, by other scripts and between game-sessions.
   
The one limitation of Long-Term variables is that only certain types can be passed in to self.remember. This is because they will eventually need to be converted into text for saving/loading.
+
The one limitation of Remembered variables is that only certain types can be passed in to game.remember. This is because they will eventually need to be converted into text to be saved with the game data.
   
 
Also note that the script editor will allow you to perform the following (bad) code:
 
Also note that the script editor will allow you to perform the following (bad) code:
foo = new Rectangle();
+
foo = new Rectangle(); //this is bad if foo is a remembered variable
After the above code is executed, the engine will continue to work fine while the game session is running, but it will not save correctly because the type that the variable has changed into is not registered by the engine as a known SaveReferenceable.
+
After the above code is executed, the engine will continue to work fine while the game session is running, but it will not save correctly because the type that the variable has changed into is not registered by the engine as a known SaveReferenceable type. We have no control over this, so just please only choose the types below when re-assigning a remembered variable.
   
=== Long-Term Variable Types ===
+
=== Remembered Variable Types ===
 
{| class="article-table"
 
{| class="article-table"
 
!Name
 
!Name
Line 47: Line 49:
 
|Any entity on the map can be saved. If the entity is dead, the reference will be lost.
 
|Any entity on the map can be saved. If the entity is dead, the reference will be lost.
 
|-
 
|-
|
 
 
|(More to come)
 
|(More to come)
 
|
 
|}
 
|}
  +
Remembered variables are also displayed in the variable list UI, which you can bring up while the game is running by pressing Ctrl+V.[[Category:Scripting]]
[[Category:Scripting]]
 

Latest revision as of 16:15, 24 March 2018

In the scripting interface, variables are declared in 2 ways, local and remembered.

Local Variables[]

The following code demonstrates how to create, query and then modify a Local variable:

var foo = "foo";
if(foo == "foo")
	foo += "bar";

The following code is how you debug a variable's value, but will only work for local variables if they are declared earlier within the same function:

print(foo); //displays "foobar"

Note that once a local variable is declared, you can change its type at any time, by re-assigning it:

foo = new Rectangle();

Remembered Variables[]

Not only are these kind of variables Global, but they will also be saved and loaded automatically with a save-game. This is very important when you need to maintain quest states, or anything else that need to be retained between game sessions.

The following code demonstrates how to create, query and then modify a Remembered variable:

game.remember("foobar","foo");
if(foobar == "foo")
	foobar += "bar";

Note that after the initial long-winded declaration, the code remains the same, except that foo will now be able to be referenced and queried outside the function, by other scripts and between game-sessions.

The one limitation of Remembered variables is that only certain types can be passed in to game.remember. This is because they will eventually need to be converted into text to be saved with the game data.

Also note that the script editor will allow you to perform the following (bad) code:

foo = new Rectangle(); //this is bad if foo is a remembered variable

After the above code is executed, the engine will continue to work fine while the game session is running, but it will not save correctly because the type that the variable has changed into is not registered by the engine as a known SaveReferenceable type. We have no control over this, so just please only choose the types below when re-assigning a remembered variable.

Remembered Variable Types[]

Name Description
Integer A whole number.
Double/Float A decimal number.
String A string of text.
Point java.awt.Point
HexagonPoint The engine's logical coordinate object. Also allowed are FinalPoint and TilePoint.
Entity Any entity on the map can be saved. If the entity is dead, the reference will be lost.
(More to come)

Remembered variables are also displayed in the variable list UI, which you can bring up while the game is running by pressing Ctrl+V.