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?