Jump to content

Collision models


dima3367

Recommended Posts

Hello!
I have a system for freeroam in which the machines models disappears collision. It seems to be working properly, but there is one problem, sometimes a conflict as if the car appears and face, I suspect that this is due to the fact that the other player off collision, and at first it appears. Just does not always work this system. For example, the player has pressed "disable conflict" and the other player's collision is still displayed.
Checkbox course made.

Could you check the code?
Client:


local ghostAvail = false
function toggleVehicleGhost()
	if guiCheckBoxGetSelected(getControl(wndMain, 'anticol')) then
		outputChatBox("Теперь ваш автомобиль не будет сталкиваться с другими автомобилями.",150,255,150)
		ghostAvail = true
		setElementData ( g_Me, "freeroam:ghost", true )
		local veh = getPedOccupiedVehicle ( g_Me )
		if veh and getPedOccupiedVehicleSeat (g_Me) == 0 then
			for i, v in ipairs ( getElementsByType ("vehicle", true) ) do
				if veh ~= v then
					setElementCollidableWith ( veh, v, false )
				end
			end
		end
	else
		outputChatBox("Теперь ваш автомобиль будет сталкиваться с другими автомобилями.",250,150,150)
		setElementData ( g_Me, "freeroam:ghost", false )
		ghostAvail = false
		local veh = getPedOccupiedVehicle ( g_Me )
		if veh and getPedOccupiedVehicleSeat (g_Me) == 0 then
			for i, v in ipairs ( getElementsByType ("vehicle", true) ) do
				if veh ~= v then
					local occupant = getVehicleOccupant (veh)
					if not occupant or not getElementData (occupant,"freeroam:ghost") then
						setElementCollidableWith ( veh, v, true )
					end
				end
			end
		end
	end
end

function onStreamIn ( )
	local veh = getPedOccupiedVehicle ( g_Me )
	if veh then
		if getElementType ( source ) == "vehicle" then
			local occupant = getVehicleOccupant (veh)
			if isElementCollidableWith ( veh, source ) then
				if ghostAvail or ( occupant and getElementData ( occupant, "freeroam:ghost" ) and getPedOccupiedVehicleSeat (occupant) == 0 ) then
					setElementCollidableWith ( veh, source, false )
				end
			else
				if not ghostAvail or ( occupant and not getElementData ( occupant, "freeroam:ghost" ) ) then
					setElementCollidableWith ( veh, source, true )
				end
			end
		end
	end
end
addEventHandler ( "onClientElementStreamIn", root, onStreamIn )

function checkPlayersGhosts (dataName)
	if getElementType ( source ) == "player" and dataName == "freeroam:ghost" and source ~= localPlayer then
		local veh = getPedOccupiedVehicle ( source )
		local localVeh = getPedOccupiedVehicle ( g_Me )
		if veh and getPedOccupiedVehicleSeat (source) == 0 and isElementStreamedIn (veh) then
			local newValue = getElementData ( source, dataName )
			if newValue then
				for i, v in ipairs ( getElementsByType ("vehicle", true) ) do
					if veh ~= v then
						setElementCollidableWith ( veh, v, false )
					end
				end
			else
				for i, v in ipairs ( getElementsByType ("vehicle", true) ) do
					if veh ~= localVeh then
						setElementCollidableWith ( veh, v, true )
					else
						if not ghostAvail then
							setElementCollidableWith ( veh, v, true )
						end
					end
				end
			end
		end
	end
end

addEventHandler ( "onClientElementDataChange", getRootElement(), checkPlayersGhosts )

addEventHandler("onClientVehicleEnter", getRootElement(),
    function(thePlayer, seat)
     --   if thePlayer ~= getLocalPlayer() then
            if seat == 0 then
				if getElementData (thePlayer,"freeroam:ghost") then
				--	local localVeh = getPedOccupiedVehicle ( g_Me )
				--	if localVeh and localVeh ~= source then
						for i, v in ipairs ( getElementsByType ("vehicle", true) ) do
							if source ~= v then
								setElementCollidableWith ( source, v, false )
							end
						end
			--		end
				end
			end
    --    end
    end
)

addEventHandler("onClientVehicleExit", getRootElement(),
    function(thePlayer, seat)
       -- if thePlayer ~= getLocalPlayer() then
            if seat == 0 then
			--	local localVeh = getPedOccupiedVehicle ( g_Me )
			--	if localVeh and localVeh ~= source then
				if getElementData (thePlayer,"freeroam:ghost") then 
					for i, v in ipairs ( getElementsByType ("vehicle", true) ) do
						if source ~= localVeh then
							setElementCollidableWith ( source, v, true )
						else
							if not ghostAvail then
								setElementCollidableWith ( source, v, true )
							end
						end
					end
				end
			end
      --  end
    end
)

Thanks in advance and I apologize for my bad English.

Link to comment

Didn't finish to read the code, but according to getElementsByType 

getElementsByType ("vehicle", true)--line 10
--should be
getElementsByType ("vehicle", root, true)

PS: this should also throw an error, did you make sure there aren't?

 

26 minutes ago, dima3367 said:

other player's collision is still displayed.

What do you mean with "displayed"? How can you display a collision?

Edited by LoPollo
Link to comment

To be honest, I'm not sure if it is there. Since I can not really understand exactly what caused the problem that the collision occurs periodically.

Quote

What do you mean with "displayed"? How can you display a collision?

Do not see, but as it turns out that they are facing and do not pass through each other. (Somewhere in the middle as if faced), but after a while everything becomes fine.

Link to comment

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...