Jump to content

Getting the object from its ID


Go to solution Solved by Pizzicato,

Recommended Posts

Hey all,

I'm having a bit of a Senior Moment. I've got an object's ID number (in this case a Scenery Object) but I can't for the life of me remember how to use it to get the Object table.

Help an old guy out?

i7-7700K @ 4.9Ghz | 16Gb DDR4 @ 3200Mhz | MSI Z270 Gaming M7 | MSI GeForce GTX 1080ti Gaming X | Win 10 Home | Thrustmaster Warthog | MFG Crosswind pedals | Oculus Rift S

Link to comment
Share on other sites

12 minutes ago, Pizzicato said:

Hey all,

I'm having a bit of a Senior Moment. I've got an object's ID number (in this case a Scenery Object) but I can't for the life of me remember how to use it to get the Object table.

Help an old guy out?

Are you using a 3rd party library or native DCS Scripting Engine? Because everything in the engine is based of the name strings, not the id numbers. 

Link to comment
Share on other sites

I'm just using the Scripting Engine. Maybe I'm misremembering the ability to find an object by ID? (Now I'm getting into really Senior Moment territory 😀).

  • Like 1

i7-7700K @ 4.9Ghz | 16Gb DDR4 @ 3200Mhz | MSI Z270 Gaming M7 | MSI GeForce GTX 1080ti Gaming X | Win 10 Home | Thrustmaster Warthog | MFG Crosswind pedals | Oculus Rift S

Link to comment
Share on other sites

Actually, I've just found a different way to approach the issue I was having. I just wanted to be able to reference a bunch of buildings on the map without having to go the error-prone route of copy/pasting their Object IDs.

The smarter solution was just to use the Assign As feature of the mission editor to expose their IDs as Trigger Zone properties, and then iterate the Trigger Zones for the OBJECT ID property. That way I can just check those IDs against the event.target property of any S_EVENT_KILL event. 

Thanks for commenting anyway, Tempest. 👍

  • Like 1

i7-7700K @ 4.9Ghz | 16Gb DDR4 @ 3200Mhz | MSI Z270 Gaming M7 | MSI GeForce GTX 1080ti Gaming X | Win 10 Home | Thrustmaster Warthog | MFG Crosswind pedals | Oculus Rift S

Link to comment
Share on other sites

Posted (edited)
1 hour ago, Pizzicato said:

Actually, I've just found a different way to approach the issue I was having. I just wanted to be able to reference a bunch of buildings on the map without having to go the error-prone route of copy/pasting their Object IDs.

The smarter solution was just to use the Assign As feature of the mission editor to expose their IDs as Trigger Zone properties, and then iterate the Trigger Zones for the OBJECT ID property. That way I can just check those IDs against the event.target property of any S_EVENT_KILL event. 

Thanks for commenting anyway, Tempest. 👍

You're welcome, but doesn't that mean you have to know the ID's before you can do the scripting? Like, they're hard coded? Is there a better way so it's more dynamic or does your solution do everything you need? 

I know I had to adjust my Superscript because I thought ID's were easier to pass around, but it turns out you can't 'get' an object from it's ID (I did raise a bug/wishlist about this a few days ago) but only from its name string. Seems like both should be available to me, but I guess that the devs thought the only person who'd know the ID's was the mission creator and thus they'd also know the names.

However, if you scrape the mission file on mission start, you can find all the unitID's with their unit and group name strings and keep your own table of them (MIST/MOOSE do this but they call them a 'database', however really it's not a database at all, it's just another lua table).

If you went that approach then you could dynamically search your own tables and match things up; however, you're also then required to keep them up to date with births/deaths etc. 

Hope that helps some. 


Edited by TEMPEST.114
  • Like 1
Link to comment
Share on other sites

If you know the object id and it is a valid object, then you can access it directly by mimicking the object table. For instance:

local obj = {  ["_id"] = 13777728,}

if Object.isExist(obj) then
    env.info(Object.getTypeName(obj))
end

If I recall it isn't treated 100% the same as if you got the object from an event. It hasn't actually inherited any of the functions, so obj:getTypeName() would error while Object.getTypeName(obj) is fine. If its a unit you could use getName and then pass that value to Unit.getByName() to make it a full on object. 

 

A word of warning about map object ids. The id will change if the map gets updated and will now represent some other object or nothing at all. It would be wise to rely on checking the coordinate and object type to verify it is indeed the object you want. Obviously varies a bit on whether or not a map will likely get updates. You'd probably be fine to rely on the id number on older maps like NTTR while steering clear of that method on Kola. 

  • Thanks 2

The right man in the wrong place makes all the difference in the world.

Current Projects:  Grayflag ServerScripting Wiki

Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread)

 SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum

Link to comment
Share on other sites

That's an awesome tip. Thanks for that, Grimes.

My Use Cases are typically around map objects and - ironically - it is the Kola map that I'm working with at the moment. In terms of the IDs changing as the map gets updated, do you happen to know if the fields in the associated Assign As trigger volumes get automatically updated when the map changes? I'd hope that the number in the trigger zone's OBJECT ID field is created by reference not copy, but maybe not.  

i7-7700K @ 4.9Ghz | 16Gb DDR4 @ 3200Mhz | MSI Z270 Gaming M7 | MSI GeForce GTX 1080ti Gaming X | Win 10 Home | Thrustmaster Warthog | MFG Crosswind pedals | Oculus Rift S

Link to comment
Share on other sites

3 hours ago, Pizzicato said:

That's an awesome tip. Thanks for that, Grimes.

My Use Cases are typically around map objects and - ironically - it is the Kola map that I'm working with at the moment. In terms of the IDs changing as the map gets updated, do you happen to know if the fields in the associated Assign As trigger volumes get automatically updated when the map changes? I'd hope that the number in the trigger zone's OBJECT ID field is created by reference not copy, but maybe not.  

I’m guessing but I’d think it’s like the DO SCRIPT FILE - everything in the mission table is a snapshot at that time, so you’d need to refresh everything to pick up the new changes, but like I said, that’s a guess. 

  • Like 1
Link to comment
Share on other sites

13 minutes ago, TEMPEST.114 said:

I’m guessing but I’d think it’s like the DO SCRIPT FILE - everything in the mission table is a snapshot at that time, so you’d need to refresh everything to pick up the new changes, but like I said, that’s a guess. 

I'll keep an eye out when Orbx do their next/first Kola update. I think they said they're looking to deliver it next month, so I'll see whether or not it changes the IDs and breaks my scripts, and report back.

  • Thanks 1

i7-7700K @ 4.9Ghz | 16Gb DDR4 @ 3200Mhz | MSI Z270 Gaming M7 | MSI GeForce GTX 1080ti Gaming X | Win 10 Home | Thrustmaster Warthog | MFG Crosswind pedals | Oculus Rift S

Link to comment
Share on other sites

6 hours ago, Pizzicato said:

That's an awesome tip. Thanks for that, Grimes.

My Use Cases are typically around map objects and - ironically - it is the Kola map that I'm working with at the moment. In terms of the IDs changing as the map gets updated, do you happen to know if the fields in the associated Assign As trigger volumes get automatically updated when the map changes? I'd hope that the number in the trigger zone's OBJECT ID field is created by reference not copy, but maybe not.  

It doesn't get updated. The association with the trigger zone and the object ends the moment the zone gets created. I've seen a few instances where the object that I created trigger zones over has been moved slightly or outright deleted. 

  • Like 1
  • Thanks 1

The right man in the wrong place makes all the difference in the world.

Current Projects:  Grayflag ServerScripting Wiki

Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread)

 SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum

Link to comment
Share on other sites

Ugh. Well that sucks. Good to know, though. Thanks for the info, Grimes.

i7-7700K @ 4.9Ghz | 16Gb DDR4 @ 3200Mhz | MSI Z270 Gaming M7 | MSI GeForce GTX 1080ti Gaming X | Win 10 Home | Thrustmaster Warthog | MFG Crosswind pedals | Oculus Rift S

Link to comment
Share on other sites

  • Solution
Posted (edited)
20 hours ago, Grimes said:

If you know the object id and it is a valid object, then you can access it directly by mimicking the object table. For instance:

local obj = {  ["_id"] = 13777728,}

if Object.isExist(obj) then
    env.info(Object.getTypeName(obj))
end

 

Just for anyone else that's coming to this, the code snippet needs the underscore to be on the right of id, not the left, i.e.

local obj = {  ["id_"] = 13777728,}

 


Edited by Pizzicato
  • Thanks 1

i7-7700K @ 4.9Ghz | 16Gb DDR4 @ 3200Mhz | MSI Z270 Gaming M7 | MSI GeForce GTX 1080ti Gaming X | Win 10 Home | Thrustmaster Warthog | MFG Crosswind pedals | Oculus Rift S

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...