Author Topic: Hunger Script not working correctly  (Read 2773 times)

So, viso made a hunger script for my hopefully upcoming hunger games server. When the code was executed, the jet bar showed up like it was supposed to, however, it didn't go down. Also, when I killed my self, the bar wasn't there anymore. I tried to fix/find the problem my self, but I couldn't find it. Please help.
Code: [Select]
function Player::HungerLoop(%this, %hunger)
{
    if(!isObject(%this)) //Return the function if nothing exists, usually happens when you call your client's player to use this function that doesn't exist or something like that
        return;
    
    //This runs on a schedule, this helps prevent multiple schedules running on an object, this is always recommended to do.
    cancel(%this.hungerLoopSch);
    
    if(%this.getState() $= "dead") //If it is dead do not continue the code (return it)
        return;
    
    if(%hunger <= 0)
        %this.addHealth(-1);
    else
        %hunger--; //Subtract the health by 1
    
    %this.hungerLoopSch = %this.schedule(1 * 5000, "HungerLoop", %hunger);
}
//Done.
//Because we don't have it called yet, it won't work, so we need to make a way for it to work.

package blah
{
    //This happens when a player spawns
    function Armor::onAdd(%armor, %playerObject)
    {
        Parent::onAdd(%armor, %playerObject);
        if(isObject(%client = %playerObject.client) && isObject(%minigame = %client.minigame)) //Do they have a client and a minigame?
            %playerObject.HungerLoop(100); //Call it, set their hunger to 100
     }
};
activatePackage("blah");
« Last Edit: December 10, 2015, 04:23:03 PM by Bow »

First of all. For the schedule time it's pointless to do 1 * 5000, because 1 * 5000 = 5000.

If you're using the energy bar to represent hunger level, then you'll need to set the value of the energy bar in the hunger loop function. Just call the setEnergyLevel function on the player. Setting it to %hunger should do.

As for the bar disappearing when you die. It disappears because you're no longer in control of your player object. You might be able to send a client command to the player to make it reappear if you really want to.

EDIT: Found the client command. It's clientCmdShowEnergyBar. Most likely has a boolean.
« Last Edit: December 10, 2015, 05:10:52 PM by jes00 »



If you're using the energy bar to represent hunger level, then you'll need to set the value of the energy bar in the hunger loop function. Just call the setEnergyLevel function on the player. Setting it to %hunger should do.


so do I add %this.setEnergyLevel(100);?
« Last Edit: December 10, 2015, 05:16:11 PM by Bow »

so do I add %this.setEnergyLevel(100);?
No. You add %this.setEnergyLevel(%hunger); Or else their energy bar will always be full. You want it to change with their hunger level.


it clearly doesn't matter anymore, but out of curiosity, why didn't you ask the guy who made it...?

it clearly doesn't matter anymore, but out of curiosity, why didn't you ask the guy who made it...?
because I felt like he would get annoyed after asking him to solve it.


Not really.
Really? You sometimes act annoyed when I ask for help. At least I saw it that way
« Last Edit: December 12, 2015, 02:24:34 PM by Bow »

EDIT: I am now scratching the idea of energy bar and now im using bottom print. Still having errors.
Code: [Select]
function Player::HungerLoop(%this, %hunger, %client)
{
    if(!isObject(%this)) //Return the function if nothing exists, usually happens when you call your client's player to use this function that doesn't exist or something like that
        return;
   
    //This runs on a schedule, this helps prevent multiple schedules running on an object, this is always recommended to do.
    cancel(%this.hungerLoopSch);
   
    if(%this.getState() $= "dead") //If it is dead do not continue the code (return it)
        return;


    if(%hunger <= 0)
        %this.addHealth(-1);
    else
        %hunger--;
bottomPrint(%client, "your hunger is" SPC %hunger, 2500);

   
    %this.hungerLoopSch = %this.schedule(5000, "HungerLoop", %hunger);
}
//Done.
//Because we don't have it called yet, it won't work, so we need to make a way for it to work.

package blah
{
    //This happens when a player spawns
    function Armor::onAdd(%armor, %playerObject)
    {
        Parent::onAdd(%armor, %playerObject);
        if(isObject(%client = %playerObject.client) && isObject(%minigame = %client.minigame)) //Do they have a client and a minigame?
            %playerObject.HungerLoop(100); //Call it, set their hunger to 100 //you might have used the wrong varible.
    }
};
activatePackage("blah");

EDIT: I am now scratching the idea of energy bar and now im using bottom print. Still having errors.

Your bottom print isn't showing for two reasons:

1. You haven't set %client to any client object on line 32. However, I would remove %client as an arg for the Player::HungerLoop() function then use %client = %this.client; to get the client object instead.

2. bottomPrint() takes seconds not milliseconds, 2500 is way too large.

Code: [Select]
    if(%hunger <= 0)
        %this.addHealth(-1);
    else
        %hunger--;
bottomPrint(%client, "your hunger is" SPC %hunger, 2500);
This part won't work how you think.

If you want an if/else statement to do more than one line of code then you have to enclose the lines with opening and closing brackets. Otherwise it'll just do the first line and not count the other lines as having anything to do with the statement, so it will always run the other lines.

If you want an if/else statement to do more than one line of code then you have to enclose the lines with opening and closing brackets. Otherwise it'll just do the first line and not count the other lines as having anything to do with the statement, so it will always run the other lines.

This is correct, but that indent directly left of bottomPrint is a single tab (not a single space). If you look at the other indents (highlight with mouse) you'll see that they are bundled spaces and not tabs.

What you should really be seeing is this:


« Last Edit: December 13, 2015, 07:30:22 AM by Crazycom »


still not working
Code: [Select]
function Player::HungerLoop(%this, %hunger)
{
%client = %this.client;

    if(!isObject(%this)) //Return the function if nothing exists, usually happens when you call your client's player to use this function that doesn't exist or something like that
        return;
    
    //This runs on a schedule, this helps prevent multiple schedules running on an object, this is always recommended to do.
    cancel(%this.hungerLoopSch);
    
    if(%this.getState() $= "dead") //If it is dead do not continue the code (return it)
        return;


    if(%hunger <= 0)
        %this.addHealth(-1);
    else
{
            %hunger--;
    bottomPrint(%client, "your hunger is" SPC %hunger, 5);
}
    
    %this.hungerLoopSch = %this.schedule(5000, "HungerLoop", %hunger);
}
//Done.
//Because we don't have it called yet, it won't work, so we need to make a way for it to work.

package blah
{
    //This happens when a player spawns
    function Armor::onAdd(%armor, %playerObject)
    {
        Parent::onAdd(%armor, %playerObject);
        if(isObject(%client = %playerObject.client) && isObject(%minigame = %client.minigame)) //Do they have a client and a minigame?
            %playerObject.HungerLoop(100); //Call it, set their hunger to 100 //you might have used the wrong varible.
    }
};
activatePackage("blah");
There were no syntax errors.
« Last Edit: December 13, 2015, 08:44:40 AM by Bow »