Wednesday, January 1, 2014

RAGS Tips and Tricks #2: Variable Experiments

RAGS Tips and Tricks #2: Variable Experiments

Download here.

For this installment, I'll be focusing on some of the experiments I have done when messing around with variables.

Variables are the backbone of many games.  Any game engine needs to allow for variables and variable manipulations.  Rags has an interesting way to deal with it.

The basic variable call is [v: variable_name].  Rags will replace that text with whatever is in the variable, variable_name.  Very straight forward stuff.  If you followed the official tutorial, this shouldn't be new.

Test 1: Testing variable names with special characters.

Test 1 focuses on the special characters.  Basically Rags check for [v: something] and replaces it with whatever is something.  What if the something resembles [v: something_else].

Right click on the player portrait and select Test 1.  You should see the following.
Base variable: Test1.  Initial Value = This is "Test 1".
Second variable: [v: Test1].  Initial Value = This is "v: Test1".

Calling base variable:
[v: Test1] = This is "Test1"

Calling second variable
[v: [v: Test1]] = 
The second variable is the call for Test1.  Some of engines typically restrict the usage of special characters.  For Rags, these would be the square brackets, [ and ].  Surprisingly, you can still name the variable using those.

The first call, [v: Test1], results in displaying the first variable.  This works as expected.

The second call, [v: [v: Test1]], however didn't work.  In fact, it displays nothing.  My guess is this is because the engine scans the inner [v: Test1] and replaces it with 'This is "Test1"'.  The resulting command is then [v: This is "Test1"]!  Since there's no variable called This is "Test1", it displays nothing.  A side effect means it's difficult to call the second variable.  No big loss though, I can't think of a reason why you would name a variable like the second one.

With my guess in mind, that brings us to Test 2...

Test 2: Reference Variables.

I'm not sure of the actual terminology, but I've been calling it a reference variable (if someone knows the actual term, please let me know and I'll update this).  The point of the reference variable isn't to hold the data you want, but instead holds a reference to the variable with the data you want.

Anyways, right click on the player portrait and select Test 2.
3 variables:
Test2a = Test2c
Test2b = v: Test2c
Test2c = Success

[v: [v: Test2a]] = Success
[[v: Test2b]] = Success
In Test 2, we have three variables.  The final variable, Test2c, holds the data we want.  In this case, it's just "Success".  The other two variables are tests for referencing the Test2c variable.

In Test2a, it just duplicates the pattern from Test1.  With Test2b, it includes some of the variable call (the v: part).

The first part of the test is [v: [v: Test2a]].  The inner part, [v: Test2a], gets replaces with the contents of Test2a (Test2c), resulting in [v: Test2c].  This is the standard variable call and gets replaced with "Success."  In other words, reference variables work.  Reference variables work!  You can store variables instead of variables!

The second part is [[v: Test2b]].  This in particular does not have the first v: as it is part of the Test2b.  Interestingly enough, it also results in "Success" meaning that the beginning [v: isn't a special order but something that can be constructed.

At this point, my guess is the Rags engine goes through all text and searches for any [v: and tries to replace them with variables starting with the innermost nested call.

The difference between Test2a and Test2b is a very interesting one.  Test2b is only missing the [ and ] for the variable call.  This brings us to our final test.

Test 3: Setting up Reference Variables.


Right click on the player portrait and select Test 3.  You'll see:
3 variables:
Test3a: [v: Test3c]
Test3b:
Test3c: Success

Set Test3b to "[v: Test3c]"
Variable: Set: Test3b, Success
[v: Test3a] = Success
[v: Test3b] = Success

Change Test3c to "Reference Success"
Variable: Set: Test3c, Reference Success

[v: Test3a] = Reference Success
[v: Test3b] = Success
The Variable: Set: ... things are just the commands taking place between the display texts.

In this test, we have two variables (Test3a and Test3b) trying to reference Test3c.  Test3a has the initial value of [v: Test3c] while Test3b is assigned that value as you run this test.  Afterwards, we change Test3c to a different value.  If the referencing works, then Test3a and Test3b should result in the new value of Test3c.

Only Test3a passed while Test3b failed.

This is perhaps the hardest part with Reference variables.  The engine will automatically replace any variable calls with the actual data.  When you set a variable with a variable call (Test3b), you are only putting the contents of Test3c into Test3b, not the actual variable call.

At the end of the test, the Debug Data will have...
Test3a: [v: Test3c]
Test3b: Success
Test3c: Reference Success

Test3b is not the same as Test3a.  The command to change Test3b to "[v: Test3c]" automatically treated it like a variable call and Rags didn't set Test3b to "[v: Test3c]" but instead set it to "Success".  This means Due to the natural of the Rags Engine, you can not set up Reference Variables in game.  They must be set as initial values!

Arguably, you do not need to include the [ and ] for Test3b and just use [[v: Test3b]] for everything.  However this requires very careful planning since if you fail to include the brackets, the call will fail.

Note: If you want to run test 3 again, you need to restart.

TL;DR Section

RAGS Tips and Tricks #2 - Variables download
Rags engine looks for the text: [v: variable_name] and replaces it with the variable context.  You can build parts of this text from other variables.
You can have variables hold other variables or reference other variables.
Setting up Reference Variables are tricky.  You need to do it as the initial set up.