Jump to content

Properly checking an items Owner


Recommended Posts

Hello! I'm making a Hat for the game that has the ability to freeze nearby enemies, but I can't seem to define the Owner of the item properly, and I'm resorting to disgusting local variables that I don't wanna use.

How can I properly define the owner of an item, and then use that to see if the owner has a certain tag? In this case, the tag is WearingIceHat.

Link to comment
Share on other sites

Howdy!
Inside OnUnequip function

Quote

owner:RemoveTag("WearingIceHat")


Inside OnEquip function

Quote

owner:AddTag("WearingIceHat")

Above main function:

local function ayylmao(inst)
local owner = inst.components.inventoryitem.owner
local pt = inst:GetPosition()
local range = 15  -- range of spell
local tags = { "monster", "character", "hostile" }
local canthavetags = {"WearingIceHat"}

	local targets = TheSim:FindEntities(pt.x,pt.y,pt.z, range, nil, canthavetags, tags)	
		for _,ent in ipairs(targets) do
			if ent.components.freezable ~= nil then
				ent.components.freezable:AddColdness(5)
				ent.components.freezable:SpawnShatterFX()
			end
		
		end          
end

local function fugg(inst)
local owner = inst.components.inventoryitem.owner	

 	if not owner.components.health:IsDead() and not owner:HasTag("playerghost") and owner:HasTag("WearingIceHat") then				
		ayylmao(inst)	
	end
end

Inside main function:

Quote

    inst:DoPeriodicTask(10, fugg, nil, inst)

Cheers!

  • Like 1
  • Sanity 1
Link to comment
Share on other sites

51 minutes ago, Yakuzashi said:

Howdy!
Inside OnUnequip function


Inside OnEquip function

Above main function:


local function ayylmao(inst)
local owner = inst.components.inventoryitem.owner
local pt = inst:GetPosition()
local range = 15  -- range of spell
local tags = { "monster", "character", "hostile" }
local canthavetags = {"WearingIceHat"}

	local targets = TheSim:FindEntities(pt.x,pt.y,pt.z, range, nil, canthavetags, tags)	
		for _,ent in ipairs(targets) do
			if ent.components.freezable ~= nil then
				ent.components.freezable:AddColdness(5)
				ent.components.freezable:SpawnShatterFX()
			end
		
		end          
end

local function fugg(inst)
local owner = inst.components.inventoryitem.owner	

 	if not owner.components.health:IsDead() and not owner:HasTag("playerghost") and owner:HasTag("WearingIceHat") then				
		ayylmao(inst)	
	end
end

Inside main function:

Cheers!

Wow! This worked wonders on what I'm trying to do! Thanks for the help, though I have one more question. If I wanted to do the freeze when I pressed a key instead of on a timer like it is in your code, how would I do so?

Right now I have this

TheInput:AddKeyDownHandler(304,fugg(inst))

But all this does is cause errors, because inst doesn't technically exist outside of a function, so then how do I declare it in this instance?

Link to comment
Share on other sites

In my HUD Hotkey mod I did this:

GLOBAL.TheInput:AddKeyDownHandler(GetModConfigData("key"), function()
	local wilson = GLOBAL.ThePlayer
	if wilson and wilson.HUD then
		if wilson.HUD:IsVisible() then
			wilson.HUD:Hide()
		else
			wilson.HUD:Show()
		end
	end
end)

This only works because HUD is purely client-side (i.e. it only affects the person pressing the key). For your case, a RPC or alike would be needed.

Kzisor's code seems to use RPC under the hood too. By copying from Kzisor's RPC Handler example and my Key Handler example, you should almost have everything needed. However, you'd also have to send the RPC in the Key Handler function. I cannot recall how that works.

To clarify, it would look something like this, I imagine:

GLOBAL.TheInput:AddKeyDownHandler(GetModConfigData("key"), function()
	local player_character = GLOBAL.ThePlayer
	if player_character ~= nil and player_character.prefab == "yourcharacter" then
		--TODO send RPC "yourhotkeyname" for player_character
	end
end)

AddModRPCHandler("yourmodname", "yourhotkeyname", fugg)
  • Like 1
Link to comment
Share on other sites

On 11/24/2020 at 3:27 AM, Mobbstar said:

In my HUD Hotkey mod I did this:


GLOBAL.TheInput:AddKeyDownHandler(GetModConfigData("key"), function()
	local wilson = GLOBAL.ThePlayer
	if wilson and wilson.HUD then
		if wilson.HUD:IsVisible() then
			wilson.HUD:Hide()
		else
			wilson.HUD:Show()
		end
	end
end)

This only works because HUD is purely client-side (i.e. it only affects the person pressing the key). For your case, a RPC or alike would be needed.

Kzisor's code seems to use RPC under the hood too. By copying from Kzisor's RPC Handler example and my Key Handler example, you should almost have everything needed. However, you'd also have to send the RPC in the Key Handler function. I cannot recall how that works.

To clarify, it would look something like this, I imagine:


GLOBAL.TheInput:AddKeyDownHandler(GetModConfigData("key"), function()
	local player_character = GLOBAL.ThePlayer
	if player_character ~= nil and player_character.prefab == "yourcharacter" then
		--TODO send RPC "yourhotkeyname" for player_character
	end
end)

AddModRPCHandler("yourmodname", "yourhotkeyname", fugg)

Are there any examples of RPC's bound to an item, like I'm trying to do? Or do I need to consolidate all of my abiltiy code into modmain to make it work properly? I've been trying to get this to work for a couple of days and keep running into problems, and I think I'm also missing a core RPC line as well.

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