Posted: 4th Dec 2021 5:25
How would I get the ID of the object if I knew its coordinates? I checked the help documentation but there isn't a
+ Code Snippet
ObjectGetID()

function. Does anyone have any ideas?
Posted: 4th Dec 2021 5:56
when you are making the object,

make sprite or load object

What I do is

Thisplayer=whateveryouloaded

so lets say im making a sprite

for x=1 to 100
mysprite=createsprite(0)
thisspritenumber=x
inc x
inc thisspritenumber
next x

print(thisspritenumber)
Posted: 4th Dec 2021 10:39
As GCH said you should really be keeping track of your object ID's but I can see cases where you need the id from position...

For this... cast a ray to the object location, the ray hits it and returns an id

first pick a median point to start, I say go a few units above the object and cast the ray down, viola, you have the id returned from ObjectRayCast

Something like this should work, you may need to play with the distance according to your object sizes

+ Code Snippet
Function GetObjectIDAtLocation(x,y,z)
	
	Distance = 100
	ExitFunction ObjectRayCast(0, x, y-Distance, z, x, y, z)
	
EndFunction 0
Posted: 4th Dec 2021 19:41
I am keeping track of my object id's:
+ Code Snippet
function setupDefense()
	playerminioncount = 0
	for counter = 0 to 31
		playerdefense[counter].id = 0
		playerdefense[counter].x = 0
		playerdefense[counter].z = 0
	next counter
endfunction

function createDefense(cellz)
	playerdefense[playerdefensecounter].x = -3.4
	playerdefense[playerdefensecounter].id = CreateObjectSphere(1, 10, 10)
	SetObjectPosition(playerdefense[playerdefensecounter].id, playerdefense[playerdefensecounter].x, 2, 5 - cellz)
	SetObjectColor(playerdefense[playerdefensecounter].id, MINION_COLOR_RED, MINION_COLOR_GREEN, MINION_COLOR_BLUE, 100)
endfunction

The issue is i can't simply say tell me the id of the object at x, y, z...
Posted: 4th Dec 2021 20:31
Ok ok ok, no need to get "Defensive" lol [For clarification, that was a joke!]

tell me the id of the object at x, y, z


Things like this are what RayCasting was built for.. ... try my simple function
Posted: 4th Dec 2021 20:35
It may be that the objects center point is not within the mesh so you might want to use GetObjectMaxX/Y/Z() GetObjectMinX/Y/Z() to calculate the center of the object.
Also it might be a good idea to check each axis. I think the example might not work if the shape was a doughnut
Posted: 4th Dec 2021 20:48
It would not matter on the objects origin as it will register a hit on the mesh geometry not the origin point, but a doughnut, yes it would fail from above, cage it and check all axis + and -, the Min/Max functions would help there too.

Edit: Actually, the Min/Max are still no good as we don't yet know the object id, distance will have to suffice.
Posted: 4th Dec 2021 20:54
You say you know it's coordinates, so you could just loop through your array of objects and return the ID of the one with coordinates that match what you are looking for.
Posted: 4th Dec 2021 21:02
But that would rely on the origin point being at that location ...

something like this should work
+ Code Snippet
Function GetObjectIDAtLocation(x,y,z)
    
    hit_id = 0
    Distance = 100
    // check x
    hit_id = ObjectRayCast(0, x-Distance, y, z, x, y, z)
    if hit_id<>0 then ExitFunction hit_id
    
	hit_id = ObjectRayCast(0, x+Distance, y, z, x, y, z)
	if hit_id<>0 then ExitFunction hit_id

	// check y
	hit_id = ObjectRayCast(0, x, y-Distance, z, x, y, z)
	if hit_id<>0 then ExitFunction hit_id
	
	hit_id = ObjectRayCast(0, x, y+Distance, z, x, y, z)
	if hit_id<>0 then ExitFunction hit_id
	
	// check z
	hit_id = ObjectRayCast(0, x, y, z-Distance, x, y, z)
	if hit_id<>0 then ExitFunction hit_id

	hit_id = ObjectRayCast(0, x, y, z+Distance, x, y, z)
	if hit_id<>0 then ExitFunction hit_id
 
EndFunction 0
Posted: 4th Dec 2021 21:04
But that would rely on the origin point being at that location ...

It would, but that's how I read the question
Posted: 4th Dec 2021 21:12
Well I guess it depends on interpretation, I was taking it as the object is dynamic, a static object sure just loop the array as it will be where you placed its but if its on the move and its 1 unit out on either axis, its a fail.
Posted: 4th Dec 2021 21:14
Another solution would to be place a hidden object at the location and check for a collision and get the ID that way, personally I'd go with ray casting
Posted: 4th Dec 2021 21:21
dit: Actually, the Min/Max are still no good as we don't yet know the object id, distance will have to suffice.

Yes you are correct. I don't know what i was thinking
Posted: 4th Dec 2021 21:39
Well you had a good point about the doughnut, and made me hungry thinking about them! lol

Thought I'd better check my air code, indeed it works
+ Code Snippet
Function GetObjectIDAtLocation(x, y, z, Distance)
    
    hit_id = 0

    // check x
    hit_id = ObjectRayCast(0, x-Distance, y, z, x, y, z)
    if hit_id<>0 then ExitFunction hit_id
    
	hit_id = ObjectRayCast(0, x+Distance, y, z, x, y, z)
	if hit_id<>0 then ExitFunction hit_id

	// check y
	hit_id = ObjectRayCast(0, x, y-Distance, z, x, y, z)
	if hit_id<>0 then ExitFunction hit_id
	
	hit_id = ObjectRayCast(0, x, y+Distance, z, x, y, z)
	if hit_id<>0 then ExitFunction hit_id
	
	// check z
	hit_id = ObjectRayCast(0, x, y, z-Distance, x, y, z)
	if hit_id<>0 then ExitFunction hit_id

	hit_id = ObjectRayCast(0, x, y, z+Distance, x, y, z)
	if hit_id<>0 then ExitFunction hit_id
 
EndFunction 0

CreateObjectBox(999, 10, 10, 10)
SetObjectPosition(999, 124, 457, 865)

do 
	print(GetObjectIDAtLocation(124, 457, 865, 100))
	Sync()
loop