Jump to content
Sign in to follow this  
singleton

Problem with array in custom unit script.

Recommended Posts

Hey guys,

I have an issue with a custom unit script which is used in our custom models. Basically what it does is detects whether or not the unit has NVGs, and if they have NVGs in the inventory, they show on the helmet in the up position. If not, they dont show at all.

The script works on Dedi MP if and only if there is one player on with the models. Once more than one player get on, the script fails to work and you cannot see the changes to anybody elses NVGs.

This is the error

<<< Error in expression <rray = (_this select 0);

_unit = _array select 0;

_fnc = _array select 1;

[_unit>

Error position: <select 0;

_fnc = _array select 1;

[_unit>

Error select: Type String, expected Array,Config entry

File ODA0117\NVG\dagr_unitAppearance_init.sqf, line 12

this is the script

private ["_unit","_array","_fnc"];

if (!isServer) then {while{isNull player} do {sleep 0.1}};

// Define variables

_unit = _this select 0;

//Start NVGappearnce script

[_unit] execVM "\ODA0117\NVG\NVGappearance.sqf";

// Add PVeventhandler to change hiddenSelections (unit Appearance)

"DAGR_uApp_PV" addPublicVariableEventHandler {

_array = (_this select 0);

_unit = _array select 0;

_fnc = _array select 1;

[_unit] spawn _fnc;

};

I semi-understand the error in how it is saying the _array is declared but not used, and how its trying to use select on an array, which is actually a string.

I just dont have the know how with how to fix it. Any ideas?

Share this post


Link to post
Share on other sites

The error message is telling you that your input is a "STRING" where an [ARRAY] is expected. Please post the script that broadcasts "DAGR_uApp_PV". Oh and please put code between code tags so that it displays correctly formatted.

private ["_unit","_array","_fnc"];

if (!isServer) then {
 while {isNull player} do {
   sleep 0.1;
 };
};

// Define variables
_unit = _this select 0;

//Start NVGappearnce script
[_unit] execVM "\ODA0117\NVG\NVGappearance.sqf";

// Add PVeventhandler to change hiddenSelections (unit Appearance)
"DAGR_uApp_PV" addPublicVariableEventHandler {
 _array = (_this select 0);
 _unit = _array select 0;
 _fnc = _array select 1;
 [_unit] spawn _fnc;
};

Edited by Demon Cleaner

Share this post


Link to post
Share on other sites

Woops, sorry about that :) learn something new everytime!

This is NVGappearance.sqf

private ["_unit","_c","_d","_restart","_respawnType","_respawnNo","_respawnText"];
if (!isServer) then {while{isNull player} do {sleep 0.1}};
// Define variables
_unit 		 = _this select 0;
if (!local _unit) exitWith {};
_c 			 = 0;
_d			 = 0;
_restart 	 = false;
_respawnType  = ["INSTANT","BASE","GROUP","SIDE","Instant","Base","Group","Side","instant","base","group","side"];
// Determine whether respawn is enabled in the mission
_respawnNo 	 = getNumber(missionConfigFile >> "Respawn");
_respawnText = getText(missionConfigFile >> "Respawn");
if (_respawnText in _respawnType || _respawnNo > 1) then {_restart = true}; 
// Exit script if unit is not one of Ards units
if !(_unit isKindOf "ODA0117_WDL_TLDA") exitWith {};  //Ard needs to add something here!!!
// HIDE NVGS
if (_unit hasWeapon "NVgoggles") then 
{
_unit setObjectTexture [3,"\ODA0117\data\pvs14_co.paa"];
} else {
_unit setObjectTexture [3,""];
};
while {alive _unit && local _unit} do
{
while {_unit hasWeapon "NVgoggles" && alive _unit && local _unit} do 
{	
	if (currentVisionMode _unit == 1 && _c == 0) then  {
		[color="#FF8C00"]DAGR_uApp_PV = [_unit, DAGR_NVG_ard];[/color]
		[color="#FF8C00"]publicVariable "DAGR_uApp_PV";[/color]
		_unit setObjectTexture [3,""];
		hint "";
		_c = 1;
	};
	if (currentVisionMode _unit != 1 && _c == 1) then  {
		[color="#FF8C00"]DAGR_uApp_PV = [_unit, DAGR_NVG_ard];[/color]
		[color="#FF8C00"]publicVariable "DAGR_uApp_PV";[/color]
		_unit setObjectTexture [3,"\ODA0117\data\pvs14_co.paa"]; //Ard needs to add something here!!!
		hint "";
		_c = 0;
	};
	_d = 1;
	sleep 1;
	hint "";
};
if (!(_unit hasWeapon "NVgoggles") && _c == 1 || !(_unit hasWeapon "NVgoggles") && _d == 1) then
{	
	[color="#FF8C00"]DAGR_uApp_PV = [_unit, DAGR_NVG_ard];[/color]
	[color="#FF8C00"]publicVariable "DAGR_uApp_PV";[/color]
	_unit setObjectTexture [3,""];
	hint "No NVG";
	_c = 0;
	_d = 0;
	sleep 1;
	hint "";
};
while {!(_unit hasWeapon "NVgoggles") && alive _unit && local _unit} do {sleep 1};
if (_unit hasWeapon "NVgoggles" && alive _unit && local _unit) then {_unit setObjectTexture [3,"\ODA0117\data\pvs14_co.paa"]};
};
// restart NVG script
if (_restart && _unit == player) then {
while {!alive player} do {sleep 1};
[player] execVM "\ODA0117\NVG\NVGappearance.sqf";
};

this is DAGR_unitappearance.sqf

private ["_unit","_array","_fnc"];
if (!isServer) then {while{isNull player} do {sleep 0.1}};
// Define variables
_unit = _this select 0;
//Start NVGappearnce script
[_unit] execVM "\ODA0117\NVG\NVGappearance.sqf";
// Add PVeventhandler to change hiddenSelections (unit Appearance)
[color="#FF8C00"]"DAGR_uApp_PV" addPublicVariableEventHandler {[/color]
_array = (_this select 0);
_unit = _array select 0;
_fnc = _array select 1;
[_unit] spawn _fnc;
};

DAGR_NVG_ard  	= Compile PreprocessFile "\ODA0117\NVG\fn\NVG_PVfn.sqf"; // add/remove NVG to/from helmet function

Edited by JollyResQ

Share this post


Link to post
Share on other sites

init.sqf

"JTK_Call_PVEH" addPublicVariableEventHandler {((_this select 1) select 0) call ((_this select 1) select 1)};

This is NVGappearance.sqf

private ["_unit","_c","_d","_restart","_respawnType","_respawnNo","_respawnText"];
if (!isServer) then {while{isNull player} do {sleep 0.1}};
// Define variables
_unit 		 = _this select 0;
if (!local _unit) exitWith {};
_c 			 = 0;
_d			 = 0;
_restart 	 = false;
_respawnType  = ["INSTANT","BASE","GROUP","SIDE","Instant","Base","Group","Side","instant","base","group","side"];
// Determine whether respawn is enabled in the mission
_respawnNo 	 = getNumber(missionConfigFile >> "Respawn");
_respawnText = getText(missionConfigFile >> "Respawn");
if (_respawnText in _respawnType || _respawnNo > 1) then {_restart = true}; 
// Exit script if unit is not one of Ards units
if !(_unit isKindOf "ODA0117_WDL_TLDA") exitWith {};  //Ard needs to add something here!!!

// HIDE NVGS
_texture = if (_unit hasWeapon "NVgoggles") then {[3,"\ODA0117\data\pvs14_co.paa"]}else{[3,""]};
_unit setObjectTexture _texture;
JTK_Call_PVEH = [_unit,_texture],compile"(_this select 0) setObjectTexture (_this select 1)"];
publicVariable "JTK_Call_PVEH"; 

while {alive _unit && local _unit} do{
while {_unit hasWeapon "NVgoggles" && alive _unit && local _unit} do {
	_texture = if (currentVisionMode _unit == 1 && _c == 0) then{[3,""]}else{[3,"\ODA0117\data\pvs14_co.paa"]};
	_c = if ((_texture select 1) == "") then{1}else{0};
	_unit setObjectTexture _texture;
	JTK_Call_PVEH = [_unit,_texture],compile"(_this select 0) setObjectTexture (_this select 1)"];
	publicVariable "JTK_Call_PVEH";
	_d = 1;
	sleep 1;
	hint "";
};
if (!(_unit hasWeapon "NVgoggles") && _c == 1 || !(_unit hasWeapon "NVgoggles") && _d == 1) then {	
	_unit setObjectTexture [3,""];
	JTK_Call_PVEH = [_unit,[3,""]],compile"(_this select 0) setObjectTexture (_this select 1)"];
	publicVariable "JTK_Call_PVEH";
	hint "No NVG";
	_c = 0;
	_d = 0;
	sleep 1;
	hint "";
};
while {!(_unit hasWeapon "NVgoggles") && alive _unit && local _unit} do {sleep 1};
if (_unit hasWeapon "NVgoggles" && alive _unit && local _unit) then {
	_unit setObjectTexture [3,"\ODA0117\data\pvs14_co.paa"];
	JTK_Call_PVEH = [_unit,[3,"\ODA0117\data\pvs14_co.paa"]],compile"(_this select 0) setObjectTexture (_this select 1)"];
	publicVariable "JTK_Call_PVEH";
};
};
// restart NVG script
if (_restart && _unit == player) then {
while {!alive player} do {sleep 1};
[player] execVM "\ODA0117\NVG\NVGappearance.sqf";
};

Share this post


Link to post
Share on other sites

As Kempco shows in his example the actual texture change is done inside NVGappearance.sqf depending on the "hasWeapon" condition:

while {_unit hasWeapon "NVgoggles" && alive _unit && local _unit} do {
 _texture = if (currentVisionMode _unit == 1 && _c == 0) then{[3,""]}else{[3,"\ODA0117\data\pvs14_co.paa"]};
 _c = if ((_texture select 1) == "") then{1}else{0};

 _unit setObjectTexture _texture;
 JTK_Call_PVEH = [_unit,_texture],compile"(_this select 0) setObjectTexture (_this select 1)"];
  publicVariable "JTK_Call_PVEH";

 _d = 1;
 sleep 1;
 hint "";
};

The publicVariableEventHandler can be used to notify all clients in a multiplayer mission to execute a certain action upon receiving an update to a specific variable. "JTK_Call_PVEH" in this case. As you can see I've formatted the loop in a way that 3 commands stand together. The first command changes the texture on the local client. The second command contains the instruction to change the texture on a specific object. The third command actually broadcasts this instruction to all other clients. This is necessary due to the locality of objects.

Understanding the locality of objects is a key principle to understand in order to successfully create multiplayer missions. This link will provide some answers to the questions you might have: http://community.bistudio.com/wiki/Locality_in_Multiplayer

In the above case adding a publicVariableEventHandler named "JTK_Call_PVEH" to each client will result in each client locally updating the texture for a specific object each time it receives an update to the "JTK_Call_PVEH" variable. In the case of your script I would suggest you implement commands to output the value of "DAGR_uApp_PV" every time an update is received. I believe this will show that you're either receiving a "TRUE" or an error message e.g a "string" instead of the required [ARRAY] of values. If this is the case backtrace the origin of the error from there. Most likely a command executed prior to broadcasting "DAGR_uApp_PV" does not yield the expected results.

DC

Share this post


Link to post
Share on other sites

Ahh okay thank you very much guys for the help. For the record, this is NOT my NVG Script. This was done by Norrin and put into my addon :)

Cheers,

Keith

Share this post


Link to post
Share on other sites

For the fix for my NVG script, in single player, it returns a script error

Error in expression <ct 0) setObjectTexture (_this select 1)"];
publicVariable "JTK_Call_PVEH"; 

whi>
 Error position: <];
publicVariable "JTK_Call_PVEH"; 

whi>
 Error Missing ;

Now, when in multiplayer, there is no script error (or at least that pops up). It does seem to spam my RPT when I checked though. The actual result of the fix, I can see other persons NVGs in the up position all of the time. Once a person or AI puts their NVGs down, the NVGs appear in the up AND down position. Any ideas?

Share this post


Link to post
Share on other sites

I just copy pasta'd Kempco's NVG Appearance.sqf fix and put his line of init.sqf into the init of a mission

Share this post


Link to post
Share on other sites

Well how do you expect this to work ? If I'm not mistaken your DAGR_unitappearance.sqf still reads:

"DAGR_uApp_PV" addPublicVariableEventHandler {
_array = _this select 0;
_unit = _array select 0;
_fnc = _array select 1;
[_unit] spawn _fnc;
};

Try this:

1. Change all occurances of "JTK_Call_PVEH" in NVGappearance.sqf to "DAGR_uApp_PV"

2. It looks as if there's an "[" missing in the variable definitions of NVGappearance.sqf

Try to correct that as follows:

DAGR_uApp_PV = [[_unit,_texture],compile "(_this select 0) setObjectTexture (_this select 1)"];

3. DAGR_uApp_PV is now a multi dimensional array so change the publicVariableEventHandler to:

"DAGR_uApp_PV" addPublicVariableEventHandler {
 private ["_unit","_texture","_fnc"];
 _unit = (_this select 0) select 0;
 _texture = (_this select 0) select 1;
 _fnc = _this select 1;
 [_unit,_texture] spawn _fnc;
};

Share this post


Link to post
Share on other sites

Sorry, missing a couple ['s. Script errors are gone, however can't confirm the texture changes (assuming addon required).

NVGappearance

private ["_unit","_c","_d","_restart","_respawnType","_respawnNo","_respawnText"];
if (!isServer) then {while{isNull player} do {sleep 0.1}};
// Define variables
_unit 		 = _this select 0;
if (!local _unit) exitWith {};
_c 			 = 0;
_d			 = 0;
_restart 	 = false;
_respawnType  = ["INSTANT","BASE","GROUP","SIDE","Instant","Base","Group","Side","instant","base","group","side"];
// Determine whether respawn is enabled in the mission
_respawnNo 	 = getNumber(missionConfigFile >> "Respawn");
_respawnText = getText(missionConfigFile >> "Respawn");
if (_respawnText in _respawnType || _respawnNo > 1) then {_restart = true}; 
// Exit script if unit is not one of Ards units
if !(_unit isKindOf "ODA0117_WDL_TLDA") exitWith {};  //Ard needs to add something here!!!

// HIDE NVGS
_texture = if (_unit hasWeapon "NVgoggles") then {[3,"\ODA0117\data\pvs14_co.paa"]}else{[3,""]};
_unit setObjectTexture _texture;
JTK_Call_PVEH = [[_unit,_texture],compile "(_this select 0) setObjectTexture (_this select 1)"];
publicVariable "JTK_Call_PVEH"; 

while {alive _unit && local _unit} do{
while {_unit hasWeapon "NVgoggles" && alive _unit && local _unit} do {
	_texture = if (currentVisionMode _unit == 1 && _c == 0) then{[3,""]}else{[3,"\ODA0117\data\pvs14_co.paa"]};
	_c = if ((_texture select 1) == "") then{1}else{0};
	_unit setObjectTexture _texture;
	JTK_Call_PVEH = [[_unit,_texture],compile"(_this select 0) setObjectTexture (_this select 1)"];
	publicVariable "JTK_Call_PVEH";
	_d = 1;
	sleep 1;
	hint "";
};
if (!(_unit hasWeapon "NVgoggles") && _c == 1 || !(_unit hasWeapon "NVgoggles") && _d == 1) then {	
	_unit setObjectTexture [3,""];
	JTK_Call_PVEH = [[_unit,[3,""]],compile"(_this select 0) setObjectTexture (_this select 1)"];
	publicVariable "JTK_Call_PVEH";
	hint "No NVG";
	_c = 0;
	_d = 0;
	sleep 1;
	hint "";
};
while {!(_unit hasWeapon "NVgoggles") && alive _unit && local _unit} do {sleep 1};
if (_unit hasWeapon "NVgoggles" && alive _unit && local _unit) then {
	_unit setObjectTexture [3,"\ODA0117\data\pvs14_co.paa"];
	JTK_Call_PVEH = [[_unit,[3,"\ODA0117\data\pvs14_co.paa"]],compile"(_this select 0) setObjectTexture (_this select 1)"];
	publicVariable "JTK_Call_PVEH";
};
};
// restart NVG script
if (_restart && _unit == player) then {
while {!alive player} do {sleep 1};
[player] execVM "\ODA0117\NVG\NVGappearance.sqf";
};

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  

×