Jump to content
Sign in to follow this  
champ-1

Unique variable for object

Recommended Posts

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;

Both are equally bad. You can define code in a mission init which is downloaded by everyone at the start rather than send it as a variable.

Share this post


Link to post
Share on other sites

Ok this is working solution:

[color="#FF8040"]KK_fnc_netId [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
[color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#191970"][b]isMultiplayer[/b][/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]exitWith[/b][/color] [color="#8B3E2F"][b]{[/b][/color][color="#191970"][b]netId[/b][/color] [color="#000000"]_this[/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]private[/b][/color] [color="#7A7A7A"]"_netId"[/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_netId[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#000000"]_this[/color] [color="#191970"][b]getVariable[/b][/color] [color="#7A7A7A"]"_netId"[/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#191970"][b]isNil[/b][/color] [color="#7A7A7A"]"_netId"[/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]then[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
	KK_var_globID [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#191970"][b]missionNamespace[/b][/color] [color="#191970"][b]getVariable[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#7A7A7A"]"KK_var_globID"[/color][color="#8B3E2F"][b],[/b][/color] [color="#FF0000"]0[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b])[/b][/color] [color="#8B3E2F"][b]+[/b][/color] [color="#FF0000"]1[/color][color="#8B3E2F"][b];[/b][/color]
	[color="#1874CD"]_netId[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#191970"][b]format[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#7A7A7A"]"0:%1"[/color][color="#8B3E2F"][b],[/b][/color] KK_var_globID[color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b];[/b][/color]
	[color="#000000"]_this[/color] [color="#191970"][b]setVariable[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#7A7A7A"]"_netId"[/color][color="#8B3E2F"][b],[/b][/color] [color="#1874CD"]_netId[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]	
[color="#1874CD"]_netId[/color]
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color][/color]

Made with KK's SQF to BBCode Converter

will return object id in "number:number" format, both in MP and SP

Share this post


Link to post
Share on other sites
Ok this is working solution:

[color="#FF8040"]KK_fnc_netId [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
[color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#191970"][b]isMultiplayer[/b][/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]exitWith[/b][/color] [color="#8B3E2F"][b]{[/b][/color][color="#191970"][b]netId[/b][/color] [color="#000000"]_this[/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]private[/b][/color] [color="#7A7A7A"]"_netId"[/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_netId[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#000000"]_this[/color] [color="#191970"][b]getVariable[/b][/color] [color="#7A7A7A"]"_netId"[/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#191970"][b]isNil[/b][/color] [color="#7A7A7A"]"_netId"[/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]then[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
	KK_var_globID [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#191970"][b]missionNamespace[/b][/color] [color="#191970"][b]getVariable[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#7A7A7A"]"KK_var_globID"[/color][color="#8B3E2F"][b],[/b][/color] [color="#FF0000"]0[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b])[/b][/color] [color="#8B3E2F"][b]+[/b][/color] [color="#FF0000"]1[/color][color="#8B3E2F"][b];[/b][/color]
	[color="#1874CD"]_netId[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#191970"][b]format[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#7A7A7A"]"0:%1"[/color][color="#8B3E2F"][b],[/b][/color] KK_var_globID[color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b];[/b][/color]
	[color="#000000"]_this[/color] [color="#191970"][b]setVariable[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#7A7A7A"]"_netId"[/color][color="#8B3E2F"][b],[/b][/color] [color="#1874CD"]_netId[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]	
[color="#1874CD"]_netId[/color]
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color][/color]

Made with KK's SQF to BBCode Converter

will return object id in "number:number" format, both in MP and SP

Yeah, this is cool. Why do you use format "number:number"?

---------- Post added at 13:32 ---------- Previous post was at 13:24 ----------

I guess we need function to retrieve object from net ID, too :D

---------- Post added at 13:38 ---------- Previous post was at 13:32 ----------

Maybe it would be better to add objects themself to "KK_var_globID", instead of count ?

Share this post


Link to post
Share on other sites
Yeah, this is cool. Why do you use format "number:number"?

because this is how netId is

---------- Post added at 13:32 ---------- Previous post was at 13:24 ----------

I guess we need function to retrieve object from net ID, too :D

already ahead of ya :)

---------- Post added at 13:38 ---------- Previous post was at 13:32 ----------

Maybe it would be better to add objects themself to "KK_var_globID", instead of count ?

Observe!

[color="#FF8040"]KK_fnc_netId [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
[color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#191970"][b]isMultiplayer[/b][/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]exitWith[/b][/color] [color="#8B3E2F"][b]{[/b][/color][color="#191970"][b]netId[/b][/color] [color="#000000"]_this[/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#191970"][b]typeName[/b][/color] [color="#000000"]_this[/color] [color="#8B3E2F"][b]![/b][/color][color="#8B3E2F"][b]=[/b][/color] [color="#7A7A7A"]"OBJECT"[/color] [color="#8B3E2F"][b]|[/b][/color][color="#8B3E2F"][b]|[/b][/color] [color="#8B3E2F"][b]{[/b][/color][color="#191970"][b]isNull[/b][/color] [color="#000000"]_this[/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]exitWith[/b][/color] [color="#8B3E2F"][b]{[/b][/color][color="#7A7A7A"]""[/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#191970"][b]isNil[/b][/color] [color="#7A7A7A"]"KK_globIDs"[/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]then[/b][/color] [color="#8B3E2F"][b]{[/b][/color]KK_globIDs [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#FF0000"]0[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]private[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#7A7A7A"]"_i"[/color][color="#8B3E2F"][b],[/b][/color] [color="#7A7A7A"]"_cnt"[/color][color="#8B3E2F"][b],[/b][/color] [color="#7A7A7A"]"_netId"[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_i[/color] [color="#8B3E2F"][b]=[/b][/color] KK_globIDs [color="#191970"][b]find[/b][/color] [color="#000000"]_this[/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_i[/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"]_cnt[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b]([/b][/color]KK_globIDs [color="#191970"][b]select[/b][/color] [color="#FF0000"]0[/color][color="#8B3E2F"][b])[/b][/color] [color="#8B3E2F"][b]+[/b][/color] [color="#FF0000"]1[/color][color="#8B3E2F"][b];[/b][/color]
	KK_globIDs [color="#191970"][b]set[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#FF0000"]0[/color][color="#8B3E2F"][b],[/b][/color] [color="#1874CD"]_cnt[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b];[/b][/color]
	[color="#1874CD"]_netId[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#191970"][b]format[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#7A7A7A"]"0:%1"[/color][color="#8B3E2F"][b],[/b][/color] [color="#1874CD"]_cnt[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b];[/b][/color]
	KK_globIDs [color="#191970"][b]append[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#1874CD"]_netId[/color][color="#8B3E2F"][b],[/b][/color] [color="#000000"]_this[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b];[/b][/color]
	[color="#1874CD"]_netId[/color]
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
KK_globIDs [color="#191970"][b]select[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_i[/color] [color="#8B3E2F"][b]-[/b][/color] [color="#FF0000"]1[/color][color="#8B3E2F"][b])[/b][/color]
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]

[color="#006400"][i]//example[/i][/color]
[color="#191970"][b]hint[/b][/color] [color="#191970"][b]str[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#000000"]player[/color] [color="#191970"][b]call[/b][/color] KK_fnc_netId[color="#8B3E2F"][b])[/b][/color][color="#8B3E2F"][b];[/b][/color] 

KK_fnc_objectFromNetId [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
[color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#191970"][b]isMultiplayer[/b][/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]exitWith[/b][/color] [color="#8B3E2F"][b]{[/b][/color][color="#191970"][b]objectFromNetId[/b][/color] [color="#000000"]_this[/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#191970"][b]typeName[/b][/color] [color="#000000"]_this[/color] [color="#8B3E2F"][b]![/b][/color][color="#8B3E2F"][b]=[/b][/color] [color="#7A7A7A"]"STRING"[/color] [color="#8B3E2F"][b]|[/b][/color][color="#8B3E2F"][b]|[/b][/color] [color="#191970"][b]isNil[/b][/color] [color="#7A7A7A"]"KK_globIDs"[/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]exitWith[/b][/color] [color="#8B3E2F"][b]{[/b][/color][color="#000000"]objNull[/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]private[/b][/color] [color="#7A7A7A"]"_i"[/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_i[/color] [color="#8B3E2F"][b]=[/b][/color] KK_globIDs [color="#191970"][b]find[/b][/color] [color="#000000"]_this[/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_i[/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="#000000"]objNull[/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
KK_globIDs [color="#191970"][b]select[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_i[/color] [color="#8B3E2F"][b]+[/b][/color] [color="#FF0000"]1[/color][color="#8B3E2F"][b])[/b][/color]
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]

[color="#006400"][i]//example[/i][/color]
[color="#191970"][b]hint[/b][/color] [color="#191970"][b]str[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#7A7A7A"]"0:1"[/color] [color="#191970"][b]call[/b][/color] KK_fnc_objectFromNetId[color="#8B3E2F"][b])[/b][/color][color="#8B3E2F"][b];[/b][/color] [/color]

Made with KK's SQF to BBCode Converter

I also put this on BIKI

Edited by Killzone_Kid

Share this post


Link to post
Share on other sites

I suggest to use BIS_fnc_objectVar.

When object has a custom name from the editor, it will use it, otherwise a new one is assigned. The name is unique no matter where the function is called from and is preserved even after respawn.

Most of our missions and modules are using this function.

Examples:

_objVar = _object call BIS_fnc_objectVar;

_object = missionNamespace getVariable _objVar;

Edited by Moricky

Share this post


Link to post
Share on other sites
I suggest to use BIS_fnc_objectVar.

When object has a custom name from the editor, it will use it, otherwise a new one is assigned. The name is unique no matter where the function is called from and is preserved even after respawn.

Most of our missions and modules are using this function.

Examples:

_objVar = _object call BIS_fnc_objectVar;

_object = missionNamespace getVariable _objVar;

It doesn't work for community missions where millions random scripts from different authors running.

_vehicle1 = createVehicle [bla, bla, bla];
_vehicle1 setVehicleVarName "car";
_vehicle1 call BIS_fnc_objectVar;

_vehicle2 = createVehicle [bla, bla, bla];
_vehicle2 setVehicleVarName "car";
_vehicle2 call BIS_fnc_objectVar;

_vehicle1 call BIS_fnc_objectVar; // returns "car"
_vehicle2 call BIS_fnc_objectVar; // returns "car"

Not unique at all.

---------- Post added at 14:47 ---------- Previous post was at 14:37 ----------

Killzone_Kid, great job. But I like your first function better.

KK_fnc_netId = {
if (isMultiplayer) exitWith {netId _this};
if (typeName _this != "OBJECT") exitWith {""};
private "_netId";
_netId = _this getVariable "_netId";
if (isNil "_netId") then {
	(missionNamespace getVariable ["KK_var_globID", []]) pushBack _this;
	_netId = format ["0:%1", count KK_var_globID];
	_this setVariable ["_netId", _netId];
};	
_netId
};

KK_fnc_getObjectFromNetId = {
if (isMultiplayer) exitWith {getObjectFromNetId _this};
if (typeName _this != "STRING") exitWith {objNull};
private ["_obj"];
{
	if (_x getVariable ["_netId", ""] isEqualTo _this) exitWith {
		_obj = _x;
	};
} forEach (missionNamespace getVariable ["KK_var_globID", []]);
_obj
};

Wouldn't this be faster and all?

Edited by Champ-1

Share this post


Link to post
Share on other sites
Doubt this will work at all.

Yeah. :D

Second try:

KK_fnc_netId = {
if (isMultiplayer) exitWith {netId _this};
if (typeName _this != "OBJECT") exitWith {""};
private "_netId";
_netId = _this getVariable "_netId";
if (isNil "_netId") then {
	KK_var_globID = (missionNamespace getVariable ["KK_var_globID", []]);
	KK_var_globID  pushBack _this;
	_netId = format ["0:%1", count KK_var_globID];
	_this setVariable ["_netId", _netId];
};	
_netId
};

KK_fnc_getObjectFromNetId = {
if (isMultiplayer) exitWith {getObjectFromNetId _this};
if (typeName _this != "STRING") exitWith {objNull};
private ["_obj"];
{
	if (_x getVariable ["_netId", ""] isEqualTo _this) exitWith {
		_obj = _x;
	};
} forEach (missionNamespace getVariable ["KK_var_globID", []]);
_obj
};

Edited by Champ-1

Share this post


Link to post
Share on other sites
Yeah. :D

Second try:

KK_fnc_getObjectFromNetId = {
if (isMultiplayer) exitWith {getObjectFromNetId _this};
if (typeName _this != "STRING") exitWith {objNull};
private ["_obj"];
{
	if (_x getVariable ["_netId", ""] isEqualTo _this) exitWith {
		_obj = _x;
	};
} forEach (missionNamespace getVariable ["KK_var_globID", []]);
_obj
};

that would be slower than a turtle :)

Share this post


Link to post
Share on other sites
It doesn't work for community missions where millions random scripts from different authors running.

_vehicle1 = createVehicle [bla, bla, bla];
_vehicle1 setVehicleVarName "car";
_vehicle1 call BIS_fnc_objectVar;

_vehicle2 = createVehicle [bla, bla, bla];
_vehicle2 setVehicleVarName "car";
_vehicle2 call BIS_fnc_objectVar;

_vehicle1 call BIS_fnc_objectVar; // returns "car"
_vehicle2 call BIS_fnc_objectVar; // returns "car"

Not unique at all.

Don't call setVehicleVarName, the function will do it for you. The command is analogous to naming an object in the editor, so the function accepts it and won't try to assign a generic name.

Share this post


Link to post
Share on other sites
Don't call setVehicleVarName, the function will do it for you.
That's just example. What if some other random script calls setVehicleVarName?

---------- Post added at 15:02 ---------- Previous post was at 15:01 ----------

that would be slower than a turtle :)
God damn it :D

Share this post


Link to post
Share on other sites

When two different scripts are using one variable name for different purposes, they risk much more than this. That's why it's good practice to use variable prefixes.

However, I'll consider expanding BIS_fnc_objectVar with condition checking if custom var is already used and assigning a generic one if it does.

Share this post


Link to post
Share on other sites
That's just example. What if some other random script calls setVehicleVarName?

---------- Post added at 15:02 ---------- Previous post was at 15:01 ----------

God damn it :D

I did some performance testing. I don't have fast PC and it will depend on how many objects are stored in array (will have no array in MP so will be ultra fast) for SP this is:

xXpK23s.jpg

I'd say this is pretty decent.

Share this post


Link to post
Share on other sites
I did some performance testing. I don't have fast PC and it will depend on how many objects are stored in array (will have no array in MP so will be ultra fast) for SP this is:

I'd say this is pretty decent.

Ok, but I really don't know much about perfomance. For me it seems if there are less code it performs better. But I guess it's not always the case. :)

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  

×