Jump to content
Sign in to follow this  
champ-1

Unique variable for object

Recommended Posts

I want to create unique variable based on object's ID, but I don't know how to get them. There is a command for multiplayer that returns unique ID for objects and groups. But it doesn't work in singleplayer.

Empty objects always specified like this: "3fcbc100# 14: heli_light_01_f.p3d". There is some sort of uniqued (?) id at the beggining. But players, AI and manned vehicles doesn't return that id. Is there a way to get it? Or maybe there is some other way how I can generate unique ID based on object?

Share this post


Link to post
Share on other sites

You could simply generate unique IDs yourself and assign them to objects, using the setVariable command. The allMissionObjects command may prove useful for you in this situation.

It would, however, be useful to know why it is that you wish for, presumably all, objects to possess a unique ID?

Share this post


Link to post
Share on other sites

This "id" is memory address of the entity, it will be different each time you say load the game and as you noticed it is not returned for all entities when converting object to string. Do you need some textual presentation of said id to display in the mission? Because otherwise object itself is already unique and not equal to any other object scripting-wise.

Share this post


Link to post
Share on other sites
You could simply generate unique IDs yourself and assign them to objects, using the setVariable command.

But I'll have to check all IDs of all object I generated before and make sure my ID is unique. Not very practical.

It would, however, be useful to know why it is that you wish for, presumably all, objects to possess a unique ID?
Well it may be usefull in many situations. For example something as simple as creating map markers for certain object, it has to be unique if you create a lot of them.

---------- Post added at 17:19 ---------- Previous post was at 17:14 ----------

and as you noticed it is not returned for all entities when converting object to string
Yeah, I thought maybe there is way to retrieve it from all objects.
Do you need some textual presentation of said id to display in the mission?
I need some unique textual ID generated/retrived from given object.

Share this post


Link to post
Share on other sites
[/color]Yeah, I thought maybe there is way to retrieve it from all objects.

I need some unique textual ID generated/retrived from given object.

Try scripting approach then:

uniqueIdCounter = 0;

fn_getUniqueId = {
private "_id";
_id = _this getVariable ["unique_id", -1];
if(_id < 0) then {
	_id = uniqueIdCounter;
	uniqueIdCounter = uniqueIdCounter + 1;

	_this setVariable ["unique_id", _id];
};

_id
};

Call fn_getUniqueNumber on any object and it will assign it said unique id if it still didn't and return it. This code is no MP compatible but can be easily changed to be.

Share this post


Link to post
Share on other sites
Try scripting approach then:

uniqueIdCounter = 0;

fn_getUniqueId = {
private "_id";
_id = _this getVariable ["unique_id", -1];
if(_id < 0) then {
	_id = uniqueIdCounter;
	uniqueIdCounter = uniqueIdCounter + 1;

	_this setVariable ["unique_id", _id];
};

_id
};

Call fn_getUniqueNumber on any object and it will assign it said unique id if it still didn't and return it. This code is no MP compatible but can be easily changed to be.

Let's say two players call this script almost simultaneously in MP. First PC runs this line:

_id = uniqueIdCounter;

and second PC runs same line before first one was able to send "uniqueIdCounter" over network. This results in two objects having same ID. Right?

I know this is pretty unlikely scenario, but still.

Other than that, this would work great for local purposes. Thanks.

Edit:

I guess you could asign IDs on server in MP...

Edited by Champ-1

Share this post


Link to post
Share on other sites
Let's say two players call this script almost simultaneously in MP. First PC runs this line:

_id = uniqueIdCounter;

and second PC runs same line before first one was able to send "uniqueIdCounter" over network. This results in two objects having same ID. Right?

I know this is pretty unlikely scenario, but still.

Other than that, this would work great for local purposes. Thanks.

Edit:

I guess you could asign IDs on server in MP...

Yes as I said my code example is no suitable for multiplayer, only for singleplayer. Here is MP-compatible version. It has small possibility of having issues when two clients call it at once, withing same frame but it is very rare and unlikely.

Server:

uniqueIdCounter = 0;
publicVariable "uniqueIdCounter";

Client:

fn_getUniqueId = {
   private "_id";
   _id = _this getVariable ["unique_id", -1];
   if(_id < 0) then {
       _id = uniqueIdCounter;
       uniqueIdCounter = uniqueIdCounter + 1;
       publicVariable "uniqueIdCounter";

       _this setVariable ["unique_id", _id, true];
   };

   _id
}; 

Share this post


Link to post
Share on other sites

Why is this important? (ie. why do you need to have an object ID?)

What are you trying to do?

There is a better way than this

Share this post


Link to post
Share on other sites
I recently came across the netId command. It may help you with your problem. As is noted on the wiki page, the objectFromNetId command complements netId.

He linked that in the OP.

Share this post


Link to post
Share on other sites

Is this what you want?

[color="#FF8040"]KK_fnc_getObjectID [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
[color="#191970"][b]private[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#7A7A7A"]"_str"[/color][color="#8B3E2F"][b],[/b][/color] [color="#7A7A7A"]"_end"[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_str[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#191970"][b]str[/b][/color] [color="#000000"]_this[/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_end[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#1874CD"]_str[/color] [color="#191970"][b]find[/b][/color] [color="#7A7A7A"]":"[/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_end[/color] [color="#8B3E2F"][b]<[/b][/color] [color="#FF0000"]0[/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]exitWith[/b][/color] [color="#8B3E2F"][b]{[/b][/color][color="#1874CD"]_str[/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_str[/color] [color="#191970"][b]select[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#FF0000"]10[/color][color="#8B3E2F"][b],[/b][/color] [color="#1874CD"]_end[/color] [color="#8B3E2F"][b]-[/b][/color] [color="#FF0000"]10[/color][color="#8B3E2F"][b]][/b][/color]
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]hint[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#191970"][b]nearestBuilding[/b][/color] [color="#000000"]player[/color] [color="#191970"][b]call[/b][/color] KK_fnc_getObjectID[color="#8B3E2F"][b])[/b][/color][color="#8B3E2F"][b];[/b][/color][/color]

Made with KK's SQF to BBCode Converter

Share this post


Link to post
Share on other sites

XY problem? Why do you need a unique ID?

Share this post


Link to post
Share on other sites
Is this what you want?

[color="#FF8040"]KK_fnc_getObjectID [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
[color="#191970"][b]private[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#7A7A7A"]"_str"[/color][color="#8B3E2F"][b],[/b][/color] [color="#7A7A7A"]"_end"[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_str[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#191970"][b]str[/b][/color] [color="#000000"]_this[/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_end[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#1874CD"]_str[/color] [color="#191970"][b]find[/b][/color] [color="#7A7A7A"]":"[/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_end[/color] [color="#8B3E2F"][b]<[/b][/color] [color="#FF0000"]0[/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]exitWith[/b][/color] [color="#8B3E2F"][b]{[/b][/color][color="#1874CD"]_str[/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_str[/color] [color="#191970"][b]select[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#FF0000"]10[/color][color="#8B3E2F"][b],[/b][/color] [color="#1874CD"]_end[/color] [color="#8B3E2F"][b]-[/b][/color] [color="#FF0000"]10[/color][color="#8B3E2F"][b]][/b][/color]
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]hint[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#191970"][b]nearestBuilding[/b][/color] [color="#000000"]player[/color] [color="#191970"][b]call[/b][/color] KK_fnc_getObjectID[color="#8B3E2F"][b])[/b][/color][color="#8B3E2F"][b];[/b][/color][/color]

Made with KK's SQF to BBCode Converter

No not really. It's impossible to retrive this ID from manned vehicles.

Anyway, Sa-matra's solutions would work for me. Thanks.

Share this post


Link to post
Share on other sites

I thought you said you need this for SP? publicVariable is redundant in SP and if you do it in MP, netId is what you want. If you are thinking of making universal code that works for both SP and MP, it is totally different ball game, and I doubt publicVariable alone will be sufficient, you will have to make server manage your ids. That would complicate things.

Share this post


Link to post
Share on other sites
If you are thinking of making universal code that works for both SP and MP, it is totally different ball game, and I doubt publicVariable alone will be sufficient, you will have to make server manage your ids. That would complicate things.
Ofcourse I want something universal.

It doesn't seems all that complicated. Btw, is it really that network intensive making public variables with setVariable? I read that making persistent BIS_fnc_MP is not recomended at all. Is it similar to that?

Share this post


Link to post
Share on other sites

There still has been no explanation of why you are trying to do this. I remain convinced that there is a better and more useful system for you

Share this post


Link to post
Share on other sites
There still has been no explanation of why you are trying to do this. I remain convinced that there is a better and more useful system for you
I wrote on the first page I need this for marker system. Also I had some problems related to this at the time I created this thread, but I don't remeber what exactly I was doing. :D

But you are free to explain your method, maybe it will be useful for someone. :)

Also I was wondering about this thing called "hash", not really sure what's that. But I heard cool guys using it. And i'm not talking about drugs :D

Share this post


Link to post
Share on other sites
Ofcourse I want something universal.

It doesn't seems all that complicated. Btw, is it really that network intensive making public variables with setVariable? I read that making persistent BIS_fnc_MP is not recomended at all. Is it similar to that?

I do not recommend to use publicVariable in this way for unique ids, because you can end up with duplicate ids. The only way to be sure is to query the server and make it send you unique id. This is why I'm saying it could get complicated.

Share this post


Link to post
Share on other sites
I wrote on the first page I need this for marker system.

Quoted below:

I want to create unique variable based on object's ID, but I don't know how to get them. There is a command for multiplayer that returns unique ID for objects and groups. But it doesn't work in singleplayer.

Empty objects always specified like this: "3fcbc100# 14: heli_light_01_f.p3d". There is some sort of uniqued (?) id at the beggining. But players, AI and manned vehicles doesn't return that id. Is there a way to get it? Or maybe there is some other way how I can generate unique ID based on object?

I don't see "marker system" anywhere.

Share this post


Link to post
Share on other sites
I do not recommend to use publicVariable in this way for unique ids, because you can end up with duplicate ids. The only way to be sure is to query the server and make it send you unique id. This is why I'm saying it could get complicated.
What if I create ID on server and then assign it publicly to the object? Would that cause alot of lag?

Although I was asking in general about public setVariable. It's persistant same way as BIS_fnc_MP. Does it create game logic objects and attach variable to them like BIS_fnc_MP? Or is it more efficient?

---------- Post added at 01:51 ---------- Previous post was at 01:50 ----------

Well it may be usefull in many situations. For example something as simple as creating map markers for certain object, it has to be unique if you create a lot of them.

Here...

Share this post


Link to post
Share on other sites
Here...

Sorry, since I didn't read between the lines and making the assumption that your example was also your intention. But if you have a variable server side that gets incremented per each marker created it should be easy to also create a unique marker on the server (createMarker is effects global):

_mrk = createMarker format["MarkerID%1",globalMarkerIDIncrement];

globalMarkerIDIncrement = globalMarkerIDIncrement + 1;

Share this post


Link to post
Share on other sites

publicVariable is persistent, every new player will get it on joining. But unless you broadcast it again it should not create any traffic.

Share this post


Link to post
Share on other sites
publicVariable is persistent, every new player will get it on joining. But unless you broadcast it again it should not create any traffic.
So what's worse?

if (isServer) then {
gameLogic setVariable ["SOMECODE", {
	//alot of code here
}, true];
};
call (gameLogic getVariable ["SOMECODE", {true}]};

or

[{
//alot of code here
}, "BIS_fnc_spawn", true, true] call BIS_fnc_MP;

Share this post


Link to post
Share on other sites

There's no mention of marker systems anywhere

Anyway, I believe a public variable event handler system would be better suited for this. For example, create an empty array in init.sqf and any time a unit is spawned or created, simply PV it.

//In init.sqf
DE_allMissionUnits = [];

"DE_addUnit" addPublicVariableEventHandler DE_eventHandler;

DE_eventHandler = {
_data = _this select 1;

if (typeName (_data select 1) == "ARRAY") then
{
	_loops = (count (_data select 1)) - 1;
	for "_i" from 0 to _loops do
	{
		DE_allMissionUnits set [(_data select 0) + _i, (_data select 1) select _i];
	};
} else
{
	DE_allMissionUnits set [_data select 0, _data select 1];
};
};


//Spawn unit example - SINGLE UNIT
_obj = createVehicle [blah,blah,blah,blah,blah];
DE_addUnit = [count DE_allMissionUnits, _obj];
publicVariable "DE_addUnit";
[nil, DE_addUnit] call DE_eventHandler;
DE_addUnit = nil;

//Spawn unit example - MULTIPLE UNITS
DE_addUnit = [];
for "_i" from 0 to 4 do
{
_obj = createVehicle [blah,blah,blah,blah,blah];
DE_addUnit pushBack _obj;
};
DE_addUnit = [count DE_allMissionUnits, DE_addUnit];
publicVariable "DE_addUnit";
[nil, DE_addUnit] call DE_eventHandler;
DE_addUnit = nil;

This method is guaranteed to generate a unique "ID" for each unit, it generates a handy list of all units which you can use (DE_allMissionUnits - ARRAY), and it makes sure all connected clients have the same list. I'm not sure how allMissionObjects works, and if it can be different client-to-client, but it might work exactly the same way. Also, there is no support for JIP players

Edited by DreadedEntity
minor changes to the code

Share this post


Link to post
Share on other sites

DreadedEntity, I'm not very familiar with "addPublicVariableEventHandler", so I can't judge how good is your method. Seems kinda complicated, maybe that's just me.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×