Local Environment – ShiVa Engine

Local Environment

We start with a blank environment. The first step is to give it a name and to fill it. Let’s take bags inventory as the tutorial example.
As we want to use this environment as a ordered structure, we have to choose a clever notation for keys.
For each bag, we want to know if it’s opened, how many items are inside, and the list of the items.
The chosen structure will be:
bag.index of the bag in the inventory.state.opened
bag.index of the bag in the inventory.items.count
bag.index of the bag in the inventory.item.index of the item in the bag
bag.index of the bag in the inventory.item.index of the item in the bag
…as many items as there are in the bag

Environment manipulation

List the content of the environment.

for i = 1, application.getCurrentUserEnvironmentVariableCount ( )
do
    local sKey = application.getCurrentUserEnvironmentVariableNameAt ( i-1 )
    log.message ( sKey ,"=", application.getCurrentUserEnvironmentVariable ( sKey  ) )
end

The order of the listing is not the same as we entered keys. That is due to the internal management of the environment.
Fortunally, we put indexes in the name of the keys.
List the properties of bag 01 :

for i = 1, application.getCurrentUserEnvironmentVariableCount ( )
do
    local sKey = application.getCurrentUserEnvironmentVariableNameAt ( i-1 )
    if ( string.contains ( sKey, "bag.01.") )
    then
        log.message ( sKey,"=", application.getCurrentUserEnvironmentVariable (  sKey ) )
    end
end

Now, list only items of bag 03:

--retrieve the item count
local iItemCount = application.getCurrentUserEnvironmentVariable ( "bag.03.items.count" )
for i = 1, iItemCount
do
    local sKey = string.format ( "bag.03.item.%i", i )
    log.message ( sKey,"=", application.getCurrentUserEnvironmentVariable (  sKey ) )
end

Imagine that we don’t know how many bags we have.
Use application.getCurrentUserEnvironmentVariableStatus as exit condition.
List the state of bags. We only know that we can’t have more bags than keys in environment.

for i = 1, application.getCurrentUserEnvironmentVariableCount ( )
do
    local sKey = string.format ( "bag.%0.2i.state.opened", i )
    if ( application.getCurrentUserEnvironmentVariableStatus ( sKey ) ~= application.kStatusNone )
    then
        log.message ( "The bag "..i.." is ",
              (application.getCurrentUserEnvironmentVariable (  sKey ) and "opened" or "closed") )
    else
        break
    end
end

Change the mouse of the designer to a graphics tablet. Just set the Key “bag.02.item.1” again.

application.setCurrentUserEnvironmentVariable ( "bag.02.item.1"       , "Graphics tablet" )

Let’s destroy the bag of the boss (bag 03).

local i = 0
local sKey = application.getCurrentUserEnvironmentVariableNameAt ( i )
while ( not string.isEmpty ( sKey ) )
do
    if ( string.contains ( sKey, "bag.03.") )
    then
        application.unsetCurrentUserEnvironmentVariable ( sKey )
    else
        i = i + 1
    end
    sKey = application.getCurrentUserEnvironmentVariableNameAt ( i )
end

When you delete a key in the environment, the next key is at the same place that the one you just delete.

Save and load environment

Use application.saveCurrentUserEnvironment ( ) to save the environment.
In the log message, you will see Write local GamePlayerEnvironment the full path to the save folder/ATeamInventory.sts.
Now you can restart the game, and load the environment backup.

application.loadCurrentUserEnvironment  ( "ATeamInventory" )
if ( application.getCurrentUserEnvironmentVariableCount ( ) == 0 )
then
    log.warning ( "No save found." )
    this.InitEnvironment ( )
end

You can also use loadCurrentUserEnvironment to cancel changes

application.loadCurrentUserEnvironment  ( "ATeamInventory" )
log.message ("bag.02.item.1", " = ", application.getCurrentUserEnvironmentVariable ("bag.02.item.1"))
application.setCurrentUserEnvironmentVariable ( "bag.02.item.1"       , "Graphics tablet" )
log.message ("bag.02.item.1", " = ", application.getCurrentUserEnvironmentVariable ("bag.02.item.1"))
application.loadCurrentUserEnvironment  ( "ATeamInventory" )
log.message ("bag.02.item.1", " = ", application.getCurrentUserEnvironmentVariable ("bag.02.item.1"))

You can load and save partial environment.

application.saveCurrentUserEnvironmentVariable  ( "bag.*" )         -- Save only bags variables
application.clearCurrentUserEnvironment ( )                         -- Clear the current environment
application.loadCurrentUserEnvironmentVariable  ( "bag.02.item.*" ) -- Load only items of the bag 02

  • slackBanner