none
How to make multiple subroutines run at the same time? RRS feed

  • Question

  • Well, as title says, is it possible to make 2 or more sub's run at once? If so, how?

    thanks :)

    Thursday, September 8, 2011 8:56 PM

Answers

  • Actually doing things literally at the same time on a dual core PC is complicated and can cause lots of issues - not recommended for SmallBasic, although it can be done as shown above.

    However, computers are very fast and doing things sequentially can appear as if they are done at the same time - a ms or two delay isn't going to be noticeable by anyone.  So do things sequentially - all of the SmallBasic games so far have done it that way.

    In your case I would have one subroutine, perhaps called 'moveCups' and inside it do all of the movements as required according to flags set as to which cups to move from where to where, possibly using the Shapes.Animate method.

    Other comments:

    1] Only download the shape once using ImageList

    2] Don't modify loop counters e.g. i inside the loop unless there is a very good reason.

    Example how I might do it below:

    h = 480 'height
    w = 780 'width

     GraphicsWindow.Show()
    GraphicsWindow.Height = h
    GraphicsWindow.Width = w

    'Only download the image once
    cupImage = ImageList.LoadImage("http://img708.imageshack.us/img708/4155/cupsombra.png")
    cup1 = Shapes.AddImage(cupImage)
    cup2 = Shapes.AddImage(cupImage)
    cup3 = Shapes.AddImage(cupImage)

    xPos1 = w/2 -150 'initial x position for cup1
    xPos2 = w/2 'initial x position for cup2
    xPos3 = w/2 + 150 'initial x position for cup3
    yPos1 = h/2-50 'y position for all cups
    yPos2 = yPos1
    yPos3 = yPos1

    Shapes.Move(cup1,xPos1,yPos1) 'inital position
    Shapes.Move(cup2,xPos2,yPos2)
    Shapes.Move(cup3,xPos3,yPos3)

    Program.Delay(1000)

    'Test swapping the cups
    xPos1 = w/2
    xPos2 = w/2 + 150
    xPos3 = w/2 -150
    MoveCups()

    Sub MoveCups ' Animate to new positions in 1 sec
      Shapes.Animate(cup1,xPos1,yPos1,1000)
      Shapes.Animate(cup2,xPos2,yPos2,1000)
      Shapes.Animate(cup3,xPos3,yPos3,1000)
      Program.Delay(1000)
    EndSub

    By the way - this looks like the start of a cool game.

    EDIT

    If you want a more complex movement you could do something like this, Import ZMB697-1. 

    The key is to work out how to define the movement in a general way so a single subroutine can handle it.  In this case I just set a value (1, 2 or 3) for the new placement of each of the cups, with a negative sign meaning move down and a positive sign move up.  Then a bit of arithmetic to calculate intermediate position of the cups as you did for the individual cases.

    • Edited by litdevModerator Friday, September 9, 2011 6:27 PM EDIT added
    • Marked as answer by Luquitas94 Sunday, September 11, 2011 1:24 PM
    Friday, September 9, 2011 5:25 PM
    Moderator

All replies

  • SmallBasic isn't designed for this, but it is possible - see this thread.

    You will need to be very careful though and I probably wouldn't recommend it - note the post by Vijaye - the author of SmallBasic..

    Thursday, September 8, 2011 9:20 PM
    Moderator
  • SmallBasic isn't designed for this, but it is possible - see this thread.

    You will need to be very careful though and I probably wouldn't recommend it - note the post by Vijaye - the author of SmallBasic..

    So i read everything and it sounds like chinese to me XD but i just tried adding the timer script you gave on the first post and i got this result:

     

    ZMB697

     

    The thing is that i want both movements to be at once.. You kno what I mean?

    When the first cup leaves the first spot, the second one has also left the 2nd spot simultaneously.

    Could I use the spot1Taken = "true" to make it so when its false the second one has to move? Could that be a solution?

    Thanks :)

     

    Friday, September 9, 2011 10:35 AM
  • Actually doing things literally at the same time on a dual core PC is complicated and can cause lots of issues - not recommended for SmallBasic, although it can be done as shown above.

    However, computers are very fast and doing things sequentially can appear as if they are done at the same time - a ms or two delay isn't going to be noticeable by anyone.  So do things sequentially - all of the SmallBasic games so far have done it that way.

    In your case I would have one subroutine, perhaps called 'moveCups' and inside it do all of the movements as required according to flags set as to which cups to move from where to where, possibly using the Shapes.Animate method.

    Other comments:

    1] Only download the shape once using ImageList

    2] Don't modify loop counters e.g. i inside the loop unless there is a very good reason.

    Example how I might do it below:

    h = 480 'height
    w = 780 'width

     GraphicsWindow.Show()
    GraphicsWindow.Height = h
    GraphicsWindow.Width = w

    'Only download the image once
    cupImage = ImageList.LoadImage("http://img708.imageshack.us/img708/4155/cupsombra.png")
    cup1 = Shapes.AddImage(cupImage)
    cup2 = Shapes.AddImage(cupImage)
    cup3 = Shapes.AddImage(cupImage)

    xPos1 = w/2 -150 'initial x position for cup1
    xPos2 = w/2 'initial x position for cup2
    xPos3 = w/2 + 150 'initial x position for cup3
    yPos1 = h/2-50 'y position for all cups
    yPos2 = yPos1
    yPos3 = yPos1

    Shapes.Move(cup1,xPos1,yPos1) 'inital position
    Shapes.Move(cup2,xPos2,yPos2)
    Shapes.Move(cup3,xPos3,yPos3)

    Program.Delay(1000)

    'Test swapping the cups
    xPos1 = w/2
    xPos2 = w/2 + 150
    xPos3 = w/2 -150
    MoveCups()

    Sub MoveCups ' Animate to new positions in 1 sec
      Shapes.Animate(cup1,xPos1,yPos1,1000)
      Shapes.Animate(cup2,xPos2,yPos2,1000)
      Shapes.Animate(cup3,xPos3,yPos3,1000)
      Program.Delay(1000)
    EndSub

    By the way - this looks like the start of a cool game.

    EDIT

    If you want a more complex movement you could do something like this, Import ZMB697-1. 

    The key is to work out how to define the movement in a general way so a single subroutine can handle it.  In this case I just set a value (1, 2 or 3) for the new placement of each of the cups, with a negative sign meaning move down and a positive sign move up.  Then a bit of arithmetic to calculate intermediate position of the cups as you did for the individual cases.

    • Edited by litdevModerator Friday, September 9, 2011 6:27 PM EDIT added
    • Marked as answer by Luquitas94 Sunday, September 11, 2011 1:24 PM
    Friday, September 9, 2011 5:25 PM
    Moderator
  • aiight thanks a lot for your help :) It's nice to have people lik u guys helping around cuz it's hard to learn by yourself XD

    I modified it a bit so that the movement cannot be predictable heres the new code:

    FDZ660

    Thank You

     

    Sunday, September 11, 2011 1:32 PM