Jump to content

Recommended Posts

Hello. I am writing a mod and I need to determine the coordinates of the portal or the distance to it when the event is triggered. I also don't understand how to search for objects after the world is generated.

		local function onGiveItemfn(inst2, data)   -- "itemget" only work in player inventory. 
			-- here need distance to the multiplayer portal
		end
		inst:ListenForEvent("itemget", onGiveItemfn)
		inst:ListenForEvent("equip", onGiveItemfn)

Thanks for the help

I wrote a similar function. I don't understand what the problem is. by condition, return is triggered, but the player is also teleported.

		local function onGiveItemfn(inst2, data)   -- "itemget" only work in player inventory. 
			local x,y,z = inst2.Transform:GetWorldPosition()
			print('onGiveItemfn')
			print(x,y,z)
			local mp = _G.TheSim:FindEntities(x, y, z, 15, {"multiplayer_portal"})
			print('check team', team, #mp, data.item.prefab) -- [00:21:19]: check team	nil	0	blue_mushroomhat
			if #mp == 0 or team then return end
			if lord_options.tp_item==data.item.prefab then team = 'lord' end
			if king_options.tp_item==data.item.prefab then team = 'king' end
			print('selected team', team) -- not print
			print('onGiveItemfn') -- not print
			q.SaveOption(inst2,'team',team)
			inst2:AddTag(team)
			local spawn = (team == 'king' and king_options.spawn_crd) or lord_options.spawn_crd
			inst2.Transform:SetPosition(spawn.x,spawn.y,spawn.z) -- this fire
		end
		inst:ListenForEvent("gotnewitem", onGiveItemfn)

 

To answer your original question, I would add a AddPrefabPostInit to the multiplayer_portal which saves it's position in TheWorld or a TUNING value. You will need to delay it a bit as during loading the position will always be (0,0,0) (not sure if 1 frame is enough, you will need to test that).

Then you can just use inst:GetDistanceSqToPoint(x,y,z) and enter the saved position of your multiplayer_portal.

As for the teleportation firing, that seems really strange, you could try to change it so that everything that happens after the check is in an if case, so instead of returning, you just don't let it run this piece of code.

I made delay 100mc for teleportation and now working normal. but i not know whay here trable... how function triggerin two time, first time without function print, second time after teleport and with checking position. i wrote -

		local function onGiveItemfn(inst2, data)   -- "itemget" only work in player inventory. 
			local x,y,z = inst2.Transform:GetWorldPosition()
			print('onGiveItemfn')
			print(x,y,z)
			local mp = _G.TheSim:FindEntities(x, y, z, 15, {"multiplayer_portal"})
			print('check team', team, #mp, data.item.prefab)
			if #mp == 0 or team then 
				do return end
			end
			if lord_options.tp_item==data.item.prefab then team = 'lord' end
			if king_options.tp_item==data.item.prefab then team = 'king' end
			print('selected team', team)
			print('onGiveItemfn')
			q.SaveOption(inst2,'team',team)
			inst2:AddTag(team)
			local spawn = (team == 'king' and king_options.spawn_crd) or lord_options.spawn_crd
			inst2:DoTaskInTime(0.1, function() --addition daley and now working
				inst2.Transform:SetPosition(spawn.x,spawn.y,spawn.z) 
			end)
		end
		inst:ListenForEvent("gotnewitem", onGiveItemfn)

 

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
×
  • Create New...