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

How to use a function in a function (SB4)

Root / Programming Questions / [.]

iwanthamCreated:
ACLS

DEF APRS 'CHECK IF A PRESSED
IF BUTTON(1,#B_RRIGHT)==1 THEN APRS=1
END

LOOP 'INITIAL GAME LOOP FOR TESTING
STARTG
VSYNC
ENDLOOP

DEF STARTG 'COMMAND TO DISPLAY INITIAL TEXT
CLS
LOCATE 10,3
SPSET 0,2
SPOFS 0,192,112
PRINT "WELCOME, PRESS A TO BEGIN."
IF APRS==1 THEN PRINT "CHERRY" 'JUST TESTING WHETHER IT RECOGNIZES THE INPUT
END
When I call APRS in the function STARTG it doesn't give me "Cherry" string. I must not be calling the function correctly. I made APRS to save me some time so I wouldn't have to type out the BUTTON stuff over and over again. Maybe that's not possible?

Maybe you tried to use DEF like Function in VBA...? To return a value, you need to write RETURN value instead of FunctionName=value. Also, you have to add () to the function name, if it returns one value.
ACLS

DEF APRS() 'CHECK IF A PRESSED
IF BUTTON(1,#B_RRIGHT)==1 THEN RETURN 1
RETURN 0
END

LOOP 'INITIAL GAME LOOP FOR TESTING
STARTG
VSYNC
ENDLOOP

DEF STARTG 'COMMAND TO DISPLAY INITIAL TEXT
CLS
LOCATE 10,3
SPSET 0,2
SPOFS 0,192,112
PRINT "WELCOME, PRESS A TO BEGIN."
IF APRS()==1 THEN PRINT "CHERRY" 'JUST TESTING WHETHER IT RECOGNIZES THE INPUT
END

Maybe you tried to use DEF like Function in VBA...? To return a value, you need to write RETURN value instead of FunctionName=value. Also, you have to add () to the function name, if it returns one value.
ACLS

DEF APRS() 'CHECK IF A PRESSED
IF BUTTON(1,#B_RRIGHT)==1 THEN RETURN 1
RETURN 0
END

LOOP 'INITIAL GAME LOOP FOR TESTING
STARTG
VSYNC
ENDLOOP

DEF STARTG 'COMMAND TO DISPLAY INITIAL TEXT
CLS
LOCATE 10,3
SPSET 0,2
SPOFS 0,192,112
PRINT "WELCOME, PRESS A TO BEGIN."
IF APRS()==1 THEN PRINT "CHERRY" 'JUST TESTING WHETHER IT RECOGNIZES THE INPUT
END
Thanks so much! Actually I was just experimenting, I have some history with C++, Java, and some Python (it's been years) but I'm still learning syntax for this. This should give me the framework I need to keep working on this :)

Ran into another slightly related issue in the same program, I'll just ask here rather than make a new thread. How do I get the functions to wait for player input? They go by too fast to see and when the button is released it goes back to the first function.
LOOP
STARTG
VSYNC
ENDLOOP


DEF STARTG 'START SCREEN
TXTF 'TEXT FORMAT ESTABLISHED EARLIER IN THE CODE
PRINT "WELCOME, PRESS A TO BEGIN."
IF APRS()==1 THEN LVL0
END

DEF LVL0
TXTF
PRINT "CHOOSE STORYLINE"
PRINT "A BUTTON: CHERRY"
PRINT "B BUTTON: ORANGE"
PRINT Y BUTTON :BANANA"
IF APRS()==1 THEN CH0 'CH0 IS THE FIRST SCREEN FOR THE CHERRY STORYLINE
END
There's more after this but when you press A it goes so fast you can't see it, then when you release a it just goes back to STARTG. Should I have used DIALOG? I didn't know about it when I started but it looks complicated

You can use another variable to keep the screen like this:
PHASE=0 '0=STARTG,1=LVL0,2=CH0
LOOP
 CASE PHASE
  WHEN 0:
   STARTG
  WHEN 1:
   LV0
  WHEN 2:
   CH0
 ENDCASE
 VSYNC
ENDLOOP


DEF STARTG 'START SCREEN
TXTF 'TEXT FORMAT ESTABLISHED EARLIER IN THE CODE
PRINT "WELCOME, PRESS A TO BEGIN."
IF APRS()==1 THEN PHASE=1 'LVL0
END

DEF LVL0
TXTF
PRINT "CHOOSE STORYLINE"
PRINT "A BUTTON: CHERRY"
PRINT "B BUTTON: ORANGE"
PRINT "Y BUTTON :BANANA"
IF APRS()==1 THEN PHASE=2 'CH0 IS THE FIRST SCREEN FOR THE CHERRY STORYLINE
END
another not recommended wayI think GOTO and GOSUB are useful, but many people say I should not use them...
@STARTG
LOOP 'START SCREEN
 VSYNC
 TXTF 'TEXT FORMAT ESTABLISHED EARLIER IN THE CODE
 PRINT "WELCOME, PRESS A TO BEGIN."
 IF APRS()==1 THEN @LVL0
ENDLOOP

@LVL0
LOOP
 VSYNC
 TXTF
 PRINT "CHOOSE STORYLINE"
 PRINT "A BUTTON: CHERRY"
 PRINT "B BUTTON: ORANGE"
 PRINT "Y BUTTON :BANANA"
 IF APRS()==1 THEN @CH0 'CH0 IS THE FIRST SCREEN FOR THE CHERRY STORYLINE
ENDLOOP

I think GOTO and GOSUB are useful, but many people say I should not use them...
@STARTG
LOOP 'START SCREEN
 VSYNC
 TXTF 'TEXT FORMAT ESTABLISHED EARLIER IN THE CODE
 PRINT "WELCOME, PRESS A TO BEGIN."
 IF APRS()==1 THEN @LVL0
ENDLOOP

@LVL0
LOOP
 VSYNC
 TXTF
 PRINT "CHOOSE STORYLINE"
 PRINT "A BUTTON: CHERRY"
 PRINT "B BUTTON: ORANGE"
 PRINT "Y BUTTON :BANANA"
 IF APRS()==1 THEN @CH0 'CH0 IS THE FIRST SCREEN FOR THE CHERRY STORYLINE
ENDLOOP
Actually this suggestion gave me an idea that eventually worked. I was hesitant to use a variable to keep track because it would be a lot of work depending on how many screens (functions) I would eventually make, and with branching paths in the story, numbering the variables would become difficult to track and assign. Here's an example what I came up with, for anyone in the future who wants to make a text based game and runs into issues like I did.
ACLS

DEF TXTF 'FUNCTION TO ESTABLISH TEXT FORMATTING, RIGHT 
'NOW IT'S ONLY CLS BUT WILL BE USED FOR MORE LATER ON IN DEVELOPMENT 

DEF CHF 'FUNCTION ESTABLISH TEXT FORMATTING FOR CHERRY STORYLINE
SPSET 1,1 'CHERRY SPRITE
SPOFS 0,370,200 'PUTS SPRITE IN BOTTOM RIGHT SO IT DOESNT BLOCK TEXT
SPSCALE 0,2,2 'CHERRY IS TWICE AS BIG AS NORMAL
END

DEF APRS  'CHECKS IF A IS RELEASED
IF BUTTON(1,#B_RRIGHT,3)==1 THEN RETURN 1 'THE 3 CHECKS IF THE BUTTON IS 
'RELEASED, THAT WAY DIALOGUE CAN'T BE CYCLED THROUGH 
'BY MERELY PRESSING THE BUTTON FOR TOO LONG/HOLDING IT DOWN
RETURN 0
END

DEF ST 'FUNCTION TO PUT IN THE BEGINNING OF EACH SCENARIO FUNCTION 
CLS
WAIT 10 'WAIT 10 FRAMES, EVEN WITH BUTTON RELEASE BEING CHECKED FOR, IT IS STILL 
'POSSIBLE FOR THE GAME TO DETECT A BUTTON RELEASE
'AND SKIP TWO OR MORE FUNCTIONS INADVERTENTLY, THIS MAKES 
'IT SO THE BUTTON CAN ONLY TRIGGER ONE FUNCTION PER RELEASE
END

LOOP 'GAME LOOP
STARTG 'CALLS FIRST LEVEL SEE BELOW
VSYNC
ENDLOOP

DEF STARTG
LOOP
TXTF 
PRINT "WELCOME, PRESS A TO BEGIN"
IF APRS()==1 THEN LVL0 'IF A BUTTON IS RELEASED, GO TO NEXT SCENARIO
VSYNC 'TO GET RID OF FLICKER
ENDLOOP
END

DEF LVL0
ST 'FUNCTION TO CLEAR SCREEN AND WAIT FOR 10 FRAMES, STARTS BEFORE LOOP
LOOP
CHF 
PRINT "SAMPLE TEXT" 
'MORE TEXT, MORE BUTTON CHECKS, MORE CALLING FUNCTIONS 
VSYNC
ENDLOOP
END
Thanks for the help Na_trium