Forum
CS2D Scripts Get object ID after spawning themGet object ID after spawning them
5 replies 1
So the best (and maybe only) approach would be the naive one: Iterate over all objects and find the one which matches your spawn configuration (this isn't 100% safe though and might lead to wrong results in some edge cases).
Something like this
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
local objectlist=object(0,"table") for _,id in pairs(objectlist) do 	if object(id, "x") == yourSpawnX and object(id, "y") == yourSpawnY and ... then 		'this is PROBABLY my object 		'do something here 		break 	end end
You could also use objectat for some object types. e.g. for buildings. If possible it is recommended to use that one because it's faster than iterating yourself.
local objectIDs = object(0, "table") return objectIDs[#objectIDs]
This will return you the ID of the object you spawned.
This trick can also be used when spawning items.
I was afraid that this is not always true because IDs are assigned dynamically and not necessarily in order.
so e.g. when you create 10 objects (IDs 1 - 10) and delete object with ID 5, then the next object you spawn will get ID 5 (because it's available again) and NOT ID 11.
But since CS2D internally stores all objects in a simple list - independent from their ID - new objects will (probably) always be added to the end of the list and therefore the newest entry will always be at the very end of the object table (which is based on the list and not the IDs).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function spawnobject(type, x, y, rot, mode, team, pl) 	rot = rot or 0 	mode = mode or 0 	pl = pl or 0 	team = team or (pl > 0 and player(pl, "team") or 0) 	local oldlist = object(0, "table") 	parse("spawnobject "..type.." "..x.." "..y.." "..rot.." "..mode.." "..team.." "..pl) 	for _, id in ipairs(object(0, "table")) do 		local found = false 		for _, v in ipairs(oldlist) do 			if v == id then 				found = true 				break 			end 		end 		if found == false then 			return id 		end 	end 	return 0 end
1