OFPEC Forum
Editors Depot - Mission Editing and Scripting => OFP - Editing/Scripting General => Topic started by: Baddo on 17 Sep 2005, 22:12:24
-
Does anyone know a way to check a variable's type in scripts or in functions?
I would love to be able to do that. I tried to abuse some scripting commands and check what they return when they are given a variable of wrong type, but I always seem to get error messages to the screen and can't get checks trying different format combinations in condition lines to work.
This would make it possible to write for example functions which adapt to the input arguments they are given. A bit like function overloading in C++.
_return = false;
if ( _this == "object" ) then
{
hint "object";
...do lot's of object related stuff here...
_return = true;
}
else
{
if ( _this == "positionarray" ) then
{
hint "posarray";
...do lot's of position related stuff here, same purpose as for the object...
_return = true;
};
};
_return
Something like that, but how? Has someone achieved this in practice?
I could include a "switch" number like [1,player] call funky.sqf or [2,getpos player] call funky.sqf but that would not be good at all. Extra work for users and doesn't look good no matter from what angle I look at it.
-
I cant think straight but maybe start with 'oftype' or 'typeof' (cant member wot one it is) and the 'count' command?
-
From a thread of mine a while back, I stumbled rather inadvertantly on what i think is a way to do this. The basic principle is, in a function, if you perform an operation only designed for one data type on another data type within the bounds of a call statement, the statement crashes without throwing an error/warning and fails to return anything. I/we used this to make a checking function to see if an array-storing variable had been initialised, but it could be converted to do this.
Example code:
_unknown = _this select 0;
_isArray = _unknown call {(count _this == count _this)};
if (_isArray) then {... array related stuff}; // _isArray either equals true or scalar bool array...
_isObject = _unknown call {getPos _this; true};
if (_isObject) then {... object related stuff};
_isNumber = _unknown call {(_this*3 == _this*3)};
if (_isNumber) then {... number related stuff};
I havent tested the object or the number code, but I know that the array code works, and based on the same principle the other 2 should too. Give it a go, anyway :) I like the idea of operator overloading, as well as having type-safe functions.
EDIT: Paddy, the typeOf command only works on variables of type Object, that is, men, vehicles, bullets, houses and so on. It will complain with an error message at the top of flashpoint if you attempt to use it with any other type, like a number. Also, count only works on arrays, where it returns the number of elements. Again, it will cause errors if you try use it on for example a String.
-
if you are only distinguishing between objects and arrays, use this condition:
?isnull (_variable select 0):hint "_variable is an object"
you could also use:
_variable != _variable
because of some error or something with certain checks... i don't quite remember but I know GB said something about this a little while back
-
isnull _object only returns true when the object _object is a null-object. In all other cases it returns false, and when used with any other type of variable it throws an error.
_variable != _variable is basically the same thing as isnull _variable (since null objects arent equal to anything, including themselves), but with the added bonuses that it works when _variable is an: array (I *think*), a number, and object, a bool, a side, a group, or a string. However, this wouldnt give you any info as to what type of variable _variable is.
-
Bn880 made just such a function two years ago:
funGetType
PURPOSE: Determine the type of data stored or referenced by a variable.
SYNTAX: [data:Anything] call funGetType
RETURNS: Integer type of data
-1 case data does not exist (undefined)
0 case data was a Boolean value
1 case data was a null group
2 case data was a null object
3 case data was a Side
4 case data was a CharArray
5 case data was an Array
6 case data was a Number
7 case data was an Object
8 case data was String
9 case data was a Group
DESCRIPTION: The funGetType function takes 1 parameter in an array and is used to detect the type of data stored or referenced in the parameter.
EXAMPLES:
[grpNull] call funGetType => 1
[[1,2,[3]]] call funGetType => 5
[["a","b"]] call funGetType => 4
[garbageUndefined] call funGetType => -1
HISTORY: CoC bn880 and Dinger 14/11/2003
DEPENDENCIES: CoC_LIBNUMSYS, CoC_LIBNETWORK
It should be in CoC Network Services (http://www.website.thechainofcommand.net/coc_network_services.htm)
-
Thanks for the info. This helps me to achieve what I'm trying to do.
-
I have to admit I gave you some misinformation. The function call only doesn't produce an error when the type is either the correct one, or 'scalar bool array string 0xfcfffef'. So my method's only good for checking initialisation. Sorry about that.
-
No problem Fragorl - I haven't had the time to test these methods with good thought yet. But I already noticed about what you posted that it can't be used like I first thought.
Every piece of information helps.
:)