How to run two different objects at the same time in different loop?

I am making an arcade game in which a soldier shoot enemies using keyboard input. I need to move enemies continuously but the soldier should only move through keyboard input. If i use a for or while loop and put callback for keyboard input in it, enemy's movement stops while callback function runs.So how could i do this without interruption in movement of enemies. Any suggestions?

Respuestas (2)

Guillaume
Guillaume el 30 de Nov. de 2015
Editada: Guillaume el 30 de Nov. de 2015
I'm afraid you're running into a fundamental limitation of matlab. matlab is single threaded, this means only one piece of code can run at a time. If a callback is running, nothing else can be running.
While there is the parallel computing toolbox or workarounds to run multithreaded code in matlab, these won't help in your case as you want to modify/access the user interface (display and keyboard input). Modifying/Accessing the UI can only be done on the UI thread, matlab main thread.
The best you can do is compute your enemies and soldier mouvement in background threads (using PCT or workarounds) and do the display for all in your main matlab code.
In my opinion though, you'd be better off coding your game in a different language.

2 comentarios

timer callbacks can interrupt graphics callbacks. However, if you change the state of a graphic object while you are in a timer callback then the graphics callback might not be expecting that possibility and might encounter difficulties.
Whatever you do, it doesn't change the fact that matlab is single threaded. If you have a fast changing display, any callback will inevitably slow-down that display unless that callback is executing in a separate thread.
If a callback is executing in a different thread, it cannot interact with the user interface. No display, no keyboard/mouse input. UI operations have to be marshalled back on the main thread, but there's no mechanism for that in matlab.
Again, assuming a fast moving display, the only way I could see the OP code working is with:
  • fast moving display on main matlab thread (you don't have a choice)
  • movement calculation on a background thread
  • callback keyboard event on main thread (you don't have a choice). callback immediately dispatch to background thread for movement calculation.
Background thread to be implemented using parallel computing toolbox (no experience there, no idea if it's feasible), or java / posix / .Net thread as per undocumentedmatlab. .Net threads would certainly work, but to be honest, I'd rather write the whole thing in .Net, it would be much easier.

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 30 de Nov. de 2015
Put the enemy movement in a timer function.
Or instead of you "put callback for keyboard input", put in drawnow() as that will check to see if there is a graphics input and will service it if there is. You would then attach a WindowKeyPressFcn to the figure to detect the keyboard action.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 30 de Nov. de 2015

Comentada:

el 30 de Nov. de 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by