Author Topic: ERROR debug  (Read 40435 times)

0 Members and 4 Guests are viewing this topic.

Offline robbo007

Re: ERROR debug
« Reply #15 on: July 08, 2025, 12:13:18 PM »
So this is my Timer Manager script in the end. It seemed to cause all types of grief when I tried to have the time state move between rooms and continue where the last room took off.  So have it restarting when moving between rooms giving a single time limit per room.

Code: [Select]
(script# 306)
(include sci.sh)
(include game.sh)
(use Main)
(use Timer)
(use Obj)
(use Controls)

(public
    timerManager 0
)

(local
    gTimeLimit = 300  ; 300 seconds / 5 mins
    gTimerActive
    gDeathScriptPending
    gQueuedScriptRan
)

(instance timerManager of Obj
    (properties)
   
    (method (init)
        ; Only create new timer if none exists or current one expired
        (if (or (not (IsObject gDhcTimer)) (<= (gDhcTimer seconds?) 0))
            (self dispose:)  ; Clean up any existing timer first
            (= gDhcTimer (Timer new:))
            (gDhcTimer setReal: self gTimeLimit)
        else
            ; Just refresh the existing timer
            (gDhcTimer setReal: self (gDhcTimer seconds?))
        )
        (= gTimerActive TRUE)
    )
   
    (method (cue)
        (= gTimerActive FALSE)
        ; Load room scripts only when needed
        (cond
            ((== gRoomNumber 7)
                (if (not (ScriptID 7)) (ScriptID 7)) ; Ensure script is loaded
                (if (IsObject (ScriptID 7 1))
                    (gRoom setScript: (ScriptID 7 1))
                else
                    (self defaultExpiredHandler:)
                )
            )
            ((== gRoomNumber 8)
                (if (not (ScriptID 8)) (ScriptID 8)) ; Ensure script is loaded
                (if (IsObject (ScriptID 8 1))
                    (gRoom setScript: (ScriptID 8 1))
                else
                    (self defaultExpiredHandler:)
                )
            )
            (else
                (self defaultExpiredHandler:)
            )
        )
    )
   
    (method (doit)
        ; Optimized script queue handling
        (if (and (IsObject gDeathScriptPending) (not gQueuedScriptRan))
            (gRoom setScript: gDeathScriptPending)
            (= gQueuedScriptRan TRUE)
            (= gDeathScriptPending 0)
        )
        (super doit:)
    )
   
    (method (defaultExpiredHandler)
        (Print "Timer expired in unexpected room!")
    )
   
    (method (showTimeLeft &tmp secondsLeft)
        (if (not gTimerActive)
            (Print "No active timer.")
            (return)
        )
        (if (IsObject gDhcTimer)
            (= secondsLeft (gDhcTimer seconds?))
            (FormatPrint "Time left: %d:%02d"
                (/ secondsLeft 60)
                (mod secondsLeft 60)
            )
        else
            (Print "Timer error!")
        )
    )
   
    (method (dispose)
        ; More thorough cleanup
        (if (IsObject gDhcTimer)
            (gDhcTimer client: 0)
            (gDhcTimer dispose:)
            (= gDhcTimer 0)
        )
        (= gTimerActive FALSE)
        (= gDeathScriptPending 0)
        (super dispose:)
    )
)

Offline doomlazer

Re: ERROR debug
« Reply #16 on: July 08, 2025, 04:02:55 PM »
Scripts that need to persist between rooms are typically regions so that they don't get unloaded.

Offline lskovlun

Re: ERROR debug
« Reply #17 on: July 08, 2025, 04:53:18 PM »
This is an antipattern:
Code: [Select]
               (if (not (ScriptID 8)) (ScriptID 8)) ; Ensure script is loaded
                (if (IsObject (ScriptID 8 1))
In the first line, the if-expression will never be true, because ScriptID always loads the script.
In the second, it will never be false, for the same reason.

Unless you miscompile that script somehow. Sierra just did:
Code: [Select]
(ScriptID 8)

Offline robbo007

Re: ERROR debug
« Reply #18 on: July 09, 2025, 07:10:21 AM »
This is an antipattern:
Code: [Select]
               (if (not (ScriptID 8)) (ScriptID 8)) ; Ensure script is loaded
                (if (IsObject (ScriptID 8 1))
In the first line, the if-expression will never be true, because ScriptID always loads the script.
In the second, it will never be false, for the same reason.

Unless you miscompile that script somehow. Sierra just did:
Code: [Select]
(ScriptID 8)

ok thanks. I've updated it now :)


SMF 2.0.19 | SMF © 2021, Simple Machines
Simple Audio Video Embedder

Page created in 0.029 seconds with 22 queries.