Storing players data with chaiscript

Topic created · 4 Posts · 98 Views
  • Hello,
    in a previous post, I've asked if it was possible to make an on-screen Killstreak counter. With some help, I was able to come up with something. Before I go forward, I want to specify that I have installed the plutosctript framework .dll in the plugins folder and that I have successfully tested the welcome script described in this post https://forum.plutonium.pw/topic/571/tutorial-chaiscript-for-iw5.
    So the question that I have is how do I store in an efficient way the various players that are going to login/logoff and update such data structure each time a player is killed. I come from a Java background so I'm not entirely unfamiliar with c++ but I'm new to chai script and I've only found just a few of meaningful scripts here and online that I could look at to see how the syntax works etc.
    So here's the source code:

    var kills = 0;
    entity players[18]; //Store players this way? A better data structure?
    
    level.onNotify("connected", fun(arguments) {
            // you can make new var
    
        var player = arguments[0];
        addPlayer(player);
    
    	var hud_text = gsc.newClientHudElem(arguments[0]);
    	hud_text.set("x", 540); //Approx middle of the screen
    	hud_text.set("y", 0);
    	hud_text.set("font", "hudbig");
    	hud_text.set("fontscale", 0.65);
    	hud_text.setText("^1Killstreak: " + kills);
    });
    
    def onPlayerKilled(ePlayer, eInflictor, eAttacker, iDamage, sMeansOfDeath, sWeapon, vDir, sHitloc, iTimeOffset, iDeathAnimDuration)
    {
     //Get ePlayer from entity and set kills = 0
     // Get ettacker and add 1 kill to counter
    
     resetCount(ePlayer);
     
    
    }
    
    add_callback_player_killed(onPlayerKilled); //Copied this line from the pluto framework github
    
    def addPlayer(connectedPla) 
    {
        for(int i=0;i<18;i++)  
        {
          if(players[i] == null) 
          {
            players[i] = connectedPla;
          }
        }
    }
    
    def resetCount(deadPlayer) 
    {
      for(int i=0;i<18;i++)  
        {
          if(players[i] equalsMethod? deadPlayer) 
          {
            kills = 0;
          }
        }
    }
    
    def onPlayerDisconnect(ePlayer)
    {
     //remove player from data structure;
    }
    
    add_callback_player_disconnect(onPlayerDisconnect);
    

    So when a player connects the onNotify() should add it to this "entity array" (which I know probably won't run) and then when it disconnects it should be removed. When a player dies the text on the screen should be updated using the Player and eAttacker parameters, I wish to find a nice way to iterate through this data structure and update the kills var of both players (does the method onPlayerKilled() trigger even when a player dies of fall damage or other?).
    Also, how would I update the var hud_text?

    This is what I was thinking for an Object that stores the data that I need but I don't know if chai script supports something like this or not.

    struct Player {
        int kills;
        std::string name;
        std::string hud_text;
        public:
    
        Player(std::string name, std::string hud_text) {
            this->name = name;
            this->hud_text = hud_text;
        }
    
        void resetKills() { //Trigger on death
            kills = 0;
            hud_text = '0';
        }
    
        void add() {
            kills += 1;
            char* intStr = itoa(kills);
            hud_text = std::string(intStr); //Actually hud_text.setValue(std::string(intStr)); should be used I think
        }
    };
    

    I would really appreciate the feedback from some of the more expert coders here on how to implement a data structure. Is it possible to store this struct in an array and access the data of the various players when the functions resetCount/PlayerKilled get executed?
    Thanks a lot in advance.
    Edit 1: Now that I think about it, where does the game store the killstreak value of the player? They must be stored somewhere, is it possible to access this number using chaiscript?

  • @Diavolo
    I may not answer all questions, but I think it can help you

    in GSC scripts of the game, players are stored in an array(it was also done in the InfinityScript, it uses the List<T>, and steam addon), players can also be found through GetEntArray(but Chai does not support array returns from GSC functions).

    If you want to update your Huds, then you have to move them outside the method

    level.onNotify("connected", fun(arguments) {
            // you can make new var
    
        var player = arguments[0];
    	var hud_text = gsc.newClientHudElem(arguments[0]);
    	hud_text.set("x", 540); //Approx middle of the screen
    	hud_text.set("y", 0);
    	hud_text.set("font", "hudbig");
    	hud_text.set("fontscale", 0.65);
    	hud_text.setText("^1Killstreak: 0");
            player.set("your_field_name", hud_text);
    });
    
    def onPlayerKilled(ePlayer, eInflictor, eAttacker, iDamage, sMeansOfDeath, sWeapon, vDir, sHitloc, iTimeOffset, iDeathAnimDuration)
    {
     // WARN: frequent access to fields may cause exceptions
     eAttacker.get("your_field_name").setText("^1Killstreak: " + eAttacker.get("kills"));
    // OR
    // eAttacker.get("your_field_name").setText("^1Killstreak: " + eAttacker.GetPlayerData("killstreaksState", "count"));
     ePlayer.get("your_field_name").setText("^1Killstreak: 0");
    }
    
    add_callback_player_killed(onPlayerKilled); //Copied this line from the pluto framework github
    

    I can’t check it, but in theory it should work, but you can modify the code yourself if necessary.

    the game already has some player data stores implemented

    var info = player.get("FIELD NAME");
    // FIELDS NAMES FOR ENTITY
    
    code_classname
    classname
    origin
    model
    spawnflags
    target
    targetname
    count
    health
    dmg 
    angles 
    birthtime
    script_linkname
    slidevelocity
    name
    sessionteam
    sessionstate
    maxhealth
    score 
    deaths
    statusicon 
    headicon 
    headiconteam 
    kills 
    assists 
    hasradar
    isradarblocked
    radarstrength 
    radarshowenemydirection
    radarmode 
    forcespectatorclient
    killcamentity
    killcamentitylookat
    archivetime
    psoffsettime
    pers
    veh_speed
    veh_pathspeed
    veh_transmission
    veh_pathdir
    veh_pathtype
    veh_topspeed
    veh_brake
    veh_throttle
    script_linkname
    script_noteworthy
    speed
    lookahead
    
    // FIELDS NAMES FOR HUDS
    x
    y
    z
    fontscale
    font
    alignx
    aligny
    horzalign
    vertalign
    color
    alpha
    label
    sort
    foreground
    lowresbackground
    hidewhendead
    hidewheninmenu
    glowcolor
    glowalpha
    archived
    hidein3rdperson
    

    if you do not need kills(killsrteaks only), you can use GetPlayerData

    var killsCount = player.GetPlayerData("killstreaksState", "count");
    
  • Thank you, I'll test it as soon as my one of my friends is online and will post an update.

  • I just wrote a little library to aid with this problem. You can check it here.

Log in to reply