Calling functions across 2 separate lua scripts.

ViralXKViralXK Join Date: 2013-05-13 Member: 185218Members
edited May 2013 in Modding
Anybody know how to use functions across 2 separate Lua files?
I tried Script.Load and all I get in the game freaking out when I try to use it.


I also need help with calling Server.GetMaxPlayers() properly. I don't know if I'm doign that correctly either. I need an example of both really.

Edit: I think it may be more of a problem with me trying to call Server.GetMaxPlayers(). How do I exactly get the numbers of players the server can hold so I can use it in my script?

Comments

  • ArtfunkelArtfunkel Join Date: 2013-05-10 Member: 185184Members
    Once a script has been loaded anywhere, everything inside it (except stuff marked local) is available everywhere else.

    There doesn't seem to be anything special about calling Server.GetMaxPlayers(). Just make sure that you're calling it on the server only, otherwise it won't exist.
  • ViralXKViralXK Join Date: 2013-05-13 Member: 185218Members
    Well my problem is, how do I get this information to the client because a lot of my client algorithms are based on player capacity of the server. Nothing works without this info.

    I dont exactly know how I would get the info to the client. A lot of how this game works in a mystery to me

    Anybody got example of how I would get this info to the client?
  • ArtfunkelArtfunkel Join Date: 2013-05-10 Member: 185184Members
    edited May 2013
    You'll need to modify an entity to make it transmit that data. Don't ask me which, I've never had to choose!

    Do this:
    // Put this in a file that is loaded by both the client and the server.
    local newNetworkVars = { maxPlayers = "integer" }
    Class_Reload("ChosenClass", newNetworkVars)
    And this:
    // put this on the server only
    local originalFunc = ChosenClass.SomeFunc
    
    function ChosenClass:SomeFunc(whatever_args)
        originalFunc(whatever_args)
        self.maxPlayers = Server.GetMaxPlayers()
    end

    You should then find that ChosenClass.maxPlayers provides the value you want on the client.
  • MCMLXXXIVMCMLXXXIV Join Date: 2010-04-14 Member: 71400Members
    To send info about the game to the clients (for Xenoswarm) I ended up having to write an entirely new entity called GamerulesInfo which exists on both server and client, and then tell NS2Gamerules to create one when it is created and update it at the end of the OnUpdate function.. if you need a more detailed example hit me up on steam sometime and I can walk you through it!
  • MCMLXXXIVMCMLXXXIV Join Date: 2010-04-14 Member: 71400Members
    A lighter example is to write a new network message and send it to clients whenever someone joins or the max players changes. On the client side you'd just set a global variable e.g. CurrentMaxPlayers from whatever you receive in that message.
  • ViralXKViralXK Join Date: 2013-05-13 Member: 185218Members
    MCMLXXXIV said:
    A lighter example is to write a new network message and send it to clients whenever someone joins or the max players changes. On the client side you'd just set a global variable e.g. CurrentMaxPlayers from whatever you receive in that message.
    I think I will give you a shout. This is pretty frustrating. I just want to send one number from the server to the client and it's apparently rocket science. I saw the network messages code and started to try and write one, but it doesnt work at all and it seems very strange and contradictory in the way it's written.
  • ArtfunkelArtfunkel Join Date: 2013-05-10 Member: 185184Members
    I had to do something similar to this today. Here is some code that works (make sure you are using an entry file):

    Shared:
    Script.Load("lua/Class.lua")
    local gameInfoNetVars = { maxPlayers = "integer" }
    Class_Reload("GameInfo",gameInfoNetVars)
    Server:
    local base_GameInfo_OnCreate = GameInfo.OnCreate
    function GameInfo:OnCreate()
    	base_GameInfo_OnCreate(self)
    	self.maxPlayers = Server.GetMaxPlayers()
    end
    Client:
    ...
    GetGameInfoEntity().maxPlayers
    ...
  • ArtfunkelArtfunkel Join Date: 2013-05-10 Member: 185184Members
    Um, did you not notice Client.GetServerMaxPlayers() and Client.GetServerNumPlayers()?
  • ViralXKViralXK Join Date: 2013-05-13 Member: 185218Members
    Artfunkel those last calls you posted dont work. Which still leaves me to write network calls to pass the info sadly. I appreciate all the help though.
  • lwflwf Join Date: 2006-11-03 Member: 58311Members, Constellation
    edited June 2013
    Artfunkel wrote: »
    Um, did you not notice Client.GetServerMaxPlayers() and Client.GetServerNumPlayers()?

    Those functions are for a server, not the server. It's only used from the server browser.
Sign In or Register to comment.