|
Post by 06hypersonic60 on May 28, 2012 9:09:46 GMT -5
hey guyz. as the title says, I would like you to give me some tips to make a code that picks the nearest object. Cuz with only entityDistance and moveEntity doesn't make the player move to the neares object. have a nice day
|
|
|
Post by EightBitDragon on May 28, 2012 12:00:17 GMT -5
Stand-alone function:
Function ReturnNearestObject.tObject() ; Setup some local variables Local nObj.tObject Local edNearest# Local edDistance# ; Set the starting distance to 10,000 edNearest# = 10000 ; Loop through each player and each object For p.tPlayer = Each tPlayer For o.tObject = Each tObject ; Get the distance edDistance# = EntityDistance(o\Entity, p\Objects\Entity) ; Check if the distance is less than the current distance, and update the current object If edDistance# < edNearest# Then edNearest# = edDistance# nObj.tObject = o EndIf Next Next ; Once all the players and objects have been checked, nObj is left as the closest object Return nObj End Function
Use it like this:
no.tObject = ReturnNearestObject()
That's how to get the handle of the nearest object within 10,000 units. What you do with it now is up to you. Bear in mind, it returns all objects, so you should probably put a If-Then in there to get only certain types of objects. The following modification returns the nearest spring within 10,000 units:
Function ReturnNearestObject.tObject() ; Setup some local variables Local nObj.tObject Local edNearest# Local edDistance# ; Set the starting distance to 10,000 edNearest# = 10000 ; Loop through each player and each object For p.tPlayer = Each tPlayer For o.tObject = Each tObject ; Get the distance edDistance# = EntityDistance(o\Entity, p\Objects\Entity) ; Check if the distance is less than the current distance, and update the current object If edDistance# < edNearest# Then ; If its a spring, update the current object If o\ObjType = OBJTYPE_SPRING Then edNearest# = edDistance# nObj.tObject = o EndIf EndIf Next Next ; Once all the players and objects have been checked, nObj is left as the closest object Return nObj End Function
|
|
|
Post by 06hypersonic60 on May 28, 2012 13:28:35 GMT -5
k. thanks alot for that code. I got most of it but still one thing. What's should I use the know if the object is the nearest. I mean for example: If EntityIsNearest = true and p\Action = ACTION_Dash then .... end if what I can't find in this code is the variable to use.
|
|
|
Post by EightBitDragon on May 28, 2012 17:41:07 GMT -5
If you used the second bit of code, no.tObject = ReturnNearestObject(), then you can probably use this right after it:
If (EntityDistance(no\Entity, p\Objects\Entity)<50) And p\Action=ACTION_DASH Then ; Do some shit here relating to no.tObject PositionEntity p\Objects\Entity, EntityX#(no\Entity), EntityY#(no\Entity), EntityZ#(no\Entity) End If
That will do stuff if the nearest object returned is less than 50 units away, and the player is dashing. I put a extra check for distance so that you don't jump clear across the stage if the objects are in the far distance.
|
|