Jump to content
Sign in to follow this  
tissue901

How to get object from object init

Recommended Posts

Hello, 

I just started looking at scripting today and I have a simple question that I haven't been able to easily find the answer on google for.

 

I'm placing an enemy on the map and calling this script in the Object Init:

enemy = this
null = execVM "enemySpawn.sqf";

Inside of this script I have the following code:

 

numAlive = numAlive + 1;
publicVariable "numAlive";

while(alive enemy) 
do {


} 

numAlive = numAlive - 1;
publicVariable "numAlive";

What I'm trying to do is on spawn increment a count of enemies and then when they are killed decrement it. Clients then see this change and report on the HUD how many enemies are still alive. When I try and run this it comes back with the error:

error while: type bool, expected code

What am I doing wrong here? Also is there a way to get the object that initialized the script directly in the script? I tried doing "alive this" but this is undefined.

Share this post


Link to post
Share on other sites

The error is caused by using () instead of { } around your while condition.

 

For more learning see if any of this makes sense to you:

// Called by: _scriptHandle = [this] execVM "enemySpawn.sqf";
// To debug: _scriptHandle = [this, true] execVM "enemySpawn.sqf";

params["_unit", ["_debug", false]];

// We sleep here to make sure all the messages happen in game since this is called via an init line.
sleep 1;

// In init.sqf or an init field we've previously run this command to initialize the variable:
// missionNameSpace setVariable ["numAlive", 0, true];

missionNameSpace setVariable["numAlive", (missionNameSpace getVariable "numAlive") + 1, true];

if (_debug) then {
	systemChat format["Before while there are currently %1 units active", missionNameSpace getVariable "numAlive"];
};


while {alive _unit} do {
	// stuffs

	systemChat format["There are currently %1 units active", missionNameSpace getVariable "numAlive"];
	sleep random (10);
	_unit setDamage 1;
};


missionNameSpace setVariable["numAlive", (missionNameSpace getVariable "numAlive") - 1, true];

if (_debug) then {
	systemChat format["After while there are currently %1 units active", missionNameSpace getVariable "numAlive"];
};

Try it with multiple units and watch the debug messages.

Share this post


Link to post
Share on other sites

Thank you, that worked perfectly. After years of lots of different languages I skimmed over the fact that while loops use {} instead of () here.

 

I'm now trying to spawn these units programatically because it would be easier to keep count of them, but I'm running into an issue when trying to change their weapons.

 

I'm doing the following:

contactGroup = [getMarkerPos "ai_1", EAST, 5] call BIS_fnc_spawnGroup;

{
removeAllWeapons _x;
removeAllItems _x;
} foreach units contactGroup;

This spawns the group fine, but the foreach complains about an undefined _x variable. Looking at the wiki (https://community.bistudio.com/wiki/forEach) I cant see what I'm doing wrong. Isnt _x a magic variable that is defined by the engine? I've also tried declaring it as private both inside and outside of the foreach but it still complains. Any idea?

Share this post


Link to post
Share on other sites

Maybe a timing thing, like spawnGroup hadn't finished by the time you started trying to remove items from it's unspawned units?  Can you put a delay or group size == 5 check or something there?

Share this post


Link to post
Share on other sites

Try

{
	_x addEventHandler ["HandleIdentity", {
		private _unt = _this select 0;
		_unt removeAllEventHandlers "HandleIdentity";
		removeAllWeapons _unt;
		removeAllItems _unt;
		false}];
} forEach (units contactGroup);

UPD: Code fixed

UPD: Argument usage fixed

Share this post


Link to post
Share on other sites

Maybe a timing thing, like spawnGroup hadn't finished by the time you started trying to remove items from it's unspawned units?  Can you put a delay or group size == 5 check or something there?

 

Putting in a sleep did not work and it still complains _x is undefined.

 

 

Try

{
	_x addEventHandler ["HandleIdentity", {
		_this removeAllEventHandlers "HandleIdentity";
		removeAllWeapons _this;
		removeAllItems _this;
		false}];

} forEach (units contactGroup);

UPD: Code fixed

 

This added the event handler, but complains that _this is an array and not an object. 

 

Solution:

I just found a solution that works, but I'm not sure how it differs from what I originally had. Instead of calling removeAllWeapons and removeAllItems in the loop directly, I'm passing it to a separate script called loadout.sqf. In that file I have:

params["_x"];
removeAllWeapons _x;
removeAllItems _x;

UPD: Here is the loop as well incase any one else runs into this:

_spawngrp = [getMarkerPos "ai_1", EAST, 5] call BIS_fnc_spawnGroup;
{[_x] execVM "loadout.sqf";} forEach units _spawngrp;

 

I'm assuming it is a timing thing and for some reason by the time execVM runs the group has properly spawned?

 

UPD 2:

I fixed it. I was running a script called serverScript.sqf from init.sqs. This was causing issues because I think it was loading it as an sqs script and everything needed to be on one line. I dislike the sqs syntax so I moved everything into initServer.sqf and its working great now.

Share this post


Link to post
Share on other sites

This added the event handler, but complains that _this is an array and not an object. 

 

Fixed:
{
	_x addEventHandler ["HandleIdentity", {
		private _unt = _this select 0;
		_unt removeAllEventHandlers "HandleIdentity";
		removeAllWeapons _unt;
		removeAllItems _unt;
		false}];
} forEach (units contactGroup);

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  

×