Possible to mod in a game timer?
aeroripper
Join Date: 2005-02-25 Member: 42471NS1 Playtester, Forum Moderators, Constellation
in Modding
Comments
Not synced at this time. Does that mean it should happen eventually? Has any 'official' word been given on this subject?
At first we have to define a new network message and register it to client and server:
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->// File: NetworkMessages.lua //
local kGameTimeMessage =
{
timeGameStateChanged = "integer" // Just transport seconds as integer, ignore milliseconds
}
Shared.RegisterNetworkMessage("GameTimeChanged", kGameTimeMessage)<!--c2--></div><!--ec2-->
Now the server can send the game time in that message to the client. The Spark script says: "Currently all messages are sent unreliably. This means that it's possible for the message to not by received in the case of packet loss" so to be on the safe side I'll send the current game time every second. 4 bytes per second per player shouldn't be a problem:
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->// File: NS2Gamerules.lua //
function NS2Gamerules:OnUpdate(timePassed)
--- Snip ---
self.timeSinceGameStateChanged = self.timeSinceGameStateChanged + timePassed // <-- find that line
//Crop milliseconds
local gameTime = math.floor(self.timeSinceGameStateChanged)
//Only send the message if we reached a new second
if gameTime ~= self.timeLastGameTimeMessage then
Server.SendNetworkMessage("GameTimeChanged", { timeGameStateChanged = gameTime }, true)
self.timeLastGameTimeMessage = gameTime
end
--- Snip ---
end<!--c2--></div><!--ec2-->
At last the client has to hook to that message, so we can handle it if we receive the message:
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->// File: NetworkMessages_Client.lua //
kTimeGameStateChanged = 0 // here we save our game time on the client side
// this function gets called if we receive the message on the client side
function OnCommandGameTimeChanged(message)
kTimeGameStateChanged = message.timeGameStateChanged
end
Client.HookNetworkMessage("GameTimeChanged", OnCommandGameTimeChanged)<!--c2--></div><!--ec2-->
and thats it! Now the client has the current game time available over the global variable kTimeGameStateChanged.
For a quick demonstration lets add it somewhere to the scoreboard:
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->// File: GUIScoreboard.lua//
function GUIScoreboard:UpdateTeam(updateTeam)
--- Snip ---
// Convert the seconds to a common time representation
local minutes = math.floor(kTimeGameStateChanged/60)
local seconds = kTimeGameStateChanged - minutes*60
local gameTimeText = string.format("%d:%02d", minutes, seconds)
// Update the team name text. // Hackepeter: and append the current game time
teamNameGUIItem:SetText(string.format("%s (%s) - Time: %s", teamNameText, Pluralize(numPlayers, "Player"), gameTimeText)) // <-- find that line
--- Snip ---
end<!--c2--></div><!--ec2-->
and here the working screenshot:
<img src="http://dl.dropbox.com/u/8054606/2011-08-26_00001.jpg" border="0" class="linked-image" />
That's all! As I said I wanted to keep it simple, so no optimized refactoring as you see. Also I didn't wanted to go in to detail how to add new items to the GUI, so just an simple appand here.
If you have further questions don't hesitate to ask me! :)
Next patch maybe.
<!--quoteo--><div class='quotetop'>QUOTE </div><div class='quotemain'><!--quotec-->Technical task Check that client files match the server files on connect has appeared with status Unstarted<!--QuoteEnd--></div><!--QuoteEEnd-->
I really hope file matching won't be enforced by default, or the how are we to take advantage of the mod-ability?
That's a complicated issue that so far hasn't been discussed very much. As it stands, you MUST have SOME kind of consistency checking going on, as right now it's trivial to cheat at this game (you would only need to modify the client's Lua-files to accomplish a myriad of advantages without the server being any of the wiser). If consistency-checking went into the game right now, you would only be able to run mods like this if they were running on the server as well. I think the most feasable solution for this would be to let this consistency-checking take place on the Lua-level, and thus allow modders to exercise control over it. This way you can have a community-effort to index the hashes of mods and you can decide at runtime whether a client-side mod is permitted based on said index.