Jump to content

Code not working on caves server for some reason


Recommended Posts

Hello, like the title says I'm trying to make when a specific character gets into combat the player gets the vignette effect from the deserthat, So I was wondering if it's possible to do it via some code to enable/disable it.

Right now I'm using this code, but it's obviously not how it's supposed to be lol.

Spoiler

--Adding effect
local hat = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)
local eslot = hat.components.equippable.equipslot
	
if hat then
	hat:AddTag("goggles")
	inst.components.inventory:Unequip(EQUIPSLOTS.HEAD)
	inst.components.inventory:Equip(hat, EQUIPSLOTS.HEAD)
end

--Removing effect
local hat = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)
local eslot = hat.components.equippable.equipslot
	
if hat then
	hat:RemoveTag("goggles")
	inst.components.inventory:Unequip(EQUIPSLOTS.HEAD)
	inst.components.inventory:Equip(hat, EQUIPSLOTS.HEAD)
end

 

 

Edited by Warbucks
Link to comment
Share on other sites

So I figured out a way to do this, but now I'm confused again :wilson_dead:.

This code works in non-caves worlds

inst.components.playervision.gogglevision = true
inst:PushEvent("gogglevision", {enabled = inst.components.playervision.gogglevision})

but in caves worlds nothing happens :wilson_cry:!

Link to comment
Share on other sites

Seems hosting a local "cave" server messes stuff up as it detects you as a client and not a host. I got this issue with my char too where nothing I coded functions well when I'm considered a "client" but its all flawless without cave.

Edited by Hell-met
Link to comment
Share on other sites

17 hours ago, Ultroman said:

Is some of your code inside an ismastersim if-statement?

I don't think so? The code is played in local functions which activate from listen for events "onattackother", "attacked", "equip", and "unequip" inside masterpostinit.

Do I have to put the code inside an if ismastersim to make it work? Like this?

if TheWorld.ismastersim then
	inst.components.playervision.gogglevision = true
	inst:PushEvent("gogglevision", {enabled = inst.components.playervision.gogglevision})
end

I realized while testing on caves servers the code would work when I typed it into console and press ctrl then it becomes "local", but otherwise doesn't work. So this code only plays on clients or something, but I don't know if there's a way to make code play on the client side :wilson_dead:.

Thanks for your time and any help :wilson_laugh:!

Link to comment
Share on other sites

Well, if your code runs on both the client and the server right now, then I don't know why it isn't working. The snippet of code you posted would make the code only be run on the server (if ismastersim is true, then the code is being called on the server right now, and if ismastersim is false, then the code is being run on the client right now), so that would be the opposite of what you want (I think).

I don't really know how the goggles work. If it is an overlay effect, perhaps you can find some knowledge in this post about the sandstorm effect.

Link to comment
Share on other sites

Well I tested a code like this on caves server.

Spoiler

local function gogglevision_on(inst)
	if inst.should_have_gogglevision == true then
		inst:DoTaskInTime(0, function()
			if not TheWorld.ismastersim then
				print("hello")
				inst.components.playervision.gogglevision = true
				inst:PushEvent("gogglevision", {enabled = inst.components.playervision.gogglevision})
			end
		end)
	end
end


--masterpostinit
inst:ListenForEvent("equip", gogglevision_on)
inst:ListenForEvent("unequip", gogglevision_on)

 

"if not ismastersim then" doesn't seem to make code run on the client side or do anything :(.

 

Thanks for your help and sorry for being a bother! I think I'll just go back to applying the goggles tag to equipped hat for the effect instead because at least that works on caves servers :wilson_dead:.

Link to comment
Share on other sites

That's because your code itself does not affect to the client. When you host a caved-server, the server and the client will run on the same machine but It does not mean the equal. That is, you have to think separately for the client and the server.

inst.components.playervision.gogglevision = true

As you know, this one is to set if inst has the vision against the sandstorm. The server checks this value while the player is in storming desert.

However, this does not automatically means whether the client should show the goggle screen or not. Especially when you just set a sole value like this, rather than call a function/method to do something. Because functions can do multiple things in row that is related to the purpose of itself. So I just wanted to say it is recommended to use functions/methods. 

Unfortunately, the method to set that value does not affect to the client's visual effect which means you can't solve the issue with it. But you can take a look this thread.

I can't write the code right now for you though, this will help you.

Link to comment
Share on other sites

That's because your code itself does not affect the client. When you host a caved-server, the server and the client will run on the same machine, but It does not mean the equal. That is, you have to think separately for the client and the server.

inst.components.playervision.gogglevision = true

As you know, this one is to set if inst has the vision against the sandstorm. The server checks this value while the player is in a storming desert.

However, this does not automatically means whether the client should show the goggle screen or not. Especially when you just set a single value like this, rather than call a function/method to do something because functions can do multiple things in a row that is related to the purpose of itself. So I just wanted to say it is recommended to use functions/methods. 

Unfortunately, the method to set that value does not affect the client's visual effect which means you can't solve the issue with it. So you need to implicate an event to a client function that is to set the vignette effect. In this case, use netvar to trigger the event.

AddPrefabPostInit("player_classified", function(inst)
	inst.setgoggle = GLOBAL.net_bool(inst.GUID, "setgoggle", "setgoggledirty")
	inst.setgoggle:set(false)

	inst:DoTaskInTime(2 * GLOBAL.FRAMES, RegisterModNetListeners) 
	-- delay two more FRAMES to ensure the original NetListeners to run first.
end)

Defining a netvar in player_classified is common. player_classified is a kind of intermediator between server events and the client. When a netvar is changed, it raises an event to both the server and the client.
In this case, when inst.setgoggle:set(true) is called in the server, the event named "setgoggledirty" triggers on the client. It also triggers on the server but we don't use it.

local function RegisterModNetListeners(inst)
	if GLOBAL.TheWorld and GLOBAL.TheWorld.ismastersim then
	else
		PatchGoggleHUD(inst)
		inst:ListenForEvent("setgoggledirty", SetGoggleEffect)
	end
end

And you can define the event behaviors in RegisterModNetListeners. And I also patched the HUD in here.

local function SetGoggleEffect(inst)
	local var = inst.setgoggle:value()
	inst._parent.HUD.gogglesover.showother = var
	inst._parent.HUD.gogglesover:ToggleGoggles(var)
end

In the client(for dirtyevents), there's no data parameter directly. Get the data we want via :value() and use it

local function PatchGoggleHUD(inst)
	inst._parent.HUD.gogglesover.showother = false
	inst._parent.HUD.gogglesover.ToggleGoggles = function(self, show)
		if show then
			if not self.shown then
				self:Show()
				self:AddChild(self.storm_overlays):MoveToBack()
			end
		elseif not self.showother and not self.owner.replica.inventory:EquipHasTag("goggles") then
			if self.shown then
				self:Hide()
				self.storm_root:AddChild(self.storm_overlays)
			end
		end
	end
end

in here. This is the code to set the goggle vignette effect and I tweaked it. In result, the goggle screen won't hide unless inst.setgoggle is false. I don't really understand what gets into combat means so you can give me the base for it or just set inst.setgoggle(true or false) whenever you want.

Link to comment
Share on other sites

I'm very dumb even when the code is made for me I can't understand how to make it work lol

These are the functions inside character.lua which the screen is supposed to get the google fx

Spoiler

-- Disable goggle fx in this function
local function combat_music_off(inst)
	TheWorld:PushEvent("enabledynamicmusic", true)
	inst.SoundEmitter:KillSound("combat_music")
	inst.combatmusic_turnoff_task = nil
	inst:SetCameraDistance()
	inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "warbuck_war_bonus")
	
	if inst.war_screenshake_task ~= nil then
		inst.war_screenshake_task:Cancel()
		inst.war_screenshake_task = nil
	end
end

-- Goggle fx enable in this function
local function combat_music_on(inst, data)
	combat_music_reset_time_till_off(inst)
	if inst.combatmusic_turnoff_task == nil then
		inst.components.locomotor:SetExternalSpeedMultiplier(inst, "warbuck_war_bonus", 1.25)
		TheWorld:PushEvent("enabledynamicmusic", false)
		local _r = math.random(1,2)
		
			if _r == 1 then
				inst.SoundEmitter:PlaySound("epic/epic/shootin_n_lootin", "combat_music")--TheFocalPoint
			else
				inst.SoundEmitter:PlaySound("epic/epic/fiery_frolic", "combat_music")
			end
		--end
		
		war_screenshake(inst)
		inst.combatmusic_turnoff_task = inst:DoTaskInTime(10, combat_music_off)
		inst.war_screenshake_task = inst:DoPeriodicTask(.75, war_screenshake)
	end
end

 

Also, the code you so generously provided is it supposed to be put this way in modmain.lua?

Spoiler

local function PatchGoggleHUD(inst)
	inst._parent.HUD.gogglesover.showother = false
	inst._parent.HUD.gogglesover.ToggleGoggles = function(self, show)
		if show then
			if not self.shown then
				self:Show()
				self:AddChild(self.storm_overlays):MoveToBack()
			end
		elseif not self.showother and not self.owner.replica.inventory:EquipHasTag("goggles") then
			if self.shown then
				self:Hide()
				self.storm_root:AddChild(self.storm_overlays)
			end
		end
	end
end

local function SetGoggleEffect(inst)
	local var = inst.setgoggle:value()
	inst._parent.HUD.gogglesover.showother = var
	inst._parent.HUD.gogglesover:ToggleGoggles(var)
end

local function RegisterModNetListeners(inst)
	if G.TheWorld and G.TheWorld.ismastersim then
	else
		PatchGoggleHUD(inst)
		inst:ListenForEvent("setgoggledirty", SetGoggleEffect)
	end
end

AddPrefabPostInit("player_classified", function(inst)
	inst.setgoggle = G.net_bool(inst.GUID, "setgoggle", "setgoggledirty")
	inst.setgoggle:set(false)
	inst:DoTaskInTime(2 * G.FRAMES, RegisterModNetListeners)
	-- Delay two more FRAMES to ensure the original NetListeners to run first.
end)

 

Also, thank you so so much for your help :wilson_laugh:!!!

 

 

Thank you, thank you, thank you so much YakumoYukari for helping, this code was really important to character thank you so much for your help the code is perfect :wilson_laugh:!!!

Edited by Warbucks
Link to comment
Share on other sites

Sorry to be so annoying when you helped out so much xD, but the code seems to crash when a player prefab is spawned with console saying "attempt to index HUD nil" or something.

I tried adding a check for if the player has playercontroller, but it seemed to just make the code not work at all. You probably know better what to do and thanks so much for your help :wilson_laugh:!!!

Spoiler

local function RegisterModNetListeners(inst)
	if G.TheWorld and G.TheWorld.ismastersim then
	else
		inst:DoTaskInTime(0, function() --prevents code from crashing when spawn player prefab, but makes goggle fx code not work
			if inst.components.playercontroller then
				PatchGoggleHUD(inst)
				inst:ListenForEvent("setgoggledirty", SetGoggleEffect)
			end
		end)
	end
end

 

 

 

Also I realized that now the goggle fx doesn't appear on non-caves worlds but does on cave worlds lol! Do I have to use the old method of

inst.components.playervision.gogglevision = true

in addition to this code to make it show up on both types of servers? Thanks so much your help and sorry to be such a bother :wilson_tranquil:

Edited by Warbucks
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...