Jump to content

Interaction with transport


fairyoggy

Recommended Posts

Not exactly sure what your question is. But I do believe this topic could help you out: 

Check out 'myonlake's' reply on it. He draws a colshape on the players position and warps all vehicle within that range to his location.
You could use this code as well but instead of warping the vehicles, you could do other things with them like opening or closing the doors.

Is this what you were looking for?

  • Like 1
Link to comment
26 minutes ago, Bartje said:

Not exactly sure what your question is. But I do believe this topic could help you out: 

Check out 'myonlake's' reply on it. He draws a colshape on the players position and warps all vehicle within that range to his location.
You could use this code as well but instead of warping the vehicles, you could do other things with them like opening or closing the doors.

Is this what you were looking for?

@Bartje I think it can help, but I don’t understand how to fix it.

addCommandHandler("wv", 
function(player, cmd) 
     local element = "vehicle" 
     local radius = 3 
     local x, y, z = getElementPosition(player) 
     local colshape = createColSphere(tonumber(x), tonumber(y), tonumber(z), tonumber(radius)) 
     for index, value in ipairs(getElementsWithinColShape(colshape, element)) do 
		  setVehicleLocked ( element, true )
          outputChatBox("lock", player, 220, 220, 0) 
     end 
end) 

 

JvSTjHc6ePw.jpg

Link to comment
3 minutes ago, slapztea said:

@Bartje I think it can help, but I don’t understand how to fix it.

I think that Bartje has given you a really good script. Using the recommended script you can create a "remote car key" script where you lock all vehicles that are close to your player. Let me help you by fixing it:

addCommandHandler("wv", 
function(player, cmd) 
     local radius = 3 
     local x, y, z = getElementPosition(player) 
     local colshape = createColSphere(tonumber(x), tonumber(y), tonumber(z), tonumber(radius)) 
     for index, value in ipairs(getElementsWithinColShape(colshape, "vehicle")) do 
		  setVehicleLocked ( value, true )
          outputChatBox("lock", player, 220, 220, 0) 
     end 
end) 

 

  • Like 1
Link to comment
7 minutes ago, The_GTA said:

I think that Bartje has given you a really good script. Using the recommended script you can create a "remote car key" script where you lock all vehicles that are close to your player. Let me help you by fixing it:


addCommandHandler("wv", 
function(player, cmd) 
     local radius = 3 
     local x, y, z = getElementPosition(player) 
     local colshape = createColSphere(tonumber(x), tonumber(y), tonumber(z), tonumber(radius)) 
     for index, value in ipairs(getElementsWithinColShape(colshape, "vehicle")) do 
		  setVehicleLocked ( value, true )
          outputChatBox("lock", player, 220, 220, 0) 
     end 
end) 

 

@The_GTA Okay, but if there are 2 cars nearby, and you need to close only one of them

Link to comment
Just now, slapztea said:

@The_GTA Okay, but if there are 2 cars nearby, and you need to close only one of them

How do you want to decide which car they player "needs" or wants to close? Does the player have a list of cars that he owns?

  • Like 1
Link to comment
7 minutes ago, The_GTA said:

How do you want to decide which car they player "needs" or wants to close? Does the player have a list of cars that he owns?

I made a parameter for each cars "uid"

For example, we have

setElementData(car,"uid",0) -- car1
setElementData(car,"uid",1) -- car2

How can I open / close, for example, a car with "uid" 0

Link to comment
4 minutes ago, slapztea said:

How can I open / close, for example, a car with "uid" 0

addCommandHandler("wv", 
function(player, cmd) 
    local radius = 3 
    local x, y, z = getElementPosition(player) 
    local colshape = createColSphere(tonumber(x), tonumber(y), tonumber(z), tonumber(radius)) 
    for index, value in ipairs(getElementsWithinColShape(colshape, "vehicle")) do
      if (getElementData(value, "uid") == 0) then
        setVehicleLocked ( value, true )
        outputChatBox("lock", player, 220, 220, 0) 
        break -- only one car.
      end
    end 
end) 

The line that I added is line 7. If you have a way to get the real "uid" by player then please modify that if-condition. ;) 

Edited by The_GTA
  • Like 1
Link to comment
11 minutes ago, The_GTA said:

addCommandHandler("wv", 
function(player, cmd) 
    local radius = 3 
    local x, y, z = getElementPosition(player) 
    local colshape = createColSphere(tonumber(x), tonumber(y), tonumber(z), tonumber(radius)) 
    for index, value in ipairs(getElementsWithinColShape(colshape, "vehicle")) do
      if (getElementData(value, "uid") == 0) then
        setVehicleLocked ( value, true )
        outputChatBox("lock", player, 220, 220, 0) 
        break -- only one car.
      end
    end 
end) 

The line that I added is line 7. If you have a way to get the real "uid" by player then please modify that if-condition. ;) 

very nice, everything works perfect, thank you very much!

Link to comment
  • Scripting Moderators
1 hour ago, slapztea said:

@The_GTA Hi again. One more question about this code. If there are no cars in the radius, how can I check this? For example, if there are no cars in the radius, a chat message is displayed

function lockCar(player, cmd)
	local radius = 3
	local x, y, z = getElementPosition(player)
	local vehiclesAround = getElementsWithinRange(x, y, z, radius, "vehicle")
	local vehicleElement = false

	if #vehiclesAround == 0 then
		outputChatBox("no vehicles around", player, 220, 220, 0)
		return
	end

	for i = 1, #vehiclesAround do
		vehicleElement = vehiclesAround[i]

		if getElementData(vehicleElement, "uid") and getElementData(vehicleElement, "uid") == 0 then
			setVehicleLocked(vehicleElement, true)
			outputChatBox("lock", player, 220, 220, 0)
			break
		end
	end
end
addCommandHandler("wv", lockCar)

Here you go, i also changed loop to faster, and we don't need colshape to get vehicles around. (+problem is that, that you create colshape and never delete it)

Edited by majqq
  • Like 2
Link to comment
18 minutes ago, majqq said:

function lockCar(player, cmd)
	local radius = 3
	local x, y, z = getElementPosition(player)
	local vehiclesAround = getElementsWithinRange(x, y, z, radius, "vehicle")
	local vehicleElement = false

	if #vehiclesAround == 0 then
		outputChatBox("no vehicles around", player, 220, 220, 0)
		return
	end

	for i = 1, #vehiclesAround do
		vehicleElement = vehiclesAround[i]

		if getElementData(vehicleElement, "uid") and getElementData(vehicleElement, "uid") == 0 then
			setVehicleLocked(vehicleElement, true)
			outputChatBox("lock", player, 220, 220, 0)
			break
		end
	end
end
addCommandHandler("wv", lockCar)

Here you go, i also changed loop to faster, and we don't need colshape to get vehicles around. (+problem is that, that you create colshape and never delete it)

Everything works as it should, thanks.

But I would like to know the nuances about the past code, what is wrong. I added delete colshape but did not post here.

Link to comment
  • Scripting Moderators
4 minutes ago, slapztea said:

Everything works as it should, thanks.

But I would like to know the nuances about the past code, what is wrong. I added delete colshape but did not post here.

local colshape = createColSphere(tonumber(x), tonumber(y), tonumber(z), tonumber(radius)) 

If you create colshape, and not delete it it will stay on map until resource restart, wanna see? After using your command follow steps below.

Execute this command in admin panel (Resources > Execute command)

setDevelopmentMode(true)

for client & server.

And then, simply write:

/showcol

in chat.

I've added additional element data check, so if vehicle doesn't have such element data, it wouldn't compare number with boolean. Besides, my version of code is somewhat faster, because of integer loop (you can't really see difference, but it's better to use it anyways)xgxWbv6.png

Source: https://springrts.com/wiki/Lua_Performance

Why after all people use ipairs without sensible reason? I don't know, maybe because some of them lazy, or they don't even know that you can use int loop.

  • Like 1
Link to comment
55 minutes ago, majqq said:

local colshape = createColSphere(tonumber(x), tonumber(y), tonumber(z), tonumber(radius)) 

If you create colshape, and not delete it it will stay on map until resource restart, wanna see? After using your command follow steps below.

Execute this command in admin panel (Resources > Execute command)


setDevelopmentMode(true)

for client & server.

And then, simply write:


/showcol

in chat.

I've added additional element data check, so if vehicle doesn't have such element data, it wouldn't compare number with boolean. Besides, my version of code is somewhat faster, because of integer loop (you can't really see difference, but it's better to use it anyways)xgxWbv6.png

Source: https://springrts.com/wiki/Lua_Performance

Why after all people use ipairs without sensible reason? I don't know, maybe because some of them lazy, or they don't even know that you can use int loop.

Good points, majqq! Let me comment on your details.

Indeed the integer loop is 100% equivalent to ipairs. Under the hood ipairs calls a function for each iteration to receive the next item while the integer loop is pure Lua bytecode. One could ask why did the Lua developer not optimize ipairs in the compiler to be a simple integer loop? Clearly he did not care about performance. So I argument that Lua was not designed to be performant. But don't get me wrong: due to per-frame event handlers you clearly need good performance or else you could destroy somebody's gameplay experience with bad scripts!

I think it has been a good idea to replace the colshape with the getElementsWithinRange function. Since OP did not ask about optimizations I was sticking to the script for simplicity's sake. Thank you for pointing it out because OP seems to like your improvement. :) 

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