Hi, I'm attempting to create a 'smart respawn' system for a chase scenario I'm working on. What this consists of is a small series of waypoints placed via the .prefab file in a few spots in the world. The system should, upon the wreck of the pursuing vehicle, find the closest respawn point, and place the chase vehicle at its position and orientation. Right now, I've got the system respawning the car through an old bit of repositioning code: Code: TorqueScript.eval('chaser.setTransform("214.025879 340.519287 6.9329319 0 0 1 3");') ...by choosing randomly off a list of the respawn points: Code: local spawnPoints = {'wp_a', 'wp_b', 'wp_c'} What I need to do yet is get the system to: Individually measure the distance between the player and the respawn points, Compile these float variables into a table, Get the shortest measurement from said table, and get that measurement's index, Use said index to choose the spawn point by name from the spawnPoints table, Use the spawn point's position (and, if possible, orientation) to respawn the car. I'm not really enough of a programmer to know how to put this all together, or to know of any shortcuts to the above process. I have a half-baked version of step 3: Code: local testTable = {13, 18, 12, 15, 25, 4, 17} for k, v in pairs(testTable) do nearest = math.min(unpack(testTable)) if v == nearest then return k end print(nearest) end ...but it's getting the value and not the index. If you have any tips or advice, to include the obligatory "actually learn how to program," leave it below.
I did pieces of code that could be thrown together to make something doing what you want, and i'm in the mood to code, so here we go (haven't checked the code in-game, I'll let to you the "fun" part of debugging) Code: local shortestDistance = 9999999 -- code only going to work if the distances are less than 9999999 units, you'll see later the utility (ugly solution, but it's the simplest and it should work) -- 1. Get the position of the vehicle local chaserVehicle = scenetree.findObject('chaser') local chaserVehiclePos = chaserVehicle:getPosition() -- 2. For each waypoint (assuming the name of the waypoint are 'wp1','wp2',...) local counter = 1 while counter <= numberOfWaypoints do --Don't forget : change numberOfWaypoints to the actual number of waypoints (or declare the variable at the beginning) -- 3. Find the position of the waypoint local waypoint = scenetree.findObject('wp_'..counter..) local waypointPos = waypoint:getPosition() -- 4. Calculate the distance between the waypoint (formula is squareRoot((Xb+Xa)²+(Yb-Ya)²+(Zb-Za)²)) distance = math.sqrt((waypointPos.x - chaserVehiclePos.x)*(waypointPos.x - chaserVehiclePos.x) + (waypointPos.y - chaserVehiclePos.y)*(waypointPos.y - chaserVehiclePos.y) + (waypointPos.z - chaserVehiclePos.z)*(waypointPos.z - chaserVehiclePos.z)) -- 5. Compare the distance to the shortest distance you found and keep only the name of the waypoint with the shortest distance (should even handle the case where multiple waypoints are at the same distance (really unlikely)) if distance < shortestDistance do shortestDistance = distance nearestWaypoint = 'wp_'..counter.. end counter = counter + 1 end -- 6. Here you're left with every waypoint distance to the vehicle checked, and only the shortest waypoint is memorised under the variable nearestWaypoint, only thing left is to spawn the player at the shortest waypoint -- Find the position of the nearest waypoint local waypoint = scenetree.findObject(nearestWaypoint) local waypointPos = waypoint:getPosition() -- Spawn the vehicle at this position TorqueScript.eval('chaser.setTransform(..waypointPos.."0 0 1 3");') -- Possible improvements to do if the rotation is different for each waypoint ? (search the corresponding rotation in a pre-made table ?) Hopefully there's enough comments, but feel free to ask me if it's still not clear. Also please keep me updated on how well this works !
I actually got this to work in the meantime. Via a hacky method, but working all the same: Code: local function setSpawn() if index <= #spawnPoints then spawnPoint = scenetree.findObject(spawnPoints[index]) --Get all the info. Object references, local player = scenetree.findObject('scenario_player0') local spawnDistance = (spawnPoint:getPosition() - player:getPosition()):len() --Distances, if nearest == nil then --And then iterate to find out if the first option wasn't the closest nearest = spawnDistance closest = spawnPoint.name elseif spawnDistance < nearest then nearest = spawnDistance closest = spawnPoint.name end index = index + 1 else nearest = scenetree.findObject(closest):getPosition() TorqueScript.eval('police1.setTransform("'..tostring(nearest.x)..' '..tostring(nearest.y)..' '..tostring(nearest.z)..' 0 0 1 3");') policeStopTimer = 0 index = 1 nearest = nil closest = nil end end It's fairly similar to yours, though it does work on waypoints that are 10million+ km away! Also, you don't need to write the full distance formula, Lua has vector length built in with (vector):len().
Hey, I don't wanna make a new post about this, but I'm having some trouble with triggers. I've got a couple of them positioned around the level and tied to a function. However, it seems as though the trigger's function can't get the vehicle ID of whatever hit it, and since the scenario has multiple vehicles, I kinda need that to keep the other car(s) from triggering the event. Code: function onTrigger(data) local playerVehicleId = be:getPlayerVehicleID(0) print(data.vehicleId) if data.vehicleId == playerVehicleId and chaserState ~= 1 then chaserState = 1 onChangeState = true end end The console says that data.vehicleId is nil. This same code functions properly on race waypoints. Edit: Sorry, figured it out.