[Resolution 800x600] Scripts: Algorithms provided

Hey, guys. I'm making a Metal Detecting minigame for my project, and I need some help making a script. I am a programmer, but I am not familiar with how ruby or RGSS works at all, so I am able to provide Algorithms. I just need some clever person to make it into working code for me.

  1. Class MetalDetecting
  2.  
  3.     static int minDistance
  4.  
  5.     method setDistance()
  6.         //Get Events with Comment "Hole" and place in array length n.
  7.  
  8.         int temp = 0
  9.         minDistance = 15
  10.  
  11.         for loop (i..n)
  12.             int tX = Math.abs(event[i].x - player.x)
  13.             int tY = Math.abs(event[i].y - player.y)
  14.             temp = Math.sqrt((tX^2)+(tY^2))
  15.             if (temp < minDistance)
  16.                 then minDistance = temp
  17.         end loop
  18.     end
  19.  
  20.     method getDistance()
  21.         return minDistance;
  22.     end
  23. end
Expand to see the code.


H'okay. So there it is. Basically, I will have events that have a comment on the top of the page, which is necessary because the pages change, you cannot detect after you've dug it up. Then, a paralell process will be calling setDistance method every frame or so, which finds out the minimum distance between all those events and the player, basically setting minDistance to the distance of the closest event to the player.

Then, a method getDistance returns that minDistance value. It's in its own method so that I can call it from a conditional fork from an event. It's also the reason why minDistance has to outlast the lifespan of just the setDistance method.

If someone could make this script for me, I would be much obliged. Thanks very much n.n
Well, there's not too much difference between your code and this one, but there are a few things I'm not sure if they're correct, I mean, I'm guessing for loop works like a for iterator in Ruby. I must admit I don't know what's the meaning of ^ in your code, but Math.abs and Math.sqrt do exist in Ruby as well.

Your code mentioned something about an array length n... You'd need to define what's the actual length or pass it as an argument in def set_distance...

BTW, event in my code is still nil so I'd need to check out some default scripts to get the actual variable you'd need to make your method work properly.

  1. class MetalDetecting
  2.   def set_distance
  3.     # Get Events with Comment "Hole" and place in array length n.
  4.     temp, min_distance = 0, 15
  5.     for i in 0..15 # Since n is still nil, we need to pass another arg...
  6.         tX = Math.abs(event[i].x - $game_player.x)
  7.         tY = Math.abs(event[i].y - $game_player.y)
  8.         temp = Math.sqrt((tX^2)+(tY^2))
  9.         min_distance = temp if temp < min_distance
  10.     end
  11.   end
  12.  
  13.   def get_distance; return min_distance end
  14. end
Expand to see the code.
Well, where I left that comment "Get Events with "Hole" and place in array" I was kinda hoping for that code to go there.

And anyway, it doesn't matter, this matter was resolved in another forum. Thanks anyway, though.
Well, if so, it wouldn't be a bad idea to post the solution here as well.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=71244