Jump to content
Sign in to follow this  
NGagedFX

CreateMarker Script - Defining default values for variables

Recommended Posts

Hello there, I'm getting more and more into scripting, and as a somewhat functional script - but more importandly as a personal tutorial - I'm writing this 'CreateMarker' script.

How to call:

nul = [position, "name", "type", "text", "colour", size, time, gps_object] execVM "scripts\CreateMarker.sqf"

Description of Variables:

position: array - getPos object or [x,y] - default = getPos this
name: string - name
type: cfgMarker - the actual icon (BISWiki > cfgMarkers) - default = empty
text: string - text visible on map - default = none
colour: string - ColorBlack, ColorBlue, ColorRed, ColorGreen, ColorYellow, ColorOrange, ColorWhite - default = ColorBlack
size: number - default = 1
time: number - time in seconds before marker gets deleted - default = 0 (infinite)
gps_object: name - object the marker should track - default = none
example: nul = [getPos unit1, "marker_unit1", "hd_objective", "Unit 1 is over here!", "ColorRed", 1, 20, unit1]

CreateMarker.sqf

_markerPos = _this select 0;
_markerName = _this select 1;
_markerType = _this select 2;
_markerText = _this select 3;
_markerColor = _this select 4;
_markerSize = _this select 5;
_markerTime = _this select 6;
_markerGpsObject = _this select 7;

_markerName = createmarker [_markerName, _markerPos];
_markerName setMarkerType _markerType;
_markerName setMarkerText _markerText;
_markerName setMarkerColor _markerColor;
_markerName setMarkerSize [_markerSize,_markerSize];

//_markerTime and _markerGpsObject are working fine so I haven't included them in this post (it's a lot of text atm :P)

If I fill in all the fields correctly, this script does exactly what I want it to. However, as with every script I've used up until now, you always had the choice to not assign some variables by cutting off the exec string or by using "" or not filling in anything. For example:

1 - nul = [getPos unit1, "", "", "Unit 1 is over here!", "", , , unit1]

2 - nul = [getPos unit1, "marker_unit1"]

As seen in 1, "" in case of a string or [space] in case of a number, or as seen in 2 where the last part is simply cut off, it should use the default values defined by the script. However, I've had major problems defining these.

if (_markerType == "") then {hint "true"}; this worked in case 1

if (isNil "_markerType") then {hint "true"}; this worked in case 2

But as you don't know how someone is gonna use your script:

if ((isNil "_markerType") || (_markerType == "")) then {hint "true"}; which didn't work at all and even gave me errors :confused:

So how do I define these default variables? :confused:

Share this post


Link to post
Share on other sites

try this

if ((isNil "_this select 2") || (_this select 2 == "")) then {hint "true"};

or

if (!(isNil "_this select 0") && !(_this select 0 == "")) then {_markerPos = _this select 0};

if (!(isNil "_this select 1") && !(_this select 1 == "")) then {_markerName = _this select 1};

if (!(isNil "_this select 2") && !(_this select 2 == "")) then {_markerType = _this select 2};

and dont use nul = , or any global varibale as nul will always = [getPos unit1, "marker_unit1"]

so if you ask later on if (isnull anything) it will return [getPos unit1, "marker_unit1"]

use _nil = [getPos unit1, "marker_unit1"] instead

nul or nil have big probs with COD aswell

Edited by Zonekiller

Share this post


Link to post
Share on other sites

_markerColor = if (count _this > 4) then {_this select 4} else {"ColorBlack"};

or

_markerColor = "ColorBlack";

if (count _this > 4) then {_markerColor = _this select 4};

Share this post


Link to post
Share on other sites

Thanks for the quick response, however, it get mixed results. :/

I made a test-script to check the possibilities.

Multiple unit1's inits:

_nil = [getPos unit1, "marker_1"] execVM "testscript.sqf"; (should activate the default color "ColorOrange")
_nil = [getPos unit1, "marker_1", ""] execVM "testscript.sqf"; (should activate the default color "ColorOrange")
_nil = [getPos unit1, "marker_1", "ColorOrange"] execVM "testscript.sqf";

testscript.sqf:

_markerPos = _this select 0;
_markerName = _this select 1;

[b]OPTION[/b]

_markerName = createmarker [_markerName, _markerPos];
_markerName setMarkerType "hd_objective";
_markerName setMarkerText "test_marker";
_markerName setMarkerColor _markerColor;
_markerName setMarkerSize [1,1];

At the place of OPTION I tried your suggestions in the following way:

Option 1 - Only works with "" (1st init)

_markerColor = _this select 2;

if ((isNil "_this select 2") || (_this select 2 == "")) then {_markerColor = "ColorOrange"};

Option 2 - Unable to overwrite default variable (marker would always be orange)

_markerColor = "ColorOrange";

if (!(isNil "_this select 2") && !(_this select 2 == "")) then {_markerColor = _this select 2};

Option 3 - Doesn't work at all

_markerColor = if (count _this > 2) then {_this select 2} else {ColorOrange};

Option 4 - Only works when cut off (2nd init)

_markerColor = "ColorOrange";

if (count _this > 2) then {_markerColor = _this select 2};

I think I'm the one making mistakes here but I don't know what's going wrong. :confused:

Edited by NGagedFX

Share this post


Link to post
Share on other sites

It's because

if (count _this > 4) then {

checks if the user has defined that parameter. Defining does include "" as well, which is an empty string.

Edit:

So, you could probably do something like this, unless someone else thinks of a nicer method:

_markerColor = "ColorBlack";
if (count _this > 4) then {
 if ((_this select 4) != "") then {
   _markerColor = _this select 4;
 };
};

Edited by Shuko

Share this post


Link to post
Share on other sites

That explains a lot, shk. Your option works perfectly! Though it's a bit of a strange approach to be able to use defaults.

Aren't any other editors here using default values, so that you can put "" where ever you don't want to change anything?

I'm going back in the editor cause I've got a load of scripting to do, and it gets more interesting every time. :D

Share this post


Link to post
Share on other sites
_markerPos = _this select 0;

_markerName = _this select 1;

OPTION

_markerName = createmarker [_markerName, _markerPos];

_markerName setMarkerType "hd_objective";

_markerName setMarkerText "test_marker";

_markerName setMarkerColor _markerColor;

_markerName setMarkerSize [1,1];

also if your using _markercolor as a global varable and getting the color from another script without sending it through params ie : _this select 3 , it wont work

you will need to use a global varabile ie : mymarkercolor , then all scripts will be able to see what the color is. using a local _something can only be seen in the script it is run in,

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  

×