Hi,
I'm trying to write a console application, for my game server and I'm having difficulties with my main loop. It appears that Console.ReadLine() will pause my main loop, which must never happen. It also blocks additional game events from being fired
Does anyone know an alternative for the Console.ReadLine() that does not block events from firing, or pause my loop?
Here is my code:
The problem is at Step 4 in the main loop.
Thanks in advance,
Steve
I'm trying to write a console application, for my game server and I'm having difficulties with my main loop. It appears that Console.ReadLine() will pause my main loop, which must never happen. It also blocks additional game events from being fired
Does anyone know an alternative for the Console.ReadLine() that does not block events from firing, or pause my loop?
Here is my code:
VB.NET:
Sub Main()
Dim ConsoleInput As String ' Hold User's Console Command input
Dim ErrVal As String ' Hold Dummy Variable to store error messages
Dim LastFrameTick As Long ' Holds the last frame tick
' First initialize the Game Core object classes:
If Not Init_GameCore(ErrVal) Then
MsgBox("Failed initializing Game Core!" & vbCrLf & ErrVal, MsgBoxStyle.Critical, AppTitle)
End ' Failed, end program here
End If
' Initialize our Console Engine (mConsole). This will handle and
' parse any commands sent on the command line. It will also intialize
' the MCP Abstract Machine for plugin handeling and the Python Script
' Extender:
If Not Init_ConsoleCore(ErrVal) Then
MsgBox("Failed initializing Console Core!" & vbCrLf & ErrVal, MsgBoxStyle.Critical, AppTitle)
End ' Failed, end program here
End If
' This is our main loop. 1 Cycle = 1 frame. To set maximum cylcles per second
' adjust the console variable sv_fpsmax. (Max value = 1000, Default = 66)
' This loop is infinite until mainLoopStatus = 0.
GameCore.MainLoopStatus = 1
While GameCore.MainLoopStatus > 0
'// === Step 1 === (Delay)
'// Force the application to sleep for a while, to lower CPU usage:
'// Equation: SleepTimeMs = 1000ms / FSP (Default: 66 FPS: 1000ms / 66 = 15ms)
System.Threading.Thread.Sleep(CShort(1000 / CShort(getConVar("sv_fpsmax"))))
'// === Step 2 === (Change gameframe data)
'// Recalculate game objects and variables:
GameCore.RenderFrame()
'// === Step 3 === (Events)
'// Make sure all events are fired:
System.Windows.Forms.Application.DoEvents()
'// === Step 4 === (Input)
' Try to read a command from the console. If a command is sent, let the mConsole handle it:
Console.Write("Command: ") : ConsoleInput = Console.ReadLine()
If ConsoleInput <> "" Then mConsole.EXECUTE_CONSOLE_COMMAND(ConsoleInput, "Root")
'// === Step 5 === (FPS)
'// Calculate live Frames Per Second
GameCore.LiveFPS = 1000 / (GameCore.getGameTick() - LastFrameTick)
LastFrameTick = GameCore.getGameTick()
End While ' Set next render frame
CleanUp() ' Do clean up operations
End ' Make sure this program has ended
End Sub
The problem is at Step 4 in the main loop.
Thanks in advance,
Steve