English Get object ID after spawning them

5 replies
Goto Page
To the start Previous 1 Next To the start
Up
AtomKuh
User
Offline Off
What would be the best way to get the dynamic object ID immediately after spawning an object?
29.03.20 12:20:21 pm
Up
DC
Admin
Offline Off
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
Code:
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.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
29.03.20 12:34:50 pm
Up
Masea
Super User
Offline Off
You can get the last element of the object table right after you spawn an object:
Code:
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.
Create your UI easy and fast: UI Framework | Go deeper into the darkness and test your bravery: file cs2d Outlast II Modification (28)
29.03.20 12:55:41 pm
Up
DC
Admin
Offline Off
@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).
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
29.03.20 01:26:39 pm
Up
MikuAuahDark
User
Offline Off
@user DC @user Masea or something like this

Code:
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.
file cs2d LuaJIT for Dedicated Server (11) JIT POWER! | Know your Lua errors! | Draw stuff at Pixelize Art --xx-- server!
To the start Previous 1 Next To the start