PDA

View Full Version : MODDING : How to make virtual money.



Killer.Pro
2nd September 2012, 11:31
Hey guys,
Today i'm going to show you how to make virtual money so that when you kill someone you get money.
For the tutorial,i am going to do it on TDM gametype.
So take out the tdm.gsc from main (main/iw_07.iwd/maps/mp/gametypes/tdm.gsc)
and drag it on your desktop.
Now in the tdm.gsc we're going to call a new thread for money when a player connects so
find :


Callback_PlayerConnect()

now here we're going to call new thread so put this code in there


self thread Money();

So from here a money thread is called but we have no money thread...Don't worry we're going to make one xD

so now above the


Callback_PlayerConnect()

put this code so it should look something like :


Money()
{
if(!isDefined(self.money)) // checks that money is already defined or not .
self.money = 0; //here is when a player will connect he'll get 0 money.You can change it as much as you want :)
}

Callback_PlayerConnect()
{

ok now that we've done that we're going to make money increase as someone kills
go into your tdm.gsc go find Callback_PlayerKilled(eInflictor, attacker, iDamage, sMeansOfDeath, sWeapon, vDir, sHitLoc, psOffsetTime, deathAnimDuration) in this function is where EVERY death is handled.
go find somewhere in that function where it says


else
{
attacker.score++;
teamscore = getTeamScore(attacker.pers["team"]);
teamscore++;
setTeamScore(attacker.pers["team"], teamscore);
checkScoreLimit();
}

here is where a player gets a kill so here we're going to give player 50 money.
so edit it so it should look something like this.


else
{
attacker.score++;
attacker.money += 50; //so player gets 50 money after a kill
teamscore = getTeamScore(attacker.pers["team"]);
teamscore++;
setTeamScore(attacker.pers["team"], teamscore);
checkScoreLimit();
}

Now that we've give the player money for every kill but if you go ingame now you wont see any difference
but i works, all we need to do now is to display money on screen
for that we'll have to make a hud
so in the start of tdm.gsc find this function


Callback_StartGameType()

you'll see under there that many things are precached
so we are going to precache our money hud

so put this code there


game["hud_money"] = &"-=^1MoneY^7=-";
precacheString(game["hud_money"]);

so now that we've precached the hud its time to make the hud
but first you have to call the hud
so find player spawn function


spawnplayer()

here we're going to call the hud so put this code in this function


self thread spawnmoneyhuds();

In this new thread we'll add the money hud so we're going to make this hud thread
just write this thread when the spawnplayer thread ends


spawnmoneyhuds()
{
self endon("disconnect");
self endon("joined_spectators");

if(!isDefined(self.moneyhud))
{
self.moneyhud = newClientHudElem(self);
self.moneyhud.x = 520;
self.moneyhud.y = 280;
self.moneyhud.alignX = "left";
self.moneyhud.alignY = "middle";
self.moneyhud.sort = 1;
self.moneyhud.alpha = 1;
self.moneyhud.fontScale = 1.2;
self.moneyhud.archived = true;
self.moneyhud setText(game["hud_money"]);
}

if(!isDefined(self.moneyVALUEhud))
{
self.moneyVALUEhud = newClientHudElem(self);
self.moneyVALUEhud.x = 600;
self.moneyVALUEhud.y = 275;
}

while(1)
{
wait(1.1);
self.moneyVALUEhud setvalue(self.money);
}
}

Don't forget to make the iwd file :p

now you've got some money in game..
In my next tutorial i'll show you guys to use this money to do some good :D
(Thanks liltc64,kungfooman,and all others)

EvoloZz
18th November 2012, 15:39
Is there any way to save the money?

DisSle
24th November 2012, 11:15
********EDITED:

For the tutorial,i am going to do it on TDM gametype.
So take out the tdm.gsc from main (main/iw_07.iwd/maps/mp/gametypes/tdm.gsc)
and drag it on your desktop.
Now in the tdm.gsc we're going to call a new thread for money when a player connects so
find :


Callback_PlayerConnect()

now here we're going to call new thread so put this code in there


self thread Money();

So from here a money thread is called but we have no money thread...Don't worry we're going to make one xD

so now above the


Callback_PlayerConnect()

put this code so it should look something like :


Money()
{
if(!isDefined(self.money)) // checks that money is already defined or not .
{
/*********************************
Disabled by Si13n7

self.money = 0; //here is when a player will connect he'll get 0 money.You can change it as much as you want :)

*********************************/

// Si13n7
self loadMoneyFile(200); // Change the 200 value to set the start money for new player
}
}

Callback_PlayerConnect()
{

ok now that we've done that we're going to make money increase as someone kills
go into your tdm.gsc go find Callback_PlayerKilled(eInflictor, attacker, iDamage, sMeansOfDeath, sWeapon, vDir, sHitLoc, psOffsetTime, deathAnimDuration) in this function is where EVERY death is handled.
go find somewhere in that function where it says


else
{
attacker.score++;
teamscore = getTeamScore(attacker.pers["team"]);
teamscore++;
setTeamScore(attacker.pers["team"], teamscore);
checkScoreLimit();
}

here is where a player gets a kill so here we're going to give player 50 money.
so edit it so it should look something like this.


else
{
attacker.score++;
attacker.money += 50; //so player gets 50 money after a kill
teamscore = getTeamScore(attacker.pers["team"]);
teamscore++;
setTeamScore(attacker.pers["team"], teamscore);
checkScoreLimit();

// Si13n7
attacker manageMoneyFile(); // Create and update the saved file with every kill
}

Now that we've give the player money for every kill but if you go ingame now you wont see any difference
but i works, all we need to do now is to display money on screen
for that we'll have to make a hud
so in the start of tdm.gsc find this function


Callback_StartGameType()

you'll see under there that many things are precached
so we are going to precache our money hud

so put this code there


game["hud_money"] = &"-=^1MoneY^7=-";
precacheString(game["hud_money"]);

so now that we've precached the hud its time to make the hud
but first you have to call the hud
so find player spawn function


spawnplayer()

here we're going to call the hud so put this code in this function


self thread spawnmoneyhuds();

In this new thread we'll add the money hud so we're going to make this hud thread
just write this thread when the spawnplayer thread ends


spawnmoneyhuds()
{
self endon("disconnect");
self endon("joined_spectators");

if(!isDefined(self.moneyhud))
{
self.moneyhud = newClientHudElem(self);
self.moneyhud.x = 520;
self.moneyhud.y = 280;
self.moneyhud.alignX = "left";
self.moneyhud.alignY = "middle";
self.moneyhud.sort = 1;
self.moneyhud.alpha = 1;
self.moneyhud.fontScale = 1.2;
self.moneyhud.archived = true;
self.moneyhud setText(game["hud_money"]);
}

if(!isDefined(self.moneyVALUEhud))
{
self.moneyVALUEhud = newClientHudElem(self);
self.moneyVALUEhud.x = 600;
self.moneyVALUEhud.y = 275;
}

while(1)
{
wait(1.1);
self.moneyVALUEhud setvalue(self.money);
}
}

Don't forget to make the iwd file :p

now you've got some money in game..



********ADDED:

Add few threads to the end...



loadMoneyFile(startMoney)
{
self endon("disconnect");
if(!isPlayer(self))
return;

guid=self getGuid();
if(!isDefined(guid) || guid==0)
return;

if(!isDefined(startMoney))
startMoney=0;

fid=openFile("money\\"+guid+".acc","read");
if(fid!=-1)
{
argcount=fReadLn(fid);
linenum=0;
while(argcount>0)
{
if(linenum>0 && argcount==2)
if(guid==int(fGetArg(fid,0)))
self.money=int(fGetArg(fid,1));

argcount=fReadLn(fid);
linenum++;
}
closeFile(fid);
}
else
self.money=startMoney;
}

manageMoneyFile()
{
if(!isPlayer(self))
return;

guid=self getGuid();
if(!isDefined(guid) || guid==0)
return;

fid=openFile("money\\"+guid+".acc","write");
closeFile(fid);

fid=openFile("money\\"+guid+".acc","append");
fPrintLn(fid,"Money-Account");
fPrintLn(fid,"\n"+guid+","+self.money);
closeFile(fid);
}


It's not for cracked servers because nobody have a guid. And I wrote that very fast and I didn't tested. But I think, it will still work. ;)

IzNoGoD
24th November 2012, 11:27
if(fid==0)
{
argcount=fReadLn(fid);
linenum=0;
while(argcount>0)
{
if(linenum>0 && argcount==2)
if(guid==int(fGetArg(fid,0)))
self.money=int(fGetArg(fid,1));

argcount=fReadLn(fid);
linenum++;
}
}
else
self.money=startMoney;

Please replace the first check with a check for -1 instead of 0. So, if(fid != -1)

Also, this looks like the reading file function is ripped DIRECTLY out of my account system:



//MY code.
argcount=freadln(fid);
linenum=0;
self.stats=[];
while(argcount!=0)
{
if(linenum>0)
self.stats[fgetarg(fid,0)]=int(fgetarg(fid,1));
argcount=freadln(fid);
linenum++;
}

No worries, please just add credits and it will be fine.

DisSle
24th November 2012, 12:30
Replaced, you was right with the changes.

But what you mean about the code... I ask me... you're really arrogant, right?
I saw only one of your mods and it was Defrag and I never liked. To be honest! ;)
Only the idea I like because it was the best mod for Q3.

CoD2 is dead for me. I interest me for programming for everything shit. So I'm not interest for other mods, only for programming itself. I have no reason to look other mod codes, only for help others and that since a long time. So shut up, I'm not a small kid! And I will not add your credits!

IzNoGoD
24th November 2012, 15:42
I mean that the code you posted is definitely written by me, or based on the code I wrote.

The file handling is so goddamn similar that it cannot be a coincidence. Why else would you create a useless line of "Money-Account" in your .acc file?

DisSle
25th November 2012, 21:59
Right, and guid arg is useless too. I really like feedback, but you behave like a child.
But OK... If the baby cry, I'll try to help...

********THREADS UPDATED:


loadMoneyFile(start)
{
if(!isPlayer(self))
return;

guid=self getGuid();
if(!isDefined(guid) || guid==0)
return;

if(!isDefined(start))
start=0;

fid=openFile("money\\"+guid+".acc","read");
if(fid!=-1)
{
self.money=int(fGetArg(fid,0));
closeFile(fid);
}
else // If no saved file found
{
self.money=start;
self manageMoneyFile(); // Create and update the saved file with every kill
}
}

manageMoneyFile()
{
if(!isPlayer(self))
return;

guid=self getGuid();
if(!isDefined(guid) || guid==0)
return;

fid=openFile("money\\"+guid+".acc","write");
closeFile(fid);

fid=openFile("money\\"+guid+".acc","append");
fPrintLn(fid,self.money);
closeFile(fid);
}

Sense
28th November 2012, 19:37
I agree with izno, it is his code.

Mitch
29th November 2012, 15:44
********EDITED:

For the tutorial,i am going to do it on TDM gametype.
So take out the tdm.gsc from main (main/iw_07.iwd/maps/mp/gametypes/tdm.gsc)
and drag it on your desktop.
Now in the tdm.gsc we're going to call a new thread for money when a player connects so
find :


Callback_PlayerConnect()

now here we're going to call new thread so put this code in there


self thread Money();

So from here a money thread is called but we have no money thread...Don't worry we're going to make one xD

so now above the


Callback_PlayerConnect()

put this code so it should look something like :


Money()
{
if(!isDefined(self.money)) // checks that money is already defined or not .
{
/*********************************
Disabled by Si13n7

self.money = 0; //here is when a player will connect he'll get 0 money.You can change it as much as you want :)

*********************************/

// Si13n7
self loadMoneyFile(200); // Change the 200 value to set the start money for new player
}
}

Callback_PlayerConnect()
{

ok now that we've done that we're going to make money increase as someone kills
go into your tdm.gsc go find Callback_PlayerKilled(eInflictor, attacker, iDamage, sMeansOfDeath, sWeapon, vDir, sHitLoc, psOffsetTime, deathAnimDuration) in this function is where EVERY death is handled.
go find somewhere in that function where it says


else
{
attacker.score++;
teamscore = getTeamScore(attacker.pers["team"]);
teamscore++;
setTeamScore(attacker.pers["team"], teamscore);
checkScoreLimit();
}

here is where a player gets a kill so here we're going to give player 50 money.
so edit it so it should look something like this.


else
{
attacker.score++;
attacker.money += 50; //so player gets 50 money after a kill
teamscore = getTeamScore(attacker.pers["team"]);
teamscore++;
setTeamScore(attacker.pers["team"], teamscore);
checkScoreLimit();

// Si13n7
attacker manageMoneyFile(); // Create and update the saved file with every kill
}

Now that we've give the player money for every kill but if you go ingame now you wont see any difference
but i works, all we need to do now is to display money on screen
for that we'll have to make a hud
so in the start of tdm.gsc find this function


Callback_StartGameType()

you'll see under there that many things are precached
so we are going to precache our money hud

so put this code there


game["hud_money"] = &"-=^1MoneY^7=-";
precacheString(game["hud_money"]);

so now that we've precached the hud its time to make the hud
but first you have to call the hud
so find player spawn function


spawnplayer()

here we're going to call the hud so put this code in this function


self thread spawnmoneyhuds();

In this new thread we'll add the money hud so we're going to make this hud thread
just write this thread when the spawnplayer thread ends


spawnmoneyhuds()
{
self endon("disconnect");
self endon("joined_spectators");

if(!isDefined(self.moneyhud))
{
self.moneyhud = newClientHudElem(self);
self.moneyhud.x = 520;
self.moneyhud.y = 280;
self.moneyhud.alignX = "left";
self.moneyhud.alignY = "middle";
self.moneyhud.sort = 1;
self.moneyhud.alpha = 1;
self.moneyhud.fontScale = 1.2;
self.moneyhud.archived = true;
self.moneyhud setText(game["hud_money"]);
}

if(!isDefined(self.moneyVALUEhud))
{
self.moneyVALUEhud = newClientHudElem(self);
self.moneyVALUEhud.x = 600;
self.moneyVALUEhud.y = 275;
}

while(1)
{
wait(1.1);
self.moneyVALUEhud setvalue(self.money);
}
}

Don't forget to make the iwd file :p

now you've got some money in game..



********ADDED:

Add few threads to the end...



loadMoneyFile(startMoney)
{
self endon("disconnect");
if(!isPlayer(self))
return;

guid=self getGuid();
if(!isDefined(guid) || guid==0)
return;

if(!isDefined(startMoney))
startMoney=0;

fid=openFile("money\\"+guid+".acc","read");
if(fid!=-1)
{
argcount=fReadLn(fid);
linenum=0;
while(argcount>0)
{
if(linenum>0 && argcount==2)
if(guid==int(fGetArg(fid,0)))
self.money=int(fGetArg(fid,1));

argcount=fReadLn(fid);
linenum++;
}
closeFile(fid);
}
else
self.money=startMoney;
}

manageMoneyFile()
{
if(!isPlayer(self))
return;

guid=self getGuid();
if(!isDefined(guid) || guid==0)
return;

fid=openFile("money\\"+guid+".acc","write");
closeFile(fid);

fid=openFile("money\\"+guid+".acc","append");
fPrintLn(fid,"Money-Account");
fPrintLn(fid,"\n"+guid+","+self.money);
closeFile(fid);
}


It's not for cracked servers because nobody have a guid. And I wrote that very fast and I didn't tested. But I think, it will still work. ;)

You can add text and value in one hud element. There is no need for two huds.



self.moneyhud = newClientHudElem(self);
self.moneyhud.label = &"MyPreCachedText";
self.moneyhud setValue(1337);


About this tutorial in general, i think it would be better if you start simple. For example, start with creating and destroying the hud. And after that make a part about the money.

Edit: Advice about openFile function. When you open a file and you don't get -1 always close this file after you are done with it. Because if you don't your server is gonna crash on the next map.

EvoloZz
8th December 2012, 16:31
********EDITED:


spawnmoneyhuds()
{
self endon("disconnect");
self endon("joined_spectators");

if(!isDefined(self.moneyhud))
{
self.moneyhud = newClientHudElem(self);
self.moneyhud.x = 520;
self.moneyhud.y = 280;
self.moneyhud.alignX = "left";
self.moneyhud.alignY = "middle";
self.moneyhud.sort = 1;
self.moneyhud.alpha = 1;
self.moneyhud.fontScale = 1.2;
self.moneyhud.archived = true;
self.moneyhud setText(game["hud_money"]);
}

if(!isDefined(self.moneyVALUEhud))
{
self.moneyVALUEhud = newClientHudElem(self);
self.moneyVALUEhud.x = 600;
self.moneyVALUEhud.y = 275;
}

while(1)
{
wait(1.1);
self.moneyVALUEhud setvalue(self.money);
}
}

Script compile error in line 622: wait(1.1);
Whats wrong?

kung foo man
8th December 2012, 17:25
I dont see any error there, but can you try to comment lines out and see, if it still triggers the error?

igstyga
16th June 2020, 20:40
Only 8 years late to the thread... I'm working on a similar mod but for S&D gametype. The problem is that player.money is set to 0 each round because PlayerConnect is being called after each spawn. No clue on that...
I've solved it by saving the money into server cvars, but doesn't seem like an elegant solution.

Anyone has a better idea?

IzNoGoD
16th June 2020, 21:35
Only 8 years late to the thread... I'm working on a similar mod but for S&D gametype. The problem is that player.money is set to 0 each round because PlayerConnect is being called after each spawn. No clue on that...
I've solved it by saving the money into server cvars, but doesn't seem like an elegant solution.

Anyone has a better idea?

Store it in player.pers["something"] variable, load it back on playerconnect if it's defined. player.pers array gets stored over multiple rounds.

maxdamage99
18th June 2020, 07:43
Only 8 years late to the thread... I'm working on a similar mod but for S&D gametype. The problem is that player.money is set to 0 each round because PlayerConnect is being called after each spawn. No clue on that...
I've solved it by saving the money into server cvars, but doesn't seem like an elegant solution.

Anyone has a better idea?

You can try storing money in global variables, I don’t know how good this idea is :)


getID()
{
return self getEntityNumber();
}

onPlayerConnect()
{
id = self getID();
level.money[id] = 0;
}

onPlayerDisconnect()
{
id = self getID();
level.money[id] = undefined;
}

Killed(/*...*/)
{
if (mod == "headshot")
money = 100;
else if (mod == "melee")
money = 200;
else
money = 50;

id = self getID();
level.money[id] += money;
}

IzNoGoD
18th June 2020, 07:53
You can try storing money in global variables, I don’t know how good this idea is :)


getID()
{
return self getEntityNumber();
}

onPlayerConnect()
{
id = self getID();
level.money[id] = 0;
}


Bad idea. The level var is also cleaned (except for level.game array) on round restart. Furthermore, since players go through the "playerconnecting" callback when the round restarts, their money gets reset using this technique.

Just store in player.pers["money"] or something similar. Look at the stock sd gametype for .pers["kills"] for example.