Author Topic: Point taxing doesn't save  (Read 1170 times)

This is a mod made by Fluffy - basically, every 10 minutes you are taxed a small percentage of your points. Your points are stored as a client variable used in VCE and are used to buy stuff on my server. The problem is, after you get taxed - it doesn't save. You can load your game again by clicking a brick on my server and you'll get the points back that you lost from tax. Let me just say that I am not deleting this load brick, because it also loads lots of other information.

Quote
$Interest::CutOff[0] = "100";
$Interest::Percentage[0] = "1";
$Interest::CutOff[2] = "20000";
$Interest::Percentage[2] = "4";
$Interest::TickTime = 10;

function intrestTick()
{
    cancel($interestTickShed);
    $interestTickShed = schedule(60000, 0, intrestTick);
    $InterestTickCount++;
    
    if($InterestTickCount < ($Interest::TickTime - 1))
        return;
    
    for(%i = 0; %i < clientGroup.getCount(); %i++)
    {
        %client = clientGroup.getObject(%i);
        %var = VariableGroup_8294.getVariabl e("Client", "points", %client);
        %num = %var;
        %per = 0;
        
        if(%var > $Interest::CutOff[0])
        {
            %per = %var * (0.01 * $Interest::Percentage[0]);
            %num = %var - %per;

        }
        
        if(%var > $Interest::CutOff[2])
        {
            %per = %var * (0.01 * $Interest::Percentage[2]);
            %num = %var - %per;

        }
        
        %num = mCeil(%num);
        %per = mCeil(%per);
        
        messageClient(%client, '', "\c2You have been taxed \c6" @ %per @ "\c2 points.");
        messageClient(%client, '', "\c2You now have a total of \c6" @ %num @ "\c2 points.");
        VariableGroup_8294.setVariabl e("Client", "points", %num, %client);
        %client.setScore(%num);
    }
    
    VariableGroup_8294.saveAllVar iables($VCE::Server::SavePath);
    $InterestTickCount = 0;
}

intrestTick();

That's the code. I'm going to take a swing that the following line has been giving me the business:
    VariableGroup_8294.saveAllVar iables($VCE::Server::SavePath);

So if anybody can find a way so that your points are saved straight after tax by fixing up this code, I'd be grateful.

I can't ask Fluffy because he's away.
« Last Edit: April 08, 2012, 12:51:31 PM by Big Brother »

man, waste of time. no help. :C

well your ID is 8294 and that number is 8294 so I assume it's saving only yours

I don't actually know how variable groups work though so don't quote me on anything  :cookieMonster:

This is why I don't approve of using VCE for gamemodes, especially when you mix it with script, it just complicates things.
To save your data, either write your own saving function, or use a default feature

well your ID is 8294 and that number is 8294 so I assume it's saving only yours

I don't actually know how variable groups work though so don't quote me on anything  :cookieMonster:
No, silly. It doesn't work on me either.

No, silly. It doesn't work on me either.

Just use file objects and like HeadCrab said, ditch VCE.

Just use file objects and like HeadCrab said, ditch VCE.
OK I'll say this just once, if I ditch VCE - I'd have to remake EVERYTHING. That means everything on my server will be wrong and I'd need to fix it. I am not willing to do this.

The save button on your server, does it use a separate event add-on?

no, just saves with VCE

What is the output event name?

saveVariable or something?


OK I'll say this just once, if I ditch VCE - I'd have to remake EVERYTHING. That means everything on my server will be wrong and I'd need to fix it. I am not willing to do this.
Taking the time to improve your code is part of being a good programmer. It won't take long (rewriting this function would have taken less than 5 minutes if I wasn't looking for/fixing other issues), and it will potentially save many headaches, as well as save everyone the hassle of clicking save and load bricks. Using a better method will allow you to modify the values directly, and save only the ones needed, only when needed, instead of constantly saving everything

Issues/Suggestions (some are fixed in the code I gave you, some are not)

I do like that you made global variables instead of magic numbers, but if this is a bigger project with many more, I would give them all a common prefix, for example, $RP::Interest::* (I'll call it RP because I have no idea what to call it)

If the client's score is greater than Interest::CutOff[2] they will be taxed twice, once according to Percentage[0] and once according to Percentage[2]. (fixed)

Why are you setting InterestTickCount to 0? Doing this, combined with not taxing if InterestTickCount is less than 10, results in running the function every minute, but only actually doing anything every 10 minutes.

You misspelled interest in several places (fixed)

Code: [Select]
$Interest::CutOff[0] = "100";
$Interest::Percentage[0] = "1";
$Interest::CutOff[2] = "20000";
$Interest::Percentage[2] = "4";
$Interest::TickTime = 10;

function interestTick()
{
    cancel($interestTickShed);
    $interestTickShed = schedule(60000, 0, interestTick);
    $InterestTickCount++;
    
    if($InterestTickCount < ($Interest::TickTime - 1))
        return;
    
    for(%i = 0; %i < clientGroup.getCount(); %i++)
    {
        %client = clientGroup.getObject(%i);
%score = %client.score;

        
        if(%score > $Interest::CutOff[0])
            %per = %score * (0.01 * $Interest::Percentage[0]);
        else if(%score > $Interest::CutOff[2])
%per = %score * (0.01 * $Interest::Percentage[2]);
        
        
        %per = mCeil(%per);
%score-=%per;
        
        messageClient(%client, '', "\c2You have been taxed \c6" @ %per @ "\c2 points.");
        messageClient(%client, '', "\c2You now have a total of \c6" @ %num @ "\c2 points.");
        %client.setScore(%num);
    }
    
    $InterestTickCount = 0;
}
interestTick();
You'd also want to to put this in server.cs
Code: [Select]
LoadRequiredAddOn("Script_Player_Persistence");
if(isFunction(RegisterPersistenceVar))
{
        RegisterPersistenceVar("rp", true, "");
       // RegisterPersistenceVar("someOtherVariable", false, "");
}
And then every client field you create for your RP will be named starting with rp, and PlayerPersistence will take care of all saving and loading for you.
You could change rp to something else too.
For default fields that you want saved, but are not saved by the default Player Persistance, add lines similar to commented out second RegisterPersistenceVar function.
Keep in mind the default Player Persistence will save some fields you may not want saved, I'm not sure how to make it unregister them
« Last Edit: April 09, 2012, 07:07:28 PM by Headcrab Zombie »

Thank you for your help - keep in mind that Fluffy coded it and I believe he purposefully mis-spelled interest so it reduced the chances of colliding with an existing mod. Players are given wrenches to help them build, but if it runs off of score they can manipulate it and give themselves points.
« Last Edit: April 09, 2012, 05:28:09 PM by Big Brother »

I believe he purposefully mis-spelled interest so it reduced the chances of colliding with an existing mod.
Instead of misspelling things and causing confusion, name functions with a common prefix, similar to my suggestion for global variables

Players are given wrenches to help them build, but if it runs off of score they can manipulate it and give themselves points.
Couldn't the same be done with VCE?
I thought score is what you wanted because of the setScore call
You could just change any reference of %client.score to %client.points, and add a RegisterPersistenceVar call - RegisterPersistenceVar("points", false, "");