Possible to mod in a game timer?

aeroripperaeroripper Join Date: 2005-02-25 Member: 42471NS1 Playtester, Forum Moderators, Constellation
<div class="IPBDescription">like NS1</div>Anybody good with lua, is it possible to add in a simple game timer like NS1 used to have? I'm not immediately seeing where I would go to add in such a thing.

Comments

  • playerplayer Join Date: 2010-09-12 Member: 73982Members
    Easy-peasy. The real challenge lies in getting that information to the client in a graceful way, as it's a server-side mod and Lua-files are not synced to clients (at this time). What you'd probably end up with is having to type !timeleft or something to that effect.
  • McGlaspieMcGlaspie www.team156.com Join Date: 2010-07-26 Member: 73044Members, Super Administrators, Forum Admins, NS2 Developer, NS2 Playtester, Squad Five Blue, Squad Five Silver, Squad Five Gold, Reinforced - Onos, WC 2013 - Gold, Subnautica Playtester
    @player: This would be a great option for Om to have ;)
  • wulf 21wulf 21 Join Date: 2011-05-03 Member: 96875Members
    Maybe it could be done in the balance mod because the client is expected to download the mod, too? If client and server files the same anyway, you could get that info to the client via Server.SendNetworkMessage(). You don't even need to synchronize it because all the clients already know a time that is synchronized to the server (but that's the time since server started I think). So all you need would be to send GetGamerules().gameStartTime to all clients when the game starts and to any client that connects in the middle of the game. Then every client can calculate the game-time from Shared.GetTime()-gameStartTime.
  • playerplayer Join Date: 2010-09-12 Member: 73982Members
    Well sure, if you can customize the client you can do whatever you wish, was kinda thinking of vanilla-NS2 myself. But yes if the Prototyping-Mod has to be downloaded by a client to function then yeah it could do a timer no problem.
  • TinCanTinCan Join Date: 2006-12-11 Member: 59010Members
    edited July 2011
    <!--quoteo(post=1864295:date=Jul 28 2011, 06:14 PM:name=player)--><div class='quotetop'>QUOTE (player @ Jul 28 2011, 06:14 PM) <a href="index.php?act=findpost&pid=1864295"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->Lua-files are not synced to clients (at this time)<!--QuoteEnd--></div><!--QuoteEEnd-->

    Not synced at this time. Does that mean it should happen eventually? Has any 'official' word been given on this subject?
  • playerplayer Join Date: 2010-09-12 Member: 73982Members
    I believe it was going to be like Garry's mod, ea. all the server's game-logic Lua-files are downloaded to the client upon connecting, and I think that is still the plan, but they're pretty occupied with other things at the moment, so it might not make it to the 1.0-release. I'm sure if you browse through the news-archives you might find a bit where they talk about it, but it's pretty old, so that may have changed in the mean time.
  • HackepeterHackepeter Join Date: 2003-06-08 Member: 17107Members, Constellation
    I made an quick and dirty test implementation a few weeks ago. I sent the current game time to the client on every update and then drew it on the client side to the scoreboard. Yeah, it's not necessary to send it that often via a solution wulf 21 suggested but I just wanted to play around with the network messages. It's quite easy to implement. Unfortunately its not possible to provide a client only mod.
  • endarendar Join Date: 2010-07-27 Member: 73256Members, Squad Five Blue
    Feel like posting the server side code you have?
  • HackepeterHackepeter Join Date: 2003-06-08 Member: 17107Members, Constellation
    If it helps you I will write down the basic steps to get this working. For this solution it's necessary to implement new code on client and server. I'll keep it as much simple as possible.

    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! :)
  • swalkswalk Say hello to my little friend. Join Date: 2011-01-20 Member: 78384Members, Squad Five Blue
    <!--quoteo(post=1865070:date=Jul 31 2011, 06:40 AM:name=TinCan)--><div class='quotetop'>QUOTE (TinCan @ Jul 31 2011, 06:40 AM) <a href="index.php?act=findpost&pid=1865070"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->Not synced at this time. Does that mean it should happen eventually? Has any 'official' word been given on this subject?<!--QuoteEnd--></div><!--QuoteEEnd-->
    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-->
  • twilitebluetwiliteblue bug stalker Join Date: 2003-02-04 Member: 13116Members, NS2 Playtester, Squad Five Blue
    Nice work Hackepeter!

    I really hope file matching won't be enforced by default, or the how are we to take advantage of the mod-ability?
  • playerplayer Join Date: 2010-09-12 Member: 73982Members
    <!--quoteo(post=1881612:date=Oct 24 2011, 04:20 PM:name=twiliteblue)--><div class='quotetop'>QUOTE (twiliteblue @ Oct 24 2011, 04:20 PM) <a href="index.php?act=findpost&pid=1881612"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->I really hope file matching won't be enforced by default, or the how are we to take advantage of the mod-ability?<!--QuoteEnd--></div><!--QuoteEEnd-->
    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.
  • aeroripperaeroripper Join Date: 2005-02-25 Member: 42471NS1 Playtester, Forum Moderators, Constellation
    Hope this mod gets implemented officially in build 188. I miss having a game timer :(
  • SewlekSewlek The programmer previously known as Schimmel Join Date: 2003-05-13 Member: 16247Members, NS2 Developer, NS2 Playtester, Squad Five Gold, Subnautica Developer
    game timer will be available in 188. i just added one :)
  • aeroripperaeroripper Join Date: 2005-02-25 Member: 42471NS1 Playtester, Forum Moderators, Constellation
Sign In or Register to comment.