Forum

> > CS2D > Scripts > Get object ID after spawning them
Forums overviewCS2D overview Scripts overviewLog in to reply

English Get object ID after spawning them

5 replies
To the start Previous 1 Next To the start

old Re: Get object ID after spawning them

DC
Admin Off Offline

Quote
I'm afraid that this is not easily possible with the current command set because cs2d cmd spawnobject is a console command which can't return a value. I have to admit that this is a major flaw.

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
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 can replace ... with additional condtion(s) e.g. check the type etc. to make sure that you really get the right thing.

You could also use cs2d lua cmd 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.

old Re: Get object ID after spawning them

Masea
Super User Off Offline

Quote
You can get the last element of the object table right after you spawn an object:
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.

old Re: Get object ID after spawning them

DC
Admin Off Offline

Quote
@user Masea: Oh actually that should work yes.
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).

old Re: Get object ID after spawning them

MikuAuahDark
User Off Offline

Quote
@user DC @user Masea or something like this

1
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
Untested of course, but I'm sure both of you got the idea.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview