Associative Arrays - KidneyThief/TinScript1.0 GitHub Wiki

A useful construct for managing data, TinScript supports the use of associative arrays. This is one of the few elements of the language that was deemed so useful, that it is supported, even though there is no direct syntactic equivalent in C++.

hashtable

The array is declared, and then individual entries are defined using an n-dimensional key. The actual variable type is named 'hashtable'. The individual entries are all independently declared, as whatever type you'd like.

For example, suppose we were implementing a game, and wanted to store the "weapon damage", for an assortment of different weapons, where the damage was dependent on:

  • The weapon type
  • The target receiving the damage
  • The player skill level.
Example:
  • We have two weapons: a Colt .45 and a Bazooka.
  • We have three types of damage recipients: A soldier, A Jeep, and A Tank.
  • We have two skill levels - easy and hard
First we declare the hashtable
  • hashtable gWeaponDamage;
Then we declare the array elements (in this case, as %damage to the target)
  • // define all the data associated with the Colt
    float gWeaponDamage["Colt", "soldier", "easy"] = 100.0f;
    float gWeaponDamage["Colt", "soldier", "hard"] = 50.0f;
    float gWeaponDamage["Colt", "jeep", "easy"] = 20.0f;
    float gWeaponDamage["Colt", "jeep", "hard"] = 5.0f;
    float gWeaponDamage["Colt", "tank", "easy"] = 10.0f;
    float gWeaponDamage["Colt", "tank", "hard"] = 0.0f;
  • // define all the data associated with the Bazooka
    float gWeaponDamage["Bazooka", "soldier", "easy"] = 100.0f;
    float gWeaponDamage["Bazooka", "soldier", "hard"] = 90.0f;
    float gWeaponDamage["Bazooka", "jeep", "easy"] = 100.0f;
    float gWeaponDamage["Bazooka", "jeep", "hard"] = 50.0f;
    float gWeaponDamage["Bazooka", "tank", "easy"] = 50.0f;
    float gWeaponDamage["Bazooka", "tank", "hard"] = 20.0f;
Now define a global to determine the skill level, and function to determine "how much damage to receive
  • string gPlayerSkillLevel = "easy";
  • float LookUpDamagePercent(string weapon_type, object recipient)
    {
        // get the type of target being shot
        string target_type = recipient.GetObjectName();

        // loop up in the hashtable, find the damage percent to apply
        float damage_percent = gWeaponDamage[weapon_type, target_type, gPlayerSkillLevel];
        Print("The percentage of damage to deal is: ", damage_percent);
        return (damage_percent);
    }
Implement the targets:
  • // initialize our soldier
    void Soldier::OnCreate()
    {
        int self.max_health = 100;
        int self.health = 100;
    }
  • // initialize our tank
    void Tank::OnCreate()
    {
        int self.max_health = 10000;
        int self.health = 10000;
    }
  • // define an "apply damage" function
    void ApplyDamage(string weapon_type, object target)
    {
        float percent = LookUpDamagePercent(weapon_type, target);

        float amount = (percent / 100.0f) * target.max_health;
        target.health -= amount;
        Print(target.GetObjectName(), " now has health: ", target.health);

        // see if we're dead
        if (target.health <= 0)
            Print("I'm dead!");
    }
Test run:
  • // create a soldier and a tank
    object soldier = create CScriptObject("Soldier");
    object tank = create CScriptObject("Tank");
  • // put the game in "hard" mode
    gPlayerSkillLevel = "hard";
  • // shoot the soldier
    ApplyDamage("Colt", soldier);
    • output: The percentage of damage to deal is 50.0000
      output: Soldier now has health: 50
  • ApplyDamage("Bazooka", soldier);
    • output: The percentage of damage to deal is 90.0000
      output: Soldier now has health: -40
      output: I'm dead!
  • // shoot the tank
    ApplyDamage("Colt", tank);
    • output: The percentage of damage to deal is 0.0000
      output: Tank how has health: 10000
  • ApplyDamage("Bazooka", tank);
    • output: The percentage of damage to deal is 20.0000
      output: Tank now has health: 8000
  • ApplyDamage("Bazooka", tank);
    • output: The percentage of damage to deal is 20.0000
      output: Tank now has health: 6000

  • The above example uses an associative array to manage and organize data - another example uses one to create a function table, and can be found in the section describing the Scheduler.
⚠️ **GitHub.com Fallback** ⚠️