Jump to content

[scripting]Do action every x seconds


lorenzoulm

Recommended Posts

The easiest way to run a script for a predetermined amount of time is to call an effect that lasts for that duration. If creating a timer didn't work, make sure that the variable you are setting with GetSecondsPassed is a float and not a short.

 

scn ninetysecondsES

float timer

begin scripteffectstart

    set timer to 0

end ; scripteffectstart

begin scripteffectupdate

    if(timer > 1)
        set timer to 0
        ; do something extraordinary, as long as it doesn't take longer than a second to do
    else
        set timer to getsecondspassed
    endif
    
end ; scripteffectupdate

begin scripteffectfinish

    ; do whatever at the end of the effect
    
end ; scripteffectfinish

 

 

If you simply MUST use a different script type, you could add a token to the player's inventory with an object script.

 

scn ninetysecondsOS

float timer
short count

begin onadd

    set timer to 0
    set count to 0
    
end ; onadd

begin gamemode

    if(count == 90)
        removeme
    else
        if(timer > 1)
            set timer to 0
            set count to count + 1
            ; do something extraordinary, as long as it doesn't take longer than a second to do
        else
            set timer to getsecondspassed
        endif
    
end ; gamemode
Link to comment
Share on other sites

You need some block to start your timer. In my example, I am using an OnLoad block which runs as soon as the object the script is on loads in the cell the first time. You can use other initializing blocks like OnActivate for containers, or even a script variable.

SCN MyTimer

Float Timer
Short Retry


Begin OnLoad
  Set Timer to 1
  Set Retry to 90
End

Begin GameMode
  If Retry
    If (Timer<=0)  
       ;Do Something
       Set Timer to 1
       Set Retry to Retry - 1
    Else
        Set Timer to Timer - GetSecondsPassed
    Endif
  Endif
End

This example will Do Something once a second until the number of Retry reaches zero, or 90 times.

The game Mode block won't run until Retry is positive and will stop when it's 0.

Also, in this case, a full second will elapse before the first time Something is done. If you want the Something to occur right away, just remove the Set Timer to 1 command in the OnLoad block.

 

EDIT: I see I was Guzumped by luth while I was writing this post...)

Edited by pkleiss
Link to comment
Share on other sites

scn MyEventTimerScript

float fEventTimer
float fsecondTimer
short iStartAction

;Object script (or fast quest script). iStartAction can be set to 1 by another script (trigger, quest stage, dialog)

BEGIN GameMode

    if fEventTimer >= 90 || iStartAction == 0
        return
    endif

    if fsecondTimer > 0
        set fsecondTimer to fsecondTimer - GetSecondsPassed
    else
        if iStartAction == 1
            ; Do action here
            set fsecondTimer to 1
            set fEventTimer to fEventTimer + GetSecondsPassed
        endif
    endif
    
END

Lol - I was double ninja'd :P

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...