Jump to content

Execute function on connection


Recommended Posts

For my mod, each player has different config value, which influences the values in their minion's brains (I'm the creator of the Shadow Control mod).
If these values are nil, the game will crash, so the values must be sent and set when they join the game (I may just make it use the default config values when they aren't sent so that it doesn't crash). The values are sent through a RPC call, to a function that sets it for the player that sends the call. I've been attempting to place the RPC call in modmain, at the end of modmain, or in AddPlayerPostInit, all of which have failed. I have even attempted to set up a listener to execute it, but it doesn't seem to work. How do I execute a function after a client connects?

The function:

function sendInitFunction()
SendModRPCToServer(MOD_RPC[modname]["ShadowControlInit"], MIN_KITE_MAXWELL, MAX_KITE_MAXWELL, ATT_MAXWELL_COOLDOWN, RUN_MAX_FOLLOW_DIST, FLW_TARGET_FOLLOW_DIST, ATT_RUN)
end

and
 

function ShadowControlInit(player, _MIN_KITE_MAXWELL, _MAX_KITE_MAXWELL, _ATT_MAXWELL_COOLDOWN, _RUN_MAX_FOLLOW_DIST, _FLW_TARGET_FOLLOW_DIST, _ATT_RUN)
	GLOBAL.REVISED_MAXWELL_RUN[player] = false
	GLOBAL.REVISED_MAXWELL_ATT[player] = false
	GLOBAL.REVISED_MAXWELL_FLW[player] = false
	GLOBAL.MIN_KITE_MAXWELL[player] = _MIN_KITE_MAXWELL
	GLOBAL.MAX_KITE_MAXWELL[player] = _MAX_KITE_MAXWELL
	GLOBAL.ATT_MAXWELL_COOLDOWN[player] = _ATT_MAXWELL_COOLDOWN
	GLOBAL.RUN_MAX_FOLLOW_DIST[player] = _RUN_MAX_FOLLOW_DIST
	GLOBAL.FLW_TARGET_FOLLOW_DIST[player] = _FLW_TARGET_FOLLOW_DIST
	GLOBAL.ATT_RUN[player] = _ATT_RUN
end

 

Edited by WorldDeva
Link to comment
Share on other sites

Does it need to be sent before the player spawns?

If not, then you could do:

AddPrefabPostInit("world",
    function(inst)
        inst:ListenForEvent("playeractivated",
            function(inst, data)
                if data == GLOBAL.ThePlayer
                then
                    -- RPC here
                end
            end
        )
    end
)

It should fire off every time the client player spawns in, for instance when moving between overworld and caves.

Link to comment
Share on other sites

@CarlZalph Thanks for the help! 

Unfortunately, the function does need to be executed before the player spawns, because the behaviorTree Nodes load in with those variables and are unchangeable (might try using addbrainpostinit, but didn't work in the past). :( 

Another question (sorry (シ_ _)シ), where is the initialization/networking stuff located? network.lua only has a bunch of functions, and I've looked through everywhere that TheNet is mentioned, and it isn't there (I've also looked for OnPlayerJoin).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...