Jump to content

Some questions about brains


Recommended Posts

So, I've been developing a mod that adds a custom brain to your character.
So far I've developed locally so I haven't run into any problems regarding clients and stuff.
But as I understand it, brains only run on the server and the behaviour then gets synced to the clients, which makes sense.
The purpose of my mod is to be use the brain as a button to toggle the brains between my brain and the default brain for characters.

Currently I have this code

local function SetSelfAI()
	print("Enabling Artificial Wilson")

	local brain = GLOBAL.require "brains/artificialwilson"
	GLOBAL.ThePlayer:SetBrain(brain)
	GLOBAL.ThePlayer:RestartBrain()
	
	ArtificalWilsonEnabled = true
end

local function SetSelfNormal()
	print("Disabling Artifical Wilson")
	
	local brain = GLOBAL.require "brains/wilsonbrain"
	GLOBAL.ThePlayer:SetBrain(brain)
	GLOBAL.ThePlayer:RestartBrain()

	ArtificalWilsonEnabled = false
end

Which is working well locally. However when a player (running as a client) tries to use the button it crashes due to missing components (which I understand is normal).

From what I've read I think I need to use RPC.
Register an RPC handler both on the server and the client and make the client call this function through RPC whenever I need to switch brains.
My question is, how do I do this? I can't figure it out and would appreciate some help.
 

On another topic.
I understand that there are several ways to do the same thing, but Is this okay to make the brain clickable?

local function MakeClickableBrain(self, owner)
	local BrainBadge = self
	
	BrainBadge:SetClickable(true)
	BrainBadge.OnMouseButton = function(self,button,down,x,y)	
		-- My Stuff here
	end
end
AddClassPostConstruct("widgets/sanitybadge", MakeClickableBrain)


 

Edited by Hineios
Link to comment
Share on other sites

So, I've figured this out so I'll just leave the answer here for another helpless soul looking for guidance.
It's actually quite simple.

You need only to register an RPC handler (I putted mine in the modmain.lua), like so:

AddModRPCHandler(modname, "My custom name here", function(player)
    if player then
    	-- My stuff here
		-- This portion of the code will be executed on the server,
        -- so it will have access to everything an entity has
    end
end)

And then call this handler like so:

SendModRPCToServer(MOD_RPC[modname]["My custom name here"])

 

If you which to pass additional data to the handler it is also easy (user configurations for example):

AddModRPCHandler(modname, "My custom name here", function(player, other_data)
    if player then
    	-- My stuff here
    end
end)

SendModRPCToServer(MOD_RPC[modname]["My custom name here"], other_data)

 

Cheers!

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...