none
Subroutine in a Subroutine? Please Help!!! RRS feed

  • Question

  • Hi, I need help with Subroutnes. i mostly understand how to use them, but can you have multiple subroutines within another.

    (problem in bold)

    GraphicsWindow.Clear()
    GraphicsWindow.Title="SBCraft v0.3"
    GraphicsWindow.BackgroundColor="White"
    W=35
    H=35
    X=-67
    Y=-65
    GraphicsWindow.Width=1024
    GraphicsWindow.Height=768
    GraphicsWindow.Left=0
    GraphicsWindow.Top=0
    GraphicsWindow.CanResize=0
    'startup screen
    logo1="C:\SBCraft\assets\alogo.png"
    logo2="C:\SBCraft\assets\i3.png"
    logo3="C:\SBCraft\assets\curve.png"
    GraphicsWindow.DrawResizedImage(logo1,0,0,1024,768)
    Program.Delay(1500)
    GraphicsWindow.DrawResizedImage(logo2,0,0,1024,768)
    Program.Delay(1500)
    GraphicsWindow.DrawResizedImage(logo3,0,0,1024,768)
    Program.Delay(1500)
    GraphicsWindow.Clear()
    head="C:\SBCraft\assets\head.png"
    doSkinchange()
    Sub doSkinchange
      GraphicsWindow.Clear()
      GraphicsWindow.BrushColor="Black"
      GraphicsWindow.DrawText(0,0,"Would you like to change your skin?")
        GraphicsWindow.DrawText(0,25,"Current:")
      GraphicsWindow.DrawResizedImage(head,0,40,30,30)
      yes=controls.AddButton("Yes, please spare me from this face!",0,75)
      no=controls.AddButton("No, my face is beautiful!",0,110)
      If Controls.LastClickedButton=no Then
        doGame()
      EndIf
      If Controls.LastClickedButton=yes Then
    EndIf 'for now
    endsub

    Sub doGame
    man=Shapes.AddImage(head)
    cube=shapes.AddImage(block)
    blocktype=cube
    Shapes.Remove(cube)
    GraphicsWindow.BrushColor="Black"
    GraphicsWindow.DrawText(805,5,"[Escape]-Exit")
    GraphicsWindow.DrawText(805,25,"[1]-Bricks")
    GraphicsWindow.DrawText(805,45,"[2]-Chrome Wood")
    GraphicsWindow.DrawText(805,65,"[3]-Burlap")
    GraphicsWindow.DrawText(805,85,"[4]-Electric Tile")
    GraphicsWindow.DrawText(805,105,"[5]-Facebook Logo")
    GraphicsWindow.DrawText(805,125,"[6]-Twitter Logo")
    GraphicsWindow.DrawText(805,145,"[7]-Eraser (Use as Normal Block)")
    GraphicsWindow.DrawText(855,718,"Press F1 for credits")
    'always resize with 0.3!!!
    Shapes.Zoom(man,0.1,0.1)
    Shapes.Move(man,x,y)

    GraphicsWindow.KeyDown=keydown
    Sub keydown
      If GraphicsWindow.LastKey="F1" Then
        doCredits()
      endif 
      'move
      If GraphicsWindow.LastKey="D" Then 
        x=x+20
      EndIf
      Shapes.Move(man,x,y)
      If GraphicsWindow.LastKey="W" Then
        y=y-20
      EndIf
      Shapes.Move(man,x,y)
      If GraphicsWindow.LastKey="A" Then
        x=x-20
      EndIf
      Shapes.Move(man,x,y)
      If GraphicsWindow.LastKey="S" Then
        y=y+20
      EndIf
      Shapes.Move(man,x,y)
      'endmove
      
      
      'place
      If GraphicsWindow.LastKey="I" Then
        cube=Shapes.AddImage(block)
    Shapes.Zoom(cube,0.1,0.1)
    Shapes.Move(cube,x+11,y-12)
    Shapes.Remove(man)
    man=Shapes.AddImage(head)
    Shapes.Zoom(man,0.1,0.1)
    Shapes.Move(man,x,y)
      EndIf
    If GraphicsWindow.LastKey="K" Then
      cube=Shapes.AddImage(block)
    Shapes.Zoom(cube,0.1,0.1)
    Shapes.Move(cube,x+11,y+33)
    Shapes.Remove(man)
    man=Shapes.AddImage(head)
    Shapes.Zoom(man,0.1,0.1)
    Shapes.Move(man,x,y)
      EndIf
        If GraphicsWindow.LastKey="J" Then
          cube=Shapes.AddImage(block)
    Shapes.Zoom(cube,0.1,0.1)
    Shapes.Move(cube,x-12,y+11)
    Shapes.Remove(man)
    man=Shapes.AddImage(head)
    Shapes.Zoom(man,0.1,0.1)
    Shapes.Move(man,x,y)
      EndIf
        If GraphicsWindow.LastKey="L" Then
          cube=Shapes.AddImage(block)
    Shapes.Zoom(cube,0.1,0.1)
    Shapes.Move(cube,x+33,y+11)
    Shapes.Remove(man)
    man=Shapes.AddImage(head)
    Shapes.Zoom(man,0.1,0.1)
    Shapes.Move(man,x,y)
    EndIf
    'endplace

    If GraphicsWindow.LastKey="D1" Then
      block="C:\SBCraft\assets\bricks.png"
    GraphicsWindow.DrawResizedImage(block,865,175,60,60)
    EndIf
    If GraphicsWindow.LastKey="D2" Then
      block="C:\SBCraft\assets\chrome.png"
    GraphicsWindow.DrawResizedImage(block,865,175,60,60)
    endif
    If GraphicsWindow.LastKey="D3" Then
      block="C:\SBCraft\assets\fabric.png"
    GraphicsWindow.DrawResizedImage(block,865,175,60,60)
      endif
    If GraphicsWindow.LastKey="D4" Then
      block="C:\SBCraft\assets\electric.png"
    GraphicsWindow.DrawResizedImage(block,865,175,60,60)
    endif
    If GraphicsWindow.LastKey="D5" Then
      block="C:\SBCraft\assets\facebook.png"
    GraphicsWindow.DrawResizedImage(block,865,175,60,60)
    endif
    If GraphicsWindow.LastKey="D6" Then
      block="C:\SBCraft\assets\twitter.png"
    GraphicsWindow.DrawResizedImage(block,865,175,60,60)
    endif
    If GraphicsWindow.LastKey="D7" Then
      block="C:\SBCraft\assets\erase.png"
    GraphicsWindow.DrawResizedImage(block,865,175,60,60)
    endif
    If GraphicsWindow.LastKey="Escape" Then
      Program.End()
    EndIf
    endsub

    Sub doCredits
      TextWindow.Show()
      TextWindow.WriteLine("Credits:")
      TextWindow.WriteLine(">Small Basic Forum. Thanks!")
      TextWindow.WriteLine(">Star-A Interactive")
      TextWindow.WriteLine(">i3 Design")
      TextWindow.WriteLine(">Curve Technology")
       TextWindow.WriteLine("")
      TextWindow.WriteLine("(Just Minimize) Don't X out!")
    endsub
    endsub


      





















    'Thanks!!
    '---------------
    'WhTurner33
    'Timo So
    '8BiT PoNy

    'Report bugs to me on forum

    'For developers only:
    'maybe add a "two player"
    'add change character


    ardiezc_quazhulu was here.

    Wednesday, June 19, 2013 3:08 AM

Answers

  • you cannot declare a subroutine withing a subroutine.. 

    This Example will not work: 
    Sub Sub1
      TextWindow.Writeline("This code will not even compile!")
      innersub()
    
      Sub innersub
        TextWindow.Writeline("Broken!")
      endsub
    endsub

    but you can call a subroutine from within another subroutine. For example:

    Sub Sub2
      Textwindow.Writeline("This executes the outersub()")
      outersub()
    endsub
    
    Sub outersub
      TextWindow.writeline("The outersub() is executing")
    endsub


    Wednesday, June 19, 2013 3:33 AM
  • Hi ardiezc_quazhulu

    Here's a sample that might help explain. TNZ807. Give it a run and see what happens.

    'to make the buttons do something when they are clicked you need to raise an event.  The "Controls.ButtonClicked" event
    '  and attach a Subroutine to it, in this case ButtonClicked.
    
    Controls.ButtonClicked = ButtonClicked 'when button click event occurs it calls the ButtonClicked subroutine
    
    yes = controls.AddButton("Yes, please spare me from this face!",0,75)
    no = controls.AddButton("No, my face is beautiful!",0,110)
    Controls.HideControl(yes)
    '-------------------------Event Driven Subroutines-------------------------------------------------------
    Sub ButtonClicked
      If Controls.LastClickedButton=no Then ' a conditional branch using keyword If
        doGame() 'calls the doGame subroutine. An unconditional branch using Sub
      EndIf
      If Controls.LastClickedButton=yes Then
      EndIf 'for now
    EndSub
    '--------------------------------------Subroutines-----------------------------------------------------------
    Sub doGame
      GraphicsWindow.Hide()
      TextWindow.WriteLine("the program has stopped being executed 1 line after the other ")
      TextWindow.WriteLine("by calling doGame(). It now interprets each line of code from Sub doGame")
      TextWindow.WriteLine("line by line. This is an unconditional branch.")
      TextWindow.WriteLine("It will return to the next line immediately after doGame() once this")
      TextWindow.WriteLine("subroutine has completed, and continue to read the code line by line.")
      TextWindow.WriteLine("")
      TextWindow.WriteLine("Before this Subroutine finishes lets branch to another part of the program.")
      TextWindow.Pause()
      callASubFromWithinAnotherSub()
      TextWindow.Pause()
      TextWindow.WriteLine("back to here")
      TextWindow.Pause()
      TextWindow.Hide()
      GraphicsWindow.Show()
      'now this sub has run the program will return to the next line after where the sub was called.
    EndSub
    
    Sub callASubFromWithinAnotherSub
      TextWindow.WriteLine("")
      TextWindow.WriteLine("This output is from the subroutine callASubFromWithinAnotherSub()")
      TextWindow.WriteLine("Now this sub is finished the program will return to next line")
      TextWindow.WriteLine("immediately after where it was called, callASubFromWithinAnotherSub()")
    EndSub

    Wednesday, June 19, 2013 6:40 AM
    Moderator

All replies

  • you cannot declare a subroutine withing a subroutine.. 

    This Example will not work: 
    Sub Sub1
      TextWindow.Writeline("This code will not even compile!")
      innersub()
    
      Sub innersub
        TextWindow.Writeline("Broken!")
      endsub
    endsub

    but you can call a subroutine from within another subroutine. For example:

    Sub Sub2
      Textwindow.Writeline("This executes the outersub()")
      outersub()
    endsub
    
    Sub outersub
      TextWindow.writeline("The outersub() is executing")
    endsub


    Wednesday, June 19, 2013 3:33 AM
  • Hi ardiezc_quazhulu

    Here's a sample that might help explain. TNZ807. Give it a run and see what happens.

    'to make the buttons do something when they are clicked you need to raise an event.  The "Controls.ButtonClicked" event
    '  and attach a Subroutine to it, in this case ButtonClicked.
    
    Controls.ButtonClicked = ButtonClicked 'when button click event occurs it calls the ButtonClicked subroutine
    
    yes = controls.AddButton("Yes, please spare me from this face!",0,75)
    no = controls.AddButton("No, my face is beautiful!",0,110)
    Controls.HideControl(yes)
    '-------------------------Event Driven Subroutines-------------------------------------------------------
    Sub ButtonClicked
      If Controls.LastClickedButton=no Then ' a conditional branch using keyword If
        doGame() 'calls the doGame subroutine. An unconditional branch using Sub
      EndIf
      If Controls.LastClickedButton=yes Then
      EndIf 'for now
    EndSub
    '--------------------------------------Subroutines-----------------------------------------------------------
    Sub doGame
      GraphicsWindow.Hide()
      TextWindow.WriteLine("the program has stopped being executed 1 line after the other ")
      TextWindow.WriteLine("by calling doGame(). It now interprets each line of code from Sub doGame")
      TextWindow.WriteLine("line by line. This is an unconditional branch.")
      TextWindow.WriteLine("It will return to the next line immediately after doGame() once this")
      TextWindow.WriteLine("subroutine has completed, and continue to read the code line by line.")
      TextWindow.WriteLine("")
      TextWindow.WriteLine("Before this Subroutine finishes lets branch to another part of the program.")
      TextWindow.Pause()
      callASubFromWithinAnotherSub()
      TextWindow.Pause()
      TextWindow.WriteLine("back to here")
      TextWindow.Pause()
      TextWindow.Hide()
      GraphicsWindow.Show()
      'now this sub has run the program will return to the next line after where the sub was called.
    EndSub
    
    Sub callASubFromWithinAnotherSub
      TextWindow.WriteLine("")
      TextWindow.WriteLine("This output is from the subroutine callASubFromWithinAnotherSub()")
      TextWindow.WriteLine("Now this sub is finished the program will return to next line")
      TextWindow.WriteLine("immediately after where it was called, callASubFromWithinAnotherSub()")
    EndSub

    Wednesday, June 19, 2013 6:40 AM
    Moderator