Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Timer problems
Rahmerh Offline
Member

Posts: 66
Threads: 7
Joined: Jun 2015
Reputation: 3
#1
Timer problems

Hey!
I'm working on a semi-complicated script, but it doesn't work somehow. I'll try to explain it:
I left out quite a bit of code that didn't have anything to do with the timers.

You start off with 2 timers. Let's call them Timer1 and Timer2.
When you walk through a certain script area, you trigger Timer1. When you trigger Timer1, it counts for 120 seconds, before it calls Timer2. Then Timer2 will count for 120 seconds and call Timer1 again. An infinite loop.
But when the player interacts with something, it should cancel both timers, so they won't run again and tigger a third timer. (We can call that Timer3 I guess) Timer3 then runs for a long time (600 seconds), before calling Timer1 again which triggers the infinite loop again.

Each time Timer1 and Timer2 are called, they give the player damage, to indicate 120 seconds have passed. But when you cancel both Timer1 and Timer2, and call Timer3, somehow Timer1 or Timer2 starts running again, without waiting the 600 seconds, and I have no idea why.

Here's the code for Timer1 and Timer2.
void Timer1(string &in asTimer){
    AddTimer("", 0.8f, "givedamage");
    AddTimer("timerb", 120, "Timer2");
}

void Timer2(string &in asTimer){
    AddTimer("", 0.8f, "givedamage");
    AddTimer("timerd", 120, "Timer1");
}

I'm using a timer to give the damage because that works better in the story.

Then when the player interacts with the object, this is called:

void food1(string &in asEntity){
    RemoveTimer("timerb");
    RemoveTimer("timberd");
        startTimerThree();
}

void startTimerThree(){
       AddTimer("Timer3", 600, "Timer1");
}

I don't think I missed anything.
Does anyone have a clue what I'm doing wrong?

Full code:
Spoiler below!
void OnStart(){
    //StartUp();
    //Local variables
    SetLocalVarInt("check1", 0);
    SetLocalVarInt("movingthrough", 0);
    SetLocalVarInt("bricksdoor", 0);
    SetLocalVarInt("meatcheck", 0);
    SetPlayerLampOil(0);
    SetPlayerSanity(70);
    
    //MEAT!
    AddEntityCollideCallback("butcher_knife_1", "ScriptArea_8", "cuttingmeat", false, 1);
    //SetEntityPlayerInteractCallback("", "", true);
    
    //Picking up items setmessage
    SetEntityPlayerInteractCallback("stone_chipper_1", "chiselPickup", false);
    
    //Getting out of the cage
    AddUseItemCallback("", "hollow_needle_1", "prison_section_plain_2", "openthatdoor1", true);
    
    //Throwing a chair at the door
    AddEntityCollideCallback("chair_wood02_1", "ScriptArea_2", "doorwontbreak1", false, 1);
    AddEntityCollideCallback("chair_wood02_2", "ScriptArea_2", "doorwontbreak1", false, 1);
    
    //Moving through that darn hole
    SetEntityPlayerLookAtCallback("ScriptArea_3", "barLoose", false);
    AddUseItemCallback("", "bone_saw_1", "wriggle_prison_bar_obj_dyn_1", "sawthatthing1", true);
    SetEntityPlayerInteractCallback("ScriptArea_4", "moveThrough", false);
    SetEntityPlayerLookAtCallback("ScriptArea_4", "sure", false);
    
    //Check door is locked
    SetEntityPlayerInteractCallback("prison_section_plain_2", "isdoorlocked1", false);
    
    //Door lockpicking    
    AddUseItemCallback("", "key_study_1", "prison_1", "notrightkey1", true);
    AddUseItemCallback("", "hollow_needle_1", "prison_1", "notrightneedle1", true);
    AddUseItemCallback("", "stone_chipper_1", "prison_1", "chiselthatdoor", true);
    AddUseItemCallback("", "wriggle_prison_bar_obj_dyn_2", "block_box_nonchar_3", "jamthatchisel", true);
    
    //Hunger
    AddEntityCollideCallback("Player", "ScriptArea_6", "hunger1", true, 1);
    AddEntityCollideCallback("Player", "ScriptArea_5", "hunger1", true, 1);
    SetEntityPlayerInteractCallback("ScriptArea_7", "food1", true);
}

void OnLeave(){
    SetGlobalVarFloat("hungertimer", GetTimerTimeLeft("starvation"));    
}

//Hunger
void applesLookAt(string &in asEntity, int alState){
    if(alState == 1){
        SetMessage("Messages", "apples", 2);
    }
}

///This starts it off
void hunger1(string &in asParent, string &in asChild, int alState){
    SetPlayerMoveSpeedMul(0.3f);
    FadePlayerRollTo(50, 10, 8);
    AddTimer("", 2, "getbetter");
    FadeImageTrailTo(1, 1.5f);
    PlayGuiSound("stomach-growl.snt", 1);
    SetEntityActive("ScriptArea_5", false);
    SetEntityActive("ScriptArea_6", false);
    AddTimer("", 20, "foodhurt");
}

void getbetter(string &in asTimer){
    FadePlayerRollTo(0, 33, 33);
    SetMessage("Messages", "hunger1", 2);
    SetPlayerMoveSpeedMul(0.7f);
    FadeImageTrailTo(1.5f, 1);
}

///Damage from hunger and they start the cycle of taking damage and screen effects
void foodhurt(string &in asTimer){
    FadePlayerRollTo(20, 4, 5);
    SetPlayerMoveSpeedMul(0.5f);
    FadeImageTrailTo(0.6f, 1);
    AddTimer("foodhurt11", 1.5f, "foodhurtdone");
    AddTimer("", 0.8f, "givedamage");
    AddTimer("foodhurt2", 120, "foodhurt1");
}

void foodhurt1(string &in asTimer){
    FadePlayerRollTo(20, 4, 5);
    SetPlayerMoveSpeedMul(0.5f);
    FadeImageTrailTo(0.6f, 1);
    AddTimer("foodhurt3", 1.5f, "foodhurtdone");
    AddTimer("", 0.8f, "givedamage");
    AddTimer("foodhurt4", 120, "foodhurt");
}

///Time between hunger periods
void startGlobalHungerTimer(){
    AddTimer("starvation", 600, "foodhurt");
}

///Walk around hungry
void foodhurtdone(string &in asTimer){
    SetPlayerMoveSpeedMul(0.5f);
    FadeImageTrailTo(0.6f, 1);
    FadePlayerRollTo(0, 4, 9);
}

///Function for damage
void givedamage(string &in asTimer){
    GivePlayerDamage(5, "", false, true);
    GiveSanityDamage(5, false);
    PlayGuiSound("react_breath_slow.snt", 10);
    int randomint = RandInt(0, 2);
    switch(randomint){
        case 0: SetMessage("Messages", "Hungry1", 2); break;
        case 1: SetMessage("Messages", "Hungry2", 2); break;
        case 2: SetMessage("Messages", "Hungry3", 2); break;
    }
}

///You just ate
void food1(string &in asEntity){
    RemoveTimer("foodhurt2");
    RemoveTimer("foodhurt4");
    FadeOut(0.7f);
    AddTimer("", 1, "eating1");
}

void eating1(string &in asTimer){
    FadePlayerRollTo(0, 33, 33);
    FadeIn(2);
    GiveSanityBoostSmall();
    AddPlayerHealth(20);
    SetPlayerMoveSpeedMul(1);
    FadeImageTrailTo(0, 0.5f);
    PlayGuiSound("eating-apple.snt", 10);
    SetEntityActive("ScriptArea_7", false);
    SetMessage("Messages", "IAte", 2);
    startGlobalHungerTimer();
}

//MEAT!
void cuttingmeat(string &in asParent, string &in asChild, int alState){
    if(GetLocalVarInt("meatcheck")==0){
        AddLocalVarInt("meatcheck", 1);
        SetPlayerActive(false);
        FadeOut(0.5f);
        AddTimer("", 1.5f, "cuttingmeat2");
        PlaySoundAtEntity("cuttingmeat", "21_meat.snt", "carcass_1", 0.1f, false);
    }else if(alState==1){
        SetMessage("Messages", "notenough1", 2);
    }
}

void cuttingmeat2(string &in asTimer){
    SetEntityActive("fresh_meat_1", true);
    PlaySoundAtEntity("cuttingmeat", "pick_meat.snt", "carcass_1", 0.1f, false);
    FadeIn(2);
    SetPlayerActive(true);
    SetPropStaticPhysics("carcass_1", false);
    SetPropHealth("carcass_1", 0);
}

//Picking up items setmessage
void chiselPickup(string &in asEntity){
    AddTimer("", 3, "chiselPickup2");
}

void chiselPickup2(string &in asTimer){
    SetMessage("Messages", "chiseldoor1", 3);
}

//Door lockpicking
void jamthatchisel(string &in asItem, string &in asEntity){
    SetEntityActive("wriggle_prison_bar_1", true);
    AddTimer("", 0.5f, "jamthatchisel2");
}

void jamthatchisel2(string &in asTimer){
    SetLeverStuckState("wriggle_prison_bar_1", -1, true);
    AddPropForce("wriggle_prison_bar_1", 0, 0, 100, "world");
    CreateParticleSystemAtEntity("impact2", "ps_impact_dirt_high.ps", "stone_chipper_2", true);
    GiveSanityBoost();
    PlaySoundAtEntity("chiselimpact", "impact_metal_high.snt", "wriggle_prison_bar_1", 0.1f, false);
    AddTimer("", 0.2f, "jamthatchisel3");
}

void jamthatchisel3(string &in asTimer){
    SetEntityActive("stone_chipper_2", false);
    SetSwingDoorLocked("prison_1", false, true);
    SetEntityActive("wriggle_prison_bar_1", false);
    SetPropStaticPhysics("prison_1", false);
    SetEntityActive("block_box_nonchar_3", false);
}

void notrightkey1(string &in asItem, string &in asEntity){
    SetMessage("Messages", "notrightkey1", 2);
}

void notrightneedle1(string &in asItem, string &in asEntity){
    SetMessage("Messages", "notrightneedle1", 3);
}

void chiselthatdoor(string &in asItem, string &in asEntity){
    SetEntityActive("stone_chipper_2", true);
    SetMessage("Messages", "chiselmore", 2);
    RemoveItem(asItem);
    SetEntityInteractionDisabled(asItem, true);
    SetEntityActive("block_box_nonchar_3", true);
    SetPropStaticPhysics(asEntity, true);
}

//Getting out of the cage
void openthatdoor1(string &in asItem, string &in asEntity){
    SetPropStaticPhysics("padlock_2", true);
    SetEntityActive("hollow_needle_2", true);
    PlaySoundAtEntity("unlocksound", "joints_cm_move.snt", "padlock_2", 0.1f, false);
    AddTimer("", 0.8f, "unlockthedoor1");
}

void unlockthedoor1(string &in asTimer){
    PlaySoundAtEntity("unlockpadlock", "unlock_door.snt", "padlock_2", 0.1f, false);
    StopSound("unlocksound", 0.1f);
    SetEntityActive("padlock_2", false);
    SetEntityActive("padlock_3", true);
    SetEntityActive("hollow_needle_2", false);
    SetSwingDoorLocked("prison_section_plain_2", false, true);
}

void needlepickup1(string &in asEntity){
    SetMessage("Messages", "needlepickup1", 3);
}

//Check door is locked
void isdoorlocked1(string &in asEntity){
    if(GetSwingDoorLocked("prison_section_plain_2")){
        SetMessage("Messages", "lockeddoor", 2);
    }
}

//Moving through that darn hole
void sawthatthing1(string &in asItem, string &in asEntity){
    FadeOut(0.7f);
    AddTimer(asEntity, 0.8f, "sawthatthing");
}

void sawthatthing(string &in asTimer){
    PlaySoundAtEntity("sawsound", "handsaw.snt", asTimer, 0.5f, false);
    AddTimer(asTimer, 3.5f, "wakeup1");
}

void wakeup1(string &in asTimer){
    FadeIn(0.7f);
    SetEntityActive(asTimer, false);
    SetEntityActive("ScriptArea_3", false);
    SetEntityActive("ScriptArea_4", true);
    SetEntityActive("wriggle_prison_bar_obj_dyn_2", true);
    StopSound("sawsound", 0.1f);
}

void sure(string &in asEntity, int alState){
    if(alState == 1){
        SetMessage("Messages", "sure", 2);
    }
}

void moveThrough(string &in asEntity){
    SetPlayerActive(false);
    if(GetLocalVarInt("movingthrough")==0){
        SetPlayerCrouching(true);
        AddTimer("", 0.5f, "down1");
        FadeOut(0.2f);
        AddLocalVarInt("movingthrough", 1);
    }else{
        SetPlayerCrouching(true);
        AddTimer("", 0.5f, "down2");
        FadeOut(0.2f);
        AddLocalVarInt("movingthrough", -1);
    }
}

//Move back
void down2(string &in asTimer){
    TeleportPlayer("PlayerStartArea_4");
    FadeIn(0.2f);
    MovePlayerHeadPos(0, -0.3f, 0, 1, 0.1f);
    AddTimer("", 0.5f, "forward2");
}

void forward2(string &in asTimer){
    MovePlayerHeadPos(0, 0, 0.8f, 1, 1);
    AddTimer("", 1, "final3");
    PlayGuiSound("step_run_rock_rev.snt", 1);
}

void final3(string &in asTimer){
    FadeOut(0.2f);
    AddTimer("", 0.2f, "final4");
}

void final4(string &in asTimer){
    FadeIn(0.2f);
    SetPlayerActive(true);
    ChangePlayerStateToNormal();
    TeleportPlayer("PlayerStartArea_5");
    MovePlayerHeadPos(0, 0, 0, 10, 0.1f);
}

//Move through
void down1(string &in asTimer){
    TeleportPlayer("PlayerStartArea_2");
    FadeIn(0.2f);
    MovePlayerHeadPos(0, -0.4f, 0, 0.5f, 0.1f);
    AddTimer("", 0.5f, "forward1");
}

void forward1(string &in asTimer){
    MovePlayerHeadPos(0, 0, 0.8f, 1, 1);
    AddTimer("", 1, "final1");
    PlayGuiSound("step_run_rock_rev.snt", 1);
}

void final1(string &in asTimer){
    FadeOut(0.2f);
    AddTimer("", 0.2f, "final2");
}

void final2(string &in asTimer){
    FadeIn(0.2f);
    SetPlayerActive(true);
    ChangePlayerStateToNormal();
    TeleportPlayer("PlayerStartArea_3");
    MovePlayerHeadPos(0, 0, 0, 10, 0.1f);
}

void barLoose(string &in asEntity, int alState){
    if(alState == 1)          
    {
        SetMessage("Messages", "barloose", 2);
    }
}

//Throwing a chair at the door
void doorwontbreak1(string &in asParent, string &in asChild, int alState){
    CreateParticleSystemAtEntity("impact", "ps_impact_dirt_high.ps", asParent, true);
    SetMessage("Messages", "wontworkbuddy", 2);
}

//Waking up
void StartUp(){
    FadeOut(0);
    AddTimer("", 2, "startintro");
    SetPlayerActive(false);
    PlayMusic("219594__xanco123__dark-ambient-music-2-the-mansion.ogg", true, 1, 0.1f, 1, true);
}

void startintro(string &in asTimer){
    PlayGuiSound("Intro_Dialogue_Final_01.snt", 5);
    AddTimer("", 119, "wakeup2");
}

void wakeup2(string &in asTimer){
    FadeIn(10);
    FadeImageTrailTo(2, 2);
    SetPlayerActive(false);    
    FadePlayerRollTo(50, 220, 220);
    SetPlayerCrouching(true);
    AddTimer("trig1", 11.0f, "reset");
}

void reset(string &in asTimer){
    ChangePlayerStateToNormal();
    SetPlayerActive(true);
    FadePlayerRollTo(0, 33, 33);
    SetPlayerCrouching(false);
    FadeImageTrailTo(0,1);
    CreateParticleSystemAtEntity("dust", "ps_dust_whirl.ps", "ScriptArea_1", true);
    PlayMusic("219594__xanco123__dark-ambient-music-2-the-mansion.ogg", true, 1, 1, 1, true);
}

//Set some messages
void tooBig(string &in asEntity){
    SetMessage("Messages", "toobig1", 2);
}

void rottenApple(string &in asEntity){
    SetMessage("Messages", "rottenapple1", 2);
}

void Locked(string &in asEntity){
    if(GetSwingDoorLocked(asEntity)){
        SetMessage("Messages", "lockeddoor", 2);
    }
}

void rusted(string &in asEntity){
    SetMessage("Messages", "rusted", 2);
}

void prisonLocked(string &in asEntity){
    if(GetSwingDoorLocked(asEntity)){
        SetMessage("Messages", "lockeddoor", 2);
        if(GetLocalVarInt("check1") ==0){
            AddTimer("trig2", 3, "ideas1");
            AddLocalVarInt("check1", 1);
        }
    }
}

void ideas1(string &in asTimer){
    SetMessage("Messages", "maybekey", 2);
}

(This post was last modified: 06-30-2015, 07:21 PM by Rahmerh.)
06-30-2015, 07:04 PM
Find
DnALANGE Offline
Banned

Posts: 1,549
Threads: 73
Joined: Jan 2012
#2
RE: Timer problems

Quote:walk through a certain script area

Can not be timers.
They need to be
PHP Code: (Select All)
AddEntityCollideCallback("Player""YOURARENAMEINEDITOR""Timer1"true1); 
-
(This post was last modified: 06-30-2015, 07:16 PM by DnALANGE.)
06-30-2015, 07:14 PM
Find
Rahmerh Offline
Member

Posts: 66
Threads: 7
Joined: Jun 2015
Reputation: 3
#3
RE: Timer problems

Yeah, that's how you call the first timer.
06-30-2015, 07:14 PM
Find
DnALANGE Offline
Banned

Posts: 1,549
Threads: 73
Joined: Jan 2012
#4
RE: Timer problems

Also remember the : AddTimer("", 0.8f, "givedamage");
Is adding damage every 120 seconds too!
06-30-2015, 07:17 PM
Find
Rahmerh Offline
Member

Posts: 66
Threads: 7
Joined: Jun 2015
Reputation: 3
#5
RE: Timer problems

(06-30-2015, 07:17 PM)DnALANGE Wrote: Also remember the : AddTimer("", 0.8f, "givedamage");
Is adding damage every 120 seconds too!

yeah it gives you damage every 120 seconds, like it's supposed to.. Not sure what you mean Smile
06-30-2015, 07:17 PM
Find
DnALANGE Offline
Banned

Posts: 1,549
Threads: 73
Joined: Jan 2012
#6
RE: Timer problems

You are adding :
PHP Code: (Select All)
AddTimer("Timer3"600"Timer1"); 
SO ye, offcourse the whole thing will start over again Wink
(This post was last modified: 06-30-2015, 07:19 PM by DnALANGE.)
06-30-2015, 07:18 PM
Find
Rahmerh Offline
Member

Posts: 66
Threads: 7
Joined: Jun 2015
Reputation: 3
#7
RE: Timer problems

I know, like I said it should wait 600 seconds. But it instantly calls timer1 again.
06-30-2015, 07:19 PM
Find
DnALANGE Offline
Banned

Posts: 1,549
Threads: 73
Joined: Jan 2012
#8
RE: Timer problems

(06-30-2015, 07:19 PM)Rahmerh Wrote: I know, like I said it should wait 600 seconds. But it instantly calls timer1 again.

Then we nede to see your whole script. Add it with a spoiler tag please.
Then we can see what is going on.
06-30-2015, 07:20 PM
Find
Rahmerh Offline
Member

Posts: 66
Threads: 7
Joined: Jun 2015
Reputation: 3
#9
RE: Timer problems

In the OP Smile
06-30-2015, 07:21 PM
Find
DnALANGE Offline
Banned

Posts: 1,549
Threads: 73
Joined: Jan 2012
#10
RE: Timer problems

OR just ass the timer in :
PHP Code: (Select All)
void food1(string &in asEntity){
     
RemoveTimer("timerb");
     
RemoveTimer("timberd");
         
//startTimerThree();
AddTimer("Timer3"600"Timer1");
 }

 
void startTimerThree(){
        
 } 
(This post was last modified: 06-30-2015, 07:22 PM by DnALANGE.)
06-30-2015, 07:21 PM
Find




Users browsing this thread: 3 Guest(s)