Jump to content

Make Creatures(Beefelo, Pig, bees etc.) follow/attack me


Recommended Posts

@rezecib, I actually figured out a really simple way to 'hack' it together by using Actions.

 

@Crazybait24, the following code will get you started on your way to making this a reality, however, you will need to put in a lot of time and effort to actually finish it. This code will specifically contains key press/down handling, communication from client to server and a demonstration of how it works by altering your health upon key press.

 

modmain.lua

local function OnKeyPressed(inst, data)    --if data.player == GLOBAL.ThePlayer then        if data.key == GLOBAL.KEY_T then            print("T key has been pressed.")             if GLOBAL.TheWorld.ismastersim then                -- Since we are the server, do the action on the server.act.target.components.health:SetCurrentHealth(1)act.target.components.health:DoDelta(0)            else                GLOBAL.SendRPCToServer(GLOBAL.RPC.DoWidgetButtonAction, GLOBAL.ACTIONS.BECOMEBAIT.code, inst, GLOBAL.ACTIONS.BECOMEBAIT.mod_name)            end        end    --endend Wilson = function(self)    self:AddComponent("keyhandler")     self:ListenForEvent("keypressed", OnKeyPressed)         return selfend AddPrefabPostInit("wilson", Wilson)  local BECOMEBAIT = GLOBAL.Action()BECOMEBAIT.str = "BecomeBait"BECOMEBAIT.id = "BECOMEBAIT"BECOMEBAIT.fn = function(act)            act.target.components.health:SetCurrentHealth(1)            act.target.components.health:DoDelta(0)            return trueend AddAction(SETHEALTH)  

 

scripts/components/keyhandler.lua

local KeyHandler = Class(function(self, inst)   self.inst = inst   self.handler = TheInput:AddKeyHandler(function(key, down) self:OnRawKey(key, down) end )end) function KeyHandler:OnRawKey(key, down)  if (key and not down) and not IsPaused() then      ThePlayer:PushEvent("keypressed", {player = ThePlayer, key = key})    elseif key and down and not IsPaused() then      ThePlayer:PushEvent("keydown", {player = ThePlayer, key = key})  endend return KeyHandler  

 

Alter the BECOMEBAIT.fn to suit your needs. This will get you started with the set-up, next you will need to figure out how to make creatures follow you.

 

Edited by Kzisor
Link to comment
Share on other sites

Actually thinking about it... With this code you sent me why would I need to have them just follow me? My character is on the workshop but I'm editing in some things and making it more then just a character but what I am getting at is if I use said code I will be able to do what I was wanting... I just wanted to make my character get attacked/followed by creatures which is what you did in the code(hopefully not 100%, haven't tested it). With them now being able to attack me with key_t my character no londer has to hit said beast to make it mad. So in any case I'll just try that later after finishing a character for a friend.. Thanks @Kzisor and @rezecib I might just be able to make this work in a way that I need it to.

Link to comment
Share on other sites

@Crazybait24, the code that I posted, doesn't make creatures attack you. It displays how to use key handlers and RPC in a simple fashion through adding a new Action.

 

if you are wanting to get creatures to follow and/or attack you, you will need different code and or actions on the server. However, what I posted fives you a great start.

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