About Monkey 2 › Forums › Monkey 2 Programming Help › Fiber confusion (SOLVED?)
This topic contains 3 replies, has 3 voices, and was last updated by
cocon 1 year, 12 months ago.
-
AuthorPosts
-
April 18, 2017 at 4:03 am #7945
I’m trying to make a new fiber/thread that keeps going in the background at all times in my game.
It updates some stuff much faster than the 60 FPS I render in the game.
But the fiber pauses the game, and I thought that was the whole point of fibers, to not pause the game?
I’m guessing I’m doing something wrong?Here’s some test code, what am I doing wrong?
Press space to run a fiber and notice the text stops scrolling.Monkey12345678910111213141516171819202122232425262728293031323334353637#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowMethod New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=Null )Super.New( title,width,height,flags )EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()canvas.DrawText( "Hello World!",Width/2+Millisecs()*0.1 Mod 320,Height/2,.5,.5 )If Keyboard.KeyHit(Key.Space) ThenNew Fiber(Lambda()Local i:IntWhile i<10Print ii+=1Sleep(0.1)WendEnd)EndifEndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndUPDATE: It seems Fiber.Sleep() pauses the fiber, while Sleep() pauses the whole app!
I’m still wondering if I’m doing this correctly.April 18, 2017 at 10:17 am #7952UPDATE: It seems Fiber.Sleep() pauses the fiber, while Sleep() pauses the whole app!
I’m still wondering if I’m doing this correctly.You are right – should to use Fiber.Sleep() inside of fiber to sleep it.
What are you wanna get at the end?
Monkey2 also has Timer and Future stuff, maybe you need one of them.
April 18, 2017 at 6:46 pm #7956My game is a music game, so it has to update the music at all times in the background.
Correct timing of every note is important!
So I want a fiber running in the background taking care of all that while the game runs normally.I’d like to never force the fiber to sleep, but if I don’t put it to sleep, the game still pauses.
April 21, 2017 at 2:23 pm #7984From Mark’s blog…
http://monkey2.monkey-x.com/
“However, unlike real threads, fibers don’t run ‘in parallel’.”I had tried also some stuff with fibers a while ago and I found that their usage is quite limited, but it’s very specific. From what I found in practical terms, fibers are more useful in event oriented applications (like Ted2Go) where you don’t want your application run in a 60fps loop but neither you want intense loops to freeze your user interface. For example if you have an intense loop, you can throw it in a thread, to let your main app – the gui mostly – be responsive.
Generally if your game runs at 60fps you won’t have any problems to use fibers, unless you want to do even more intense calculations (like lightmapping).
-
AuthorPosts
You must be logged in to reply to this topic.