LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Can a function detect whether it is called in Direct Mode?

Root / Programming Questions / [.]

MZ952Created:
Some instructions can't be called in Direct Mode, like DISPLAY. It would be neat if the function knew whether it would be appropriate to call such functions. A solution I'm seeing now is to have a global var in the program that can only be changed if that program is running. Like,
DIM T,DT,RT
WHILE 1
 IF SomeCondition THEN F ENDIF
 '...
 INC T
WEND
DEF F
 IF !RT THEN DT=T RT=1 RETURN ENDIF
 DIM D=-1
 IF RT && DT!=T THEN DISPLAY OUT D ENDIF
 'if D is -1, we're in Direct Mode.
 '...
 RT=0
END
But this would require that F fails on the first call. Any ideas?

BACKTRACE seems to always be 0: 0 from Direct mode (even if there are linebreaks in the input)
COMMON DEF MOE_DIRECT()
  BACKTRACE
  VAR S$
  FOR L=0TO 7
    INC S$,CHR$(CHKCHR(L,CSRY-1))
  NEXT
  RETURN S$=="0:     0"
END
(demonstration only) If you're okay with dirtying the text console and failing if called from line 0 of slot 0 Simple programs and those that normally hide text layer can use
REPEAT?" "*50LOCATE 0,CSRY-1
UNTIL CHKCHR(1,CSRY)!=58
LOCATE 0,CSRY+1
to clean up; TUI-based programs should call the screen redraw routine if available.

Hmm interesting. For my specific purposes I do care about dirtying the text screen, but I suppose there's really no other way, other than using dedicated globals. I guess then that Direct Mode uses line 0 of slot 0? Huh.

I think it's just that the call stack trace is empty in direct mode or when a program isn't suspended so it just outputs nonsense. BACKTRACE is supposed to be used when a program is STOPped so in that event this might not work.

Direct mode basically acts like a debugger for a specific slot, Which is slot 0 if the program ended normally (reaching the end of a slot, or END) (even if it wasn't running slot 0 before), or if there was an error (or STOP, or you press start), it will be whatever slot it was last running (unless the last slot was the smiletool (slot 4)) A few more ideas: When a program is running normally, ERRNUM=0, ERRLINE=-1, ERRPRG=-1 even if you stop the program, set ERRNUM (by causing an error in direct mode), then use CONT, they will be set back to the default values So if these are set to any other values, you know that your code is being called from direct mode (I'm pretty sure) But that only happens after a program ended with an error, not by STOP or END or pressing start. So there are basically 5 possible states you can be in:
| state                    | BACKTRACE |  ERRNUM |   ERRLINE |
|--------------------------+-----------+---------+-----------|
| direct mode, after END   | 0:  0     |       0 |        -1 |
| direct mode, after STOP  | line      |       0 |        -1 |
| direct mode, after error | line      | nonzero |      line |
| program running          | line      |       0 |        -1 |
| error in direct mode     | unchanged | nonzero | unchanged |
You can't tell apart states 2 and 4 as far as I know,