Jump to content


Photo

Instance Nearest Extended


  • Please log in to reply
74 replies to this topic

#1 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 19 March 2007 - 03:00 PM

Overview:
This extension provides some new instance_nearest functions that allow you to set conditions the instances must meet before being considered. For example, you could find the 4th nearest instance with health less than 30, or the 3rd farthest instance with myvarname greater than 2.

Extension Webpage

Edited by Kyle_Solo, 01 March 2010 - 06:22 AM.

  • 0

#2 fire_soul453

fire_soul453

    aarons video games

  • GMC Member
  • 542 posts

Posted 01 April 2007 - 02:13 AM

instance_nth_nearest(x,y,obj,n) - this one can be very useful thanks for making the extension keep up the good work
  • 0
Game maker Top sites
I'm sick of paypal. From now on I'm not buying indie games unless I can use something else...

#3 Mynameisonic

Mynameisonic

    GMC Member

  • New Member
  • 170 posts

Posted 03 April 2007 - 10:21 PM

I agree with Fire Soul, nth nearest and xth nearest are very useful (in fact I will probably use this or something like it in one of my games one of these days...). However, I don't quite understand the purpose of the dsd() function. Is it just to restore a sprite to its defaults more quickly than normal? Anyway, keep working on it, it's a good start!

Edited by Mynameisonic, 03 April 2007 - 10:22 PM.

  • 0
Posted Image

#4 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 03 April 2007 - 10:24 PM

EDIT: No longer relevant

Edited by Kyle_Solo, 15 October 2007 - 12:39 AM.

  • 0

#5 Bill23

Bill23

    GMC Member

  • GMC Member
  • 460 posts
  • Version:GM:Studio

Posted 14 October 2007 - 08:50 PM

Does instance_xth_nearest(x,y,obj,n,expression) use execute_string ?
  • 0
Posted Image
Posted ImagePosted Image

#6 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 15 October 2007 - 12:25 AM

Not anymore.
  • 0

#7 Flamer_Shaftglutton

Flamer_Shaftglutton

    GMC Member

  • GMC Member
  • 304 posts

Posted 17 October 2007 - 03:12 PM

So let me see if I understand: instance_xth_nearest() finds you the nth nearest whose variable is a certain value? That's what I'm assuming, which would be great.

I don't see how anyone could pass this up. This is a must have for almost all games!
  • 0

#8 The_Cookie_Monster

The_Cookie_Monster

    GMC Member

  • New Member
  • 217 posts

Posted 17 October 2007 - 05:07 PM

If only I saw this three days ago... Nice work!
  • 0

#9 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 17 October 2007 - 09:18 PM

So let me see if I understand: instance_xth_nearest() finds you the nth nearest whose variable is a certain value? That's what I'm assuming, which would be great.

I don't see how anyone could pass this up. This is a must have for almost all games!

That's exactly what it does, and it no longer uses execute_string (so no security hole anymore).

Glad you like it!
  • 0

#10 thegreatgaveno@mac.com

thegreatgaveno@mac.com

    GMC Member

  • GMC Member
  • 205 posts
  • Version:GM:Studio

Posted 21 October 2007 - 10:32 PM

I've been looking for something like this for a long time. Very helpful idea and I look forward to testing it out.
-Gavin
  • 0

#11 Licht

Licht

    GMC Member

  • New Member
  • 23 posts

Posted 21 October 2007 - 11:26 PM

Very nice, thanks a ton!
  • 0

#12 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 22 October 2007 - 03:20 PM

Update! New features:
Actually returns -1 when no instance is found (it didn't before).
Doesn't care if "variable" is not defined in some instances of "obj"
Faster, takes less memory
  • 0

#13 Cheppa

Cheppa

    GMC Member

  • GMC Member
  • 50 posts

Posted 23 October 2007 - 09:43 AM

WOW! This is very useful! Thank you very much!
  • 0

#14 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 24 October 2007 - 06:34 PM

Added instance_furthest functions.
  • 0

#15 Eva unit-01

Eva unit-01

    GMC Member

  • New Member
  • 771 posts

Posted 24 October 2007 - 09:15 PM

Ok so how does this work? i'm all good up until I hit that n thing and then i'm lost, what does that mean and how can these be used? Does it mean it finds the nearest instance of anything or what?

..little confused..
  • 0
My Deviant Art
Posted ImageOngoing manga/comic or whatever. Check it out.
Posted Image
Posted Image
((Surrounded)), W.I.P. and completion expected soon.
Gonna be one awesome TDS. Test and leave a comment. :]

#16 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 24 October 2007 - 09:31 PM

With instance_nth_nearest you can find the first closest (the closest, same as instance_nearest). Or you could find the second closest instance. Or the third closest. Or the nth closest instance.

With instance_xth_nearest you can find the nth closest instance of an obj (ex: obj_target) that has a variable (ex: health) equal to a value (ex: 100).

Does that clear it up?
  • 0

#17 Katuko

Katuko

    GMC Member

  • GMC Member
  • 5968 posts

Posted 25 October 2007 - 06:20 PM

This is very useful indeed. Good job. :GM008:
A question though: Does the instance_xth_nearest() accept for example '<100' as the value? I haven't got time to test it right now, but if it does, this will be even more useful. (AI attacking only players low on health, or those with a high score, or whatever.)

<(Katuko)>
  • 0
My other profiles: WordPress | deviantART | Steam

#18 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 25 October 2007 - 08:20 PM

It's in the works. Never fear, it will support at least these operators (and no execute_string!):
<,>,<=,>=,!=,=
  • 0

#19 Katuko

Katuko

    GMC Member

  • GMC Member
  • 5968 posts

Posted 25 October 2007 - 08:23 PM

That's great. Keep up the good work! :GM008:
  • 0
My other profiles: WordPress | deviantART | Steam

#20 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 25 October 2007 - 09:00 PM

Update! Now supports operators (optional argument)!
Added instance_xth_number!
  • 0

#21 Alex

Alex

    3lite Member

  • New Member
  • 3098 posts

Posted 26 October 2007 - 02:10 AM

Thanks very much, I was just about to create my own, but now I don't have to :GM008:
  • 0

#22 Katuko

Katuko

    GMC Member

  • GMC Member
  • 5968 posts

Posted 26 October 2007 - 02:22 PM

Thank you! Now that it's got operators it can't get any better! :D

<(Katuko)>
  • 0
My other profiles: WordPress | deviantART | Steam

#23 fogger

fogger

    Clockwerk Studios

  • GMC Member
  • 430 posts
  • Version:GM6

Posted 30 November 2007 - 01:02 AM

can I have the scripts themselves? I only have gm6.
  • 0

#24 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 30 November 2007 - 03:12 AM

I have to ask, why not use GM7? You can stop whenever you want (there is a GMK-> GM6 converter available), it runs on Vista, it supports extensions, some integer bug was fixed. Much better than 6.
  • 0

#25 armigus

armigus

    GMC Member

  • New Member
  • 94 posts

Posted 30 November 2007 - 01:50 PM

I'm looking for a function that handles more complicated expressions, say checking several variables at once:

instance_near_obj_script(obj,n,sref)
returns nth nearest object of type obj to the current object that satisfies condition defined by a script. If script is noone or does not return a value then true is assumed. In the script, the checked object is me and the calling object is other.

example:
Is_Clear(blk)
return (collision_line(x,y,other.x,other.y,blk,true,true)<0)

Gunner::Step
// Select nearest target that gunner can see
mTrg = instance_near_obj_script(Target,1,Is_Clear(Barrier
))

This particular example might have a solution using instance_xth_nearest(), but this idea allows for more complex scripts.

Note that the script works as if used in this code:

Gunner::Step
sighted = 0
with (Target) { if (Is_Clear(Barrier)) other.sighted+=1 }
  • 0

#26 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 30 November 2007 - 04:58 PM

Done! See first post. It doesn't support arguments, but you can get around that by setting global variables before calling it and having the script check those variables.

Edited by Kyle_Solo, 30 November 2007 - 05:05 PM.

  • 0

#27 armigus

armigus

    GMC Member

  • New Member
  • 94 posts

Posted 02 December 2007 - 01:21 AM

So the code should look like this:

global.objBlock = Barrier

Is_Clear()
return (collision_line(x,y,other.x,other.y,global.objBloc
k,true,true)<0)

Gunner::Step
// Select nearest target that gunner can see
mTrg = instance_xth_nearest(x, y, Target, 1, Is_Clear, true, =)

One thing: the script will automatically use the calling object's coordinates while the function could use any location. The x,y variables in the function might be superfluous and therefore a source of confusion to green coders.

The idea behind the script is to allow for several object variables to come into play. Say you have an AA gun and all enemy units inherit from Enemy but have air (travel = 2), ground (travel = 1), and naval (travel = 0) units. The script could then return -1 for an obscured object or travel for others.
  • 0

#28 fogger

fogger

    Clockwerk Studios

  • GMC Member
  • 430 posts
  • Version:GM6

Posted 02 December 2007 - 10:47 AM

Yeah I really like to use GM, but I can
only register GM6 because GM7 uses an online
system and can't cheat an online system, yet.

Nah just kidding:)

~Pr0n
  • 0

#29 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 02 December 2007 - 05:37 PM

Should be:
mTrg = instance_xth_nearest(x, y, Target, 1, Is_Clear, true, "=")

The x and y variables are never superfluous. The above script will find the nearest (n = 1) object to x,y that meets the condition. Even though you may not care, the instances are sorted by distance from x,y.

Edited by Kyle_Solo, 02 December 2007 - 05:40 PM.

  • 0

#30 Eva unit-01

Eva unit-01

    GMC Member

  • New Member
  • 771 posts

Posted 04 December 2007 - 07:39 PM

I have a little question on this, how could I use this to make a deathmatch sort of mode?
where every instance is looking for another instance but all instances are the same.
i've tryed but I get confused and it just messes up and they seem to be following themselves :(...so yea,
  • 0
My Deviant Art
Posted ImageOngoing manga/comic or whatever. Check it out.
Posted Image
Posted Image
((Surrounded)), W.I.P. and completion expected soon.
Gonna be one awesome TDS. Test and leave a comment. :]

#31 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 04 December 2007 - 11:04 PM

You could use instance_nth_nearest(x, y, object_index, 2). It would find the second closest object from this unit (meaning it finds the closest but not itself).
  • 0

#32 Consoft

Consoft

    GMC Member

  • GMC Member
  • 449 posts

Posted 06 December 2007 - 08:56 AM

I would like the .gm6 scripts for this. Why?

I'm running GMPhysics version 4. Doesn't work on gm7 sry.

Is it possible to hand out the scripts please?
  • 0
19 / Australia. [GML/Java/C++] Known as kkg, HeXeN and elimzke as well.
Make games in GML, compile them in C++! http://www.enigma-dev.org [Windows/Linux/Mac]

KUICS Australia PTY LTD

#33 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 07 December 2007 - 03:23 PM

PM me an email and I'll send you them (so long as you don't distribute them).
  • 0

#34 armigus

armigus

    GMC Member

  • New Member
  • 94 posts

Posted 07 December 2007 - 04:58 PM

I wondered if the operator was a string.

Remember that you are calling these functions from an object that already has a location (x,y). The function could just as easily use the location of the calling object in lieu of x,y. That's what I meant by the "superfluous" x,y.
  • 0

#35 OctoBot

OctoBot

    GMC Member

  • New Member
  • 26 posts

Posted 14 December 2007 - 01:53 AM

I have tried to approach this problem several ways, haven't succeeded.

I would love it if you explain the mathematical idea of it, perhaps you'd rather not to give code out, just how it works basically.

Also, you've got credits from me atleast for such a great piece of work!

Edited by OctoBot, 14 December 2007 - 02:11 AM.

  • 0

#36 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 14 December 2007 - 02:23 AM

It isn't really that hard. I put all instances of obj in a priority queue with distance as the priority. I then remove the nearest one n times and return the nearest one (which will be the nth closest one).

For the xth nearest, I only add the instances that meet the condition.

Simple.
  • 0

#37 OctoBot

OctoBot

    GMC Member

  • New Member
  • 26 posts

Posted 14 December 2007 - 02:43 PM

Haven't thought about the queue haha

Thanks man.
  • 0

#38 armigus

armigus

    GMC Member

  • New Member
  • 94 posts

Posted 27 December 2007 - 01:32 AM

Then instance_xth_number just returns ds_priority_size(queue)...
instance_n(x)th_nearest uses ds_priority_delete_min(queue), ds_priority_find_min(queue)
instance_n(x)th_furthest uses ds_priority_delete_max(queue), ds_priority_find_max(queue)

I now have a genuine need for these functions and have installed the gex. Testing will proceed shortly.
  • 0

#39 armigus

armigus

    GMC Member

  • New Member
  • 94 posts

Posted 11 January 2008 - 03:43 AM

Implementing the GEX as a script rather than a DLL is hampering performance. If a heat-seeking missile needs to hunt every step for all aircraft whose IFF != 1 with 40+ aircraft in the sky you will notice lag. With several missiles it gets bad quickly.
  • 0

#40 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 11 January 2008 - 03:54 AM

Well, I don't think a DLL would be much better. You'd still have to give the DLL all the positions of all the objects involved, then you'd have to give it the variables of all the objects involved. It would be slower if anything. To help solve your problem, have the missiles check every few steps instead of every step. I'll PM you.
  • 0

#41 armigus

armigus

    GMC Member

  • New Member
  • 94 posts

Posted 12 January 2008 - 07:33 PM

A better idea is to start with instance_nearest(), then test the condition. If the condition passes then the expensive instance_xth_nearest is unnecessary:
Target_Near()
if (not instance_exists(argument0)) return noone
var ans0,ans1;
ans0 = instance_nearest(x,y,argument0)
if (Target_Block(ans0)<0) return ans0
ans1 = instance_xth_nearest(x,y,argument0,1,Target_Block,0,"<")
if (ans1<0) then
  return ans0
else 
  return ans1

Target_Block()
return collision_line(x,y,other.x,other.y,Wall11,true,true)

That way I save the nasty function for when I have no other option.

If you consistently need the second, third, etc, object in line then this workaround fails.
  • 0

#42 GmFreaky

GmFreaky

    robson entertaiment

  • New Member
  • 249 posts

Posted 19 January 2008 - 08:17 PM

Where is the download link?
  • 0

#43 Zezuken

Zezuken

    AKA: Warriorccc0

  • GMC Member
  • 2799 posts
  • Version:Unknown

Posted 19 January 2008 - 08:25 PM

Where is the download link?

<{POST_SNAPBACK}>


On the page he provided..
  • 0

#44 GmFreaky

GmFreaky

    robson entertaiment

  • New Member
  • 249 posts

Posted 20 January 2008 - 09:09 AM

I already looked at it, but now i found it!
  • 0

#45 hotshotscott

hotshotscott

    GMC Member

  • New Member
  • 3483 posts

Posted 23 January 2008 - 07:04 AM

I wish this could be used with gm6 :blink: I could really use this, it would make things soo much easier! Especially the xth one. I need this :P :P :GM6:

Scotty
  • 0

TEAM ODDBOX

Did I try your game or help you out with yours?? Please return the favour and try out my game! NOT just another Halo Clone!!

Posted Image


#46 SquareWheel

SquareWheel

    Square Wheel Games

  • New Member
  • 851 posts
  • Version:Unknown

Posted 25 January 2008 - 09:55 AM

Did you write the scripts yourself, or just modify this script?
http://www.gmlscript...nce_nth_nearest
  • 0
Cook Console | Editor Engine | Mass File Namer

All of my examples are publicly available and can be found at

#47 Tratser

Tratser

    GMC Member

  • New Member
  • 69 posts

Posted 25 January 2008 - 08:31 PM

A DLL could solve this & many other problems very nicely...

Right now I'm building my most advanced AI ever, & it need to take more advanced conditions into account before attacking (like: if there are no blocks between AI & player).

This is my solution:
-create a list of players.
-eliminate players from the list that have blockages in-between AI & it (check if line collides with blocks).
-sort list by [distance to AI] + hp of player (& other important stats).
-select the player with highest priority!
-KILL!
:D

I could do this with GM.. But a DLL with these features could solve the problem so much easier & quicker:
-can create classes derived from earlier created classes.
-can add strings & floats to class.
-can add a list to a list to a list to a list... (hierarchical structuring).
-can sort list dependent upon a selected var of obj.
-can load lists from file.
-can load data-classes from file.
  • 0

#48 Kyle_Solo

Kyle_Solo

    GMC Member

  • GMC Member
  • 1071 posts
  • Version:Unknown

Posted 25 January 2008 - 09:29 PM

@SquareWheel: I wrote it myself, but they are pretty similar.

I posted the GML file on the website because people have been asking for it (as of this post, before this, the link was broken). Compare the two and see for yourself.
  • 0

#49 SquareWheel

SquareWheel

    Square Wheel Games

  • New Member
  • 851 posts
  • Version:Unknown

Posted 26 January 2008 - 12:21 AM

Ahh ok, thanks.
  • 0
Cook Console | Editor Engine | Mass File Namer

All of my examples are publicly available and can be found at

#50 kubaszewczyk11

kubaszewczyk11

    GMC Member

  • New Member
  • 100 posts

Posted 25 February 2008 - 03:09 PM

It's too hard for me!
Any tutorial?
I DON'T KNOW WHAT IS NTH AND XTH!
  • 0