locked
GraphicsWindow crashes when clicking on a Textbox RRS feed

  • Question

  • I think I know why this happens.

    It's because the Sub I created.

    GraphicsWindow.MouseDown=down

    And when I click on the Textbox my GraphicsWindow crashes.

    I've tried to make a custom Sub, so "down" cannot be used meanwhile the custom Sub is active.

    But I still can't solve it.

    Edit: I haven't much experience with custom Subs.

    • Edited by box57 Tuesday, August 14, 2018 12:49 PM
    Wednesday, August 1, 2018 10:56 PM

Answers

  • Hi,

    I published the changes I made to XHM306

    Please import this, save it to the same place as your version (so the download settigs file file can be found) and uncomment the auto-commented File commands (lines 38 only if using existing settings).

    This then runs not crashing as you describe.


    However this was to illustrate the reasons for the crash, not a final solution.

    To proceed with your program you will need to understand why:

    1] GraphicsWindow and most other actions should be performed on the main UI (User Interface) thread.  Event subroutines are performed asynchronously, not on the UI thread.  The UI is not updated until after the event has completed and may cause bad/unpredictable/crash behaviour if they occur concurrently. See this Wiki article and  this Wiki article.

    2] Only set flags (simple variables) in event subroutines, especially ones that are called often.  Best practice do this for ALL event subroutnes.

    3] Use a 'game loop' to control the actions for the event flags - because it is called a 'game loop', doesn't mean it only appilies to games; it applies to any event driven program.

    4] Structuring the program as described in this Wiki article will help and is certainly good practice.

    We can further help explaining the above, if you have specific questions - perhaps with short simple test examples.

    The full list of these Wiki articles can be found here.

    • Marked as answer by box57 Tuesday, August 14, 2018 12:46 PM
    Sunday, August 12, 2018 11:22 AM

All replies

  • I cant run your code as posted - do you have a small sample that shows the issue
    Friday, August 10, 2018 7:41 PM
  • Save the .sb file in a folder, otherwise the game folders and everything else will land on the file path where the .sb file is.

    If the game won't download the textures, open the settings.txt and change download=1 into download=0 and then save it and run the game.

    Edit: You don't even need the textures but they will be downloaded anyways.

    • Edited by box57 Saturday, August 11, 2018 12:13 PM
    Saturday, August 11, 2018 12:11 PM
  • Your program looks great.

    It load loads of images (fine), then sets the arrows (fine), the set events for mousemove and mousedown (also fine, but see other comments below), then it moves to the end of the program (it is finished - appears to crash).

    Solution - you need a game loop - see for more details https://social.technet.microsoft.com/wiki/contents/articles/20865.small-basic-dynamic-graphics.aspx

    Events happen asyncronously and you shouldn't do graphicswindow manipulations here really, they shoudld be done on the main UI thread so they aren't being done asynchronously (at the same time).

    My simple temp fix is to add a game loop to keep the programming runnung at the end, but you should consider putting all code which is in the main loop at the start followed by subroutines - mixing them up can cause confusion.

    While ("True")
      If (mouseMove) Then
        MousePosition()
        mouseMove = ""
      EndIf
      Program.Delay(20)
    EndWhile

    This calls your MousePosition on the main UI thread when the mouse moves (happens a lot so we need to only do it when the UI thread is available to do it).

    To do this I set a flag (mouseMove) when the mouse moves, which is processed in the game loop.

    GraphicsWindow.MouseDown=down
    GraphicsWindow.MouseMove=OnMouseMove
    
    Sub OnMouseMove
      mouseMove = "True"
    EndSub

    The modified program - XHM306

    I would re-order the code as suggested by the wiki article to be ordered:

    1] Call Initialise subroutine (it can call other subs if needed) to do most of your setup including registering all events and writing the flags for downloaded files etc

    2] Start a game loop to process events

    3] All your subroutines to do stuff including the initialisation subroutine

    4] Event subroutines to set flags processed in the game loop

    Good luck

    • Edited by litdev Saturday, August 11, 2018 3:08 PM
    • Marked as answer by box57 Sunday, August 12, 2018 9:30 AM
    • Unmarked as answer by box57 Sunday, August 12, 2018 9:34 AM
    Saturday, August 11, 2018 2:59 PM
  • Sorry, I used ur code wrong.

    It seems like it works, a big thanks to you!

    Edit: nvm, its not working

    To make sure which problem I exactly mean:

    1) Run my Game

    2) Click on "one player"

    3) Left click on the textbox (game crashes)

    • Edited by box57 Sunday, August 12, 2018 9:36 AM
    Sunday, August 12, 2018 9:29 AM
  • Hi,

    I published the changes I made to XHM306

    Please import this, save it to the same place as your version (so the download settigs file file can be found) and uncomment the auto-commented File commands (lines 38 only if using existing settings).

    This then runs not crashing as you describe.


    However this was to illustrate the reasons for the crash, not a final solution.

    To proceed with your program you will need to understand why:

    1] GraphicsWindow and most other actions should be performed on the main UI (User Interface) thread.  Event subroutines are performed asynchronously, not on the UI thread.  The UI is not updated until after the event has completed and may cause bad/unpredictable/crash behaviour if they occur concurrently. See this Wiki article and  this Wiki article.

    2] Only set flags (simple variables) in event subroutines, especially ones that are called often.  Best practice do this for ALL event subroutnes.

    3] Use a 'game loop' to control the actions for the event flags - because it is called a 'game loop', doesn't mean it only appilies to games; it applies to any event driven program.

    4] Structuring the program as described in this Wiki article will help and is certainly good practice.

    We can further help explaining the above, if you have specific questions - perhaps with short simple test examples.

    The full list of these Wiki articles can be found here.

    • Marked as answer by box57 Tuesday, August 14, 2018 12:46 PM
    Sunday, August 12, 2018 11:22 AM
  • Ah sorry, I found the problem. Your solution was right.

    Thanks^^

    Tuesday, August 14, 2018 12:46 PM