[Release] ChaiScript Player Data Management

Topic created ยท 3 Posts ยท 270 Views
  • Why?

    The current state of Pluto IW5 modding is very different from Pluto T6 and other managed modding environments. Currently, storing player data the way you would store it in those other environments is unstable or non-functional.

    ChaScript Player Data Management is a simple library that provides a simple and idiomatic way of storing and managing player data purely in managed code.

    How?

    There are two classes in the library: PlayerStore and PlayerData.

    PlayerData stores data of a single player. Pretty straight forward, right?
    Internally it's just a a class that provides safe access to a dictionary.

    PlayerStore indexes all PlayerData you feed to it. Again, just a simple dictionary access API.

    Download

    Here.

    Pratical Example

    This topic will teach you to implement this library on your code. If you don't want to follow a step by step guide, you can scroll down to the documentation and figure it out yourself.

    Before starting, copy the library code to your .chai file.

    1. Create your PlayerStore

    On the top-level scope of you script, you can create a players object that will hold all your players' data.

    var players = PlayerStore()
    

    2. Register every player that connects

    When any player connects, we want to register it to our PlayerStore. First, you need to create a PlayerData object with that player and then you can set default data, if you want.

    level.onNotify("connected", fun(arguments) 
    {
        // create the `PlayerData`
        var data = PlayerData(player);
    
        // set some default data
        data.Set("welcomed", false);
        data.Set("is_vip", true);
    
       // add it to our `PlayerStore`
       players.Add(data);
    
       player.onNotify("spawned_player", fun[player](arguments)
       {
            welcomePlayer(player);
       }
    }
    

    3. Use PlayerData inside a function

    To find PlayerData on any player that exists within our PlayerStore, you can use the Find function. If the player you give exists inside our store, it will return the PlayerData object on it.

    From then on, you can can get specific values of this PlayerData with the Get function.

    def welcomePlayer(player)
    {
        var data := players.Find(player);
    
       if(!data.Get("welcomed")) 
       {
           player.iPrintLnBold("Welcome!");
          
           if(data.Get("is_vip"))
           {
               player.iPrintlnBold("You're VIP. Cool, huh?");
           }
    
           data.Set("welcomed", true);
       }
    }
    

    And that's it.

    Future Development

    A lot of things can be implemented to make this library more robust.

    • The library doesn't handle players disconnecting. In big servers, this may be a problem because the PlayerStore can get massive. A simple PlayerData.Remove(playerObject) function would do the trick.

    • The PlayerStore is not persistent between games. Using tom's Plutonium Filesystem and a JSON C++ plugin would be a good approach to this problem.

    Documentation

    PlayerData

    Stores data on a specific player.

    PlayerData(player) - Creates data on a player.

    • entity (Object): the player entity object

    Find(key) - Finds a data value given a key.

    • key (string): the key to be found
    • returns: the key's value if it exists, else nothing.

    Set(key, value) - Sets the value of a key. Creates a new key if the given key is not found.

    • key (string): the key to be set
    • value (any): the value

    Exists(key) - Checks if a key exists.

    • key (string): the key to be checked
    • returns: true if the key exists, else false.

    PlayerStore

    Manages all players' PlayerData.

    PlayerStore() - creates an empty PlayerStore

    Find(playerObject) - Finds a PlayerData given the player object.

    • playerObject (Object)
    • returns (PlayerData): the data associeated with the given player

    Get(index) - Gets the player at the given index. This function does not check bounds.

    • returns (PlayerData): the data associated with the given index

    Add(playerData) - Adds a new PlayerData to the store.

    • playerData (PlayerData): the data to be added

    First(function)- Gets the first PlayerData that matches the function.

    • function (fun): a boolean function that takes a PlayerData.
    • returns (PlayerData)

    Filter(function) - Gets all PlayerData that match the function.

    • function (fun): a boolean function that takes a PlayerData.
    • returns (PlayerStore): a new PlayerStore containing all filtered PlayerData.

    Empty() - Returns true if this PlayerStore is empty.

    Exists(playerObject)- Returns true if PlayerData of the given player exists, else false.

  • Nice script

Log in to reply