Gothem Posted April 22, 2008 Share Posted April 22, 2008 How can create that function? if someone can say me how or make it thx Link to comment
Borov Posted April 23, 2008 Share Posted April 23, 2008 getElementsbyType("player")->loop through the table->calculate the distance->if less than maxdis then return the player Link to comment
Gothem Posted April 23, 2008 Author Share Posted April 23, 2008 getElementsbyType("player")->loop through the table->calculate the distance->if less than maxdis then return the player but it will return more than 1 player i like only one player Link to comment
50p Posted April 23, 2008 Share Posted April 23, 2008 Then use a function table.sort(resultTable) and see which distance is the smallest. Link to comment
Gothem Posted April 23, 2008 Author Share Posted April 23, 2008 Then use a function table.sort(resultTable) and see which distance is the smallest. and how get the distance? im have that code but dont think it is good function getNearestPlayer(player, maxdis) local pos[6] local players = getElementsByType ( "player" ) local pos[0],pos[1],pos[2] = getElementPosition( player ) for theKey,thePlayer in ipairs(players) do local pos[3],pos[4],pos[5] = getElementPosition( thePlayer ) if (pos[0] >= (pos[3] - maxdis) && pos[0] <= (pos[3] + maxdis) && pos[1] >= (pos[4] - maxdis) && pos[1] <= (pos[4] + maxdis) && pos[2] >= (pos[5] - maxdis) && pos[2] <= (pos[5] + maxdis)) then local i ++ local j = thePlayer end end if(i > 1) then i = 0 maxdis - 1 getNearestPlayer(player,maxdis) else return j end end dont know where to apply the tabble.sort Link to comment
Cazomino05 Posted April 23, 2008 Share Posted April 23, 2008 uhh getDistanceBetweenPoints3D is better than that massive if statement... it does same thing then you can just check if its smaller than the max distance and smaller than the most recent minimum distance you store in a variable... if it isn't then set the players name to the variable and his distance then eventually you will end up with the smallest distance in one variable and his element in another could even use a table for storing the above data, just an example. Link to comment
tma Posted April 23, 2008 Share Posted April 23, 2008 I use something like: function tuFindNearestElement(to,elementType,maxDistance) local x,y,z = getElementPosition(to) local bestD = maxDistance + 1 local bv = nil for _,av in pairs(getElementsByType(elementType)) do if av ~= to then local vx,vy,vz = getElementPosition(av) local d = getDistanceBetweenPoints3D(x,y,z,vx,vy,vz) if d < bestD then bestD = d bv = av end end end return bv,bestD end You call it like: local vehicleClosest,distance = tuFindNearestElement(player,"vehicle",100) This finds the closest "vehicle" to the "player" within a distance of 100 units. Link to comment
Cazomino05 Posted April 23, 2008 Share Posted April 23, 2008 my idea > stolezorzted na jk Link to comment
Gothem Posted April 23, 2008 Author Share Posted April 23, 2008 thanks guys i will test what us saying ____ EDIT \_________________________________________________________ it send me an error in this line i++ full code: function getNearestPlayer(player, maxdis) local i = 0 local players = getElementsByType ( "player" ) local x,y,z = getElementPosition( player ) for theKey,thePlayer in ipairs(players) do local px,py,pz = getElementPosition( thePlayer ) local dis = getDistanceBetweenPoints3D ( x, y, z, px, py, pz ) if dis <= maxdis then i++ local j = thePlayer end end if(i > 1) then i = 0 maxdis - 1 getNearestPlayer(player,maxdis) else return j end end Link to comment
Brophy Posted April 24, 2008 Share Posted April 24, 2008 try replacing i++ with i = i + 1 Link to comment
tma Posted April 24, 2008 Share Posted April 24, 2008 it send me an error No offence but why don't you just use my code above ? It does what you want and works. In your code you've way more errors than you think: + Yeah, that i++ should be i=i+1 (as has been pointed out) + You have the same problem with maxdis on the following line + You return "j" but j was set locally in an "if block" so it won't have a value to return. + You don't need recursion to do this - what you're going to end up with if you get it working is a check through all players at one distance. If that fails, you're going to keep calling it with every-decreasing values for "maxdis". Think about how much processing you're going to require just to find the closest. You only need to loop through the elements once to find the closest. If you did use my code then to find the closest player to "player" you'd just use: local playerClosest,distance = tuFindNearestElement(player,"player",100) This would set playerClosest to the player element that is closest to the first parameter player - our target (or nil if no other player is found). Link to comment
Gothem Posted April 24, 2008 Author Share Posted April 24, 2008 it send me an error No offence but why don't you just use my code above ? It does what you want and works. In your code you've way more errors than you think: + Yeah, that i++ should be i=i+1 (as has been pointed out) + You have the same problem with maxdis on the following line + You return "j" but j was set locally in an "if block" so it won't have a value to return. reason down + You don't need recursion to do this - what you're going to end up with if you get it working is a check through all players at one distance. If that fails, you're going to keep calling it with every-decreasing values for "maxdis". Think about how much processing you're going to require just to find the closest. You only need to loop through the elements once to find the closest. If you did use my code then to find the closest player to "player" you'd just use: local playerClosest,distance = tuFindNearestElement(player,"player",100) This would set playerClosest to the player element that is closest to the first parameter player - our target (or nil if no other player is found). i like it return THE CLOSEST player the reason of i dont use your code is: i like return only 1 player Link to comment
50p Posted April 24, 2008 Share Posted April 24, 2008 In Lua if a function returns more than one value and you assign it to one variable only the first value will be assigned. You can use: local closestPlayer = tuFindNearestElement(player,"player",100) This will assign the closest player to the variable closestPlayer. The distance is also returned but it's not assigned as you use only 1 variable. Link to comment
tma Posted April 24, 2008 Share Posted April 24, 2008 the reason of i dont use your code is: i like return only 1 player But it does return only one player ? The two variables being assigned on function return are the closest element (player if you want, of any other element type) and the distance it is from the source player. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now