locked
Subroutine Arrays? RRS feed

  • Question

  • Hey,

    This is my first post so yah..

     

    So i'm creating a Maze game for school and at the moment im just trying to make it so that i can draw piece by piece of the maze in a way that can be easily expanded. I'm using dual arrays to represent each square of the maze. I'm using the strings "NW" etc. NW being North Wall. EW East wall etc etc. So that will represent whether or not the player can walk in that direction. If NW is "True" then there is a wall there. If NW is "False" then there is no wall, and the player can move that way.

    So i wanted to use a Subrouting-Array kind of thing. But it doesnt work apparently.

    Here is the code ive got so far:

    square[1]["NW"] = "True"

    square[1]["EW"] = "False"

    square[1]["SW"] = "False"

    square[1]["WW"] = "True"

     

    square[2]["NW"] = "True"

    square[2]["EW"] = "True"

    square[2]["SW"] = "False"

    square[2]["WW"] = "False"

     

    square[3]["NW"] = "True"

    square[3]["EW"] = "False"

    square[3]["SW"] = "True"

    square[3]["WW"] = "True"

     

    value = 1

     

    For x = 1 to 3

    If square[value]["NW"] = "True" Then

    NW[value]()

    EndIf 

     

    If square[value]["WW"] = "True" Then

    WW[value]()

    EndIf 

     

    If square[value]["EW"] = "True" Then

    EW[value]()

    EndIf 

     

     

    If square[value]["SW"] = "True" Then

    SW[value]()

    value = value + 1

    EndIf 

     

    endfor

     

    GraphicsWindow.Show()

    GraphicsWindow.CanResize = 0

    GraphicsWindow.Width = 120

    GraphicsWindow.Height = 120 

     

     

    Sub NW[1]

    GraphicsWindow.DrawLine(11,11,41,11)

    endsub

    Sub EW[1]

    GraphicsWindow.DrawLine(41,11,41,41)

    endsub

    Sub SW[1]

    GraphicsWindow.DrawLine(11,41,41,41)

    endsub

    Sub WW[1]

    GraphicsWindow.DrawLine(11,11,11,41)

    endsub

     

    Sub NW[2]

      GraphicsWindow.DrawLine(41,11,71,11)

    endsub

    Sub EW[2]

      GraphicsWindow.DrawLine(71,11,71,41)

    endsub

    Sub SW[2]

      GraphicsWindow.DrawLine(41,41,71,41)

    endsub

    Sub WW[2]

      GraphicsWindow.DrawLine(41,11,41,41)

    endsub

     

    Sub NW[3]

      GraphicsWindow.DrawLine(71,11,101,11)

    EndSub

    Sub EW[3]

      GraphicsWindow.DrawLine(101,11,101,41)

    EndSub

    Sub SW[3]

      GraphicsWindow.DrawLine(71,41,101,41)

    EndSub

    Sub WW[3]

      GraphicsWindow.DrawLine(71,11,71,41)

    EndSub

    I know you forum guys don't wanna do every kids assignment lol. But, I would love some direction. A way to fix my Sub-Array thingo.

     

    Cheers!

    Thursday, December 1, 2011 11:19 AM

Answers

  • Hi,

    You cannot call subroutines with arguments in SmallBasic, so NW[value]() isn't valid.

    However, all variables are global (can be used and changed anywhere in your code - even in subroutines.  So you can call a subroutine and the variable value will be available to be checked inside the subroutine.

    Sample to show the idea below.

    value = 5
    testSub()
    
    Sub testSub
      If (value = 1) Then
        TextWindow.WriteLine("The number is one")
      ElseIf (value = 2) Then
        TextWindow.WriteLine("The number is two")
      ElseIf (value = 3) Then
        TextWindow.WriteLine("The number is three")
      ElseIf (value = 4) Then
        TextWindow.WriteLine("The number is four")
      ElseIf (value = 5) Then
        TextWindow.WriteLine("The number is five")
      ElseIf (value = 6) Then
        TextWindow.WriteLine("The number is 6")
      EndIf 
    EndSub


    Friday, December 2, 2011 6:57 PM

All replies

  • Hi,

    You cannot call subroutines with arguments in SmallBasic, so NW[value]() isn't valid.

    However, all variables are global (can be used and changed anywhere in your code - even in subroutines.  So you can call a subroutine and the variable value will be available to be checked inside the subroutine.

    Sample to show the idea below.

    value = 5
    testSub()
    
    Sub testSub
      If (value = 1) Then
        TextWindow.WriteLine("The number is one")
      ElseIf (value = 2) Then
        TextWindow.WriteLine("The number is two")
      ElseIf (value = 3) Then
        TextWindow.WriteLine("The number is three")
      ElseIf (value = 4) Then
        TextWindow.WriteLine("The number is four")
      ElseIf (value = 5) Then
        TextWindow.WriteLine("The number is five")
      ElseIf (value = 6) Then
        TextWindow.WriteLine("The number is 6")
      EndIf 
    EndSub


    Friday, December 2, 2011 6:57 PM
  • Just to make sure you don't mix up variables as parameters for subroutines, you can use subroutineName_variableName, or the other way around.
    ~~AirWaves!!~~
    Saturday, December 3, 2011 2:43 AM
  • @ Airwaves

    I'm not following you.  This is the code I tried for that.  I also tried the same thing with textwindow.read() instead of readnumber.

    Thanks

     

    TextWindow.Show()

    TextWindow.WriteLine("Enter '1', '2', or '3':")
    myIndex = TextWindow.ReadNumber()

    sayWhat_myIndex()


    Sub sayWhat_1
      TextWindow.WriteLine("Hi.")
    EndSub

    Sub sayWhat_2
      TextWindow.WriteLine("Howdy.")
    EndSub

    Sub sayWhat_3
      TextWindow.WriteLine("What.")
    EndSub


    Allen
    Tuesday, December 6, 2011 3:46 PM
  • @Allen

    AirWaves meant variables could be used like this:

    TextWindow.Show()
    While "True"
      TextWindow.WriteLine("Enter '1', '2', or '3':")
      Function_Parameter = TextWindow.ReadNumber()
      Function()
      TextWindow.WriteLine("Function returned '"+Function_Return+"' with parameter '"+Function_Parameter+"'")
    EndWhile

    Sub Function
      Function_Return = "FAILED"
      If Function_Parameter = 1 Then
        Function_Return = "One"
      ElseIf Function_Parameter = 2 Then
        Function_Return = "Two"  
      ElseIf Function_Parameter = 3  Then
        Function_Return = "Three"
      EndIf
    EndSub

    • Edited by Oskariok Tuesday, December 6, 2011 7:28 PM
    Tuesday, December 6, 2011 7:26 PM