Freesnöw
Freesnöw

Reputation: 32163

Waiting x seconds before continuing while continuing to respond to user input and render frames

Background

I am currently creating an XNA application that utilizes LuaInterface so that I can control the application with Lua scripts.


What I Want

I've noticed that a lot of things will probably be 'time' based in my development. Such things may include showing information and then removing it after x amount of seconds, end a game in defeat if it takes x amount of seconds, or even used in easing a camera a certain distance every x seconds. I wanted to implement the wait function in Lua as opposed to my application, but at this point, I'm open to input (because I can't seem to get it to work).


What I've Tried So Far

inv.lua

 _G.Wait=(function(t)
    local dest=Time.ElapsedMilliseconds+t*1000
    while Time.ElapsedMilliseconds<dest do
        gel:UIUpdate()
    end
    return true
 end)

 --Aliases
 _G.wait=Wait

gELua.vb

Public Sub UIUpdate()
    Application.DoEvents()
    Game.Tick()
End Sub

(Where Game is My XNA Game Object)


So Why Doesn't My Idea Work?

The current problem is that, even though I'm calling Game.Tick() every time to keep everything going, the XNA part of my application stops receiving input.


So, my question is, is there a better way to do this? I thought it was working pretty well until I encountered the problem with user input. Have any of you implemented something like this that worked well?

Upvotes: 3

Views: 375

Answers (1)

Ani
Ani

Reputation: 10906

What you want is for your game loop to continue running - but not call "Update" (that's inside a lua script) for an object. If each of your objects are different scripts, then this is fairly simple. A "Wait(time)" method will only tell your game engine that "this" object doesn't want to be updated for time milliseconds. The engine will note that down and continue calling Update and Render for all the other scripts (objects).

The next time it comes around to Update/Render the "waiting" object, it will skip the "Update" call and call only Render instead. When the timeout expires - you can resume calling Update on the object. If possible - you could resume execution of the Update method from the next line you suspended it from. This would make writing the script simple, like a C# "async" method.

Of course - this depends heavily on how your scripts interact with the rest of your engine. If the described solution does not work for you, I would request you to describe how your scripts interact with the engine in greater detail.

Upvotes: 3

Related Questions