Jump to content
Sign in to follow this  
eymerich

What's wrong with this code?

Recommended Posts

I heed help.

This is a simple eject sqf script.

I execute it with

_null = [units group player, Truck] execVm "Eject.sqf"

but the code is not run...:butbut:

//code

{

if (!isServer) exitWith {};

private ["_index", "_arr", "_truck"];

_arr = _this select 0;

_truck = _this select 1;

_index = count _arr;

player sidechat "eseguito";

for [{_index = count _arr},{_index < 0},{_index = _index - 1}]

do

{

sleep 1;

unassignvehicle _arr select _index;

(_arr select _index) action ["eject", _truck];

};

};

Thanks in advance

Share this post


Link to post
Share on other sites

The problem is where you defined your private variables to the wrong index. _index = _this select 0 | _arr = _this select 1 | _truck = _this select 2;

Share this post


Link to post
Share on other sites

Another problem is that you rely on the "player" variable and at the same time you force the script to end when it is not run by the server.

Therefor it can never happen.

Share this post


Link to post
Share on other sites
The problem is where you defined your private variables to the wrong index. _index = _this select 0 | _arr = _this select 1 | _truck = _this select 2;

Thanks.

But how do I get the "source" data (units group player, Truck)?

I thought that the expression "_this" was related to the first argument executed (units group player), so writing:

_arr = _this select 0 was related to the first argument of the array _null = [units group player,...] execVM..., composed by all the units in the player Group.

(jusk like suggested here

http://community.bistudio.com/wiki/execVM

)

Could you kindly tell me why I am wrong?

@Tajin Thanks for your observation too

Share this post


Link to post
Share on other sites
// Ejects a group from whatever vehicle they happen to be in.
// _null = [group player] execVM "ejectGroup.sqf";

_group = _this select 0;

{
sleep 1;
unassignvehicle _x;
_x action ["eject", vehicle _x];
} forEach units _group;

Share this post


Link to post
Share on other sites
// Ejects a group from whatever vehicle they happen to be in.
// _null = [group player] execVM "ejectGroup.sqf";

_group = _this select 0;

{
sleep 1;
unassignvehicle _x;
_x action ["eject", vehicle _x];
} forEach units _group;

Thank you very much..

Share this post


Link to post
Share on other sites

Ok Kylania's example is a nice solid all round performer, pass it a group and it will eject each of the group members from what ever vehicle they are in. Use that.

The problem is where you defined your private variables to the wrong index. _index = _this select 0 | _arr = _this select 1 | _truck = _this select 2;
Not quite sure what Johnson is on about here, the declaration of private variables has nothing to do with assigning the index's of _this.
Another problem is that you rely on the "player" variable and at the same time you force the script to end when it is not run by the server.

Therefor it can never happen.

Tajin's reply is a good observation to note.

Exiting the script on !isserver will cause your script to not run if you are either a player on a dedicated server or a client of a hosted server. However if you are playing a SP game or are the host of a hosted server it would work but syntax wise is 1. not what you want 2. not a sound way to handle this.

[color="#FF0000"]{[/color]

[color="#00bf00"]if (!isServer) exitWith {};

private ["_index", "_arr", "_truck"];

_arr = _this select 0;

_truck = _this select 1;

_index = count _arr;

player sideChat "eseguito";[/color]
for [{_index = count _arr},{_index < 0},{_index = _index - 1}] do {
	[color="#00bf00"]sleep 1;
	unassignVehicle _arr select _index;
	(_arr select _index) action ["eject", _truck];
};[/color]

[color="#FF0000"]};[/color]

There is no need to wrap your script in code braces {. Marked above in red. Although it does not harm.

Everything markerd in green is generally ok (taking note of Tajin's comment).

You declare your private variables and then assign the passed array to your variables and display a sidechat message from the player.

You sleep for a second, unassign a unit from their vehicle and eject the unit from the _truck.

Your FOR setup is where everything falls apart.

FOR syntax is

for [{initial index},{while},{index maths per loop iteration}] do {

1. (minor) you reassign _index, you have already made _index equal to the count of _arr previously, you could either place just _index for the initial index or remove the previous code.

2. (error) _index being the count of _arr means it is one greater than the number of index's available from _arr.

For example lets say you pass the script a group that has 3 units, Index available in _arr would be 0,1,2, the count of _arr would be 3. When your script goes to get _arr select _index(3) you will receive an error. _index would need to be initialised as

_index = (count _arr) -1

3. (cause of loop not functioning at all) You have set your while condition to be while _index < 0, sticking with our example of a group of 3 units passed to the script, _index straight away is greater than 0(3) so the for loop will exit with out doing anything.

The while condition really needs to be while _index > -1, greater than -1 as remember _arr's first index is actually 0.

Taking everything above into account your FOR loop should look like this

_index = (count _arr) -1;
for [{_index},{_index > -1},{_index = _index - 1}] do {

A better way (read cleaner, maybe) to write your FOR would be

_index = count _arr;
for[{_loop = 0},{_loop < _index},{_loop = _loop + 1}] do {

Here loop will start at 0 and increase while it is smaller than _index (the count of _arr) giving us the values 0,1,2.

Any way as i stated at the beginning of this use Kylania's example but i hope the above has given you an understanding of why your script was not working.

Larrow

Edited by Larrow

Share this post


Link to post
Share on other sites
Ok Kylania's example is a nice solid all round performer, pass it a group and it will eject each of the group members from what ever vehicle they are in. Use that.

Not quite sure what Johnson is on about here, the declaration of private variables has nothing to do with assigning the index's of _this.

Counting in computers start at 0. So if you have 3 indexes in the array. The first object is at index 0. So in his code he put
_truck = _this select 1;

So he basically assigned _truck to _arr.

Share this post


Link to post
Share on other sites
Counting in computers start at 0. So if you have 3 indexes in the array. The first object is at index 0. So in his code he put
_truck = _this select 1;

So he basically assigned _truck to _arr.

You are confusing the assignment of private variables with the array being referenced by the script.

_null = [units group player, Truck] execVm "Eject.sqf"  //this array is being referenced

private ["_index", "_arr", "_truck"];  //this array is not being referenced

_arr = _this select 0;  //  "unit groups player"

_truck = _this select 1;  //  "Truck"

_index = count _arr;

\/\/\/ Kylania explains this a bit more concisely.

Edited by Harzach

Share this post


Link to post
Share on other sites

No. _this is the input array from the execVM line, in this case:

[units group player, truck]

So within the script _this is an array containing the above.

He assigns _arr to _this select 0 which is units group player, an array.

He assigns _truck to _this select 1, the second element of the _this array, which is truck.

Edit:

Oh, private. Don't think that order matters or ever is used at all. :)

Share this post


Link to post
Share on other sites

Roger now I understand. I totally misunderstood the question.

---------- Post added at 14:18 ---------- Previous post was at 14:16 ----------

Roger I misunderstood the question. I stand corrected sorry about that.

Share this post


Link to post
Share on other sites
Ok Kylania's example is a nice solid all round performer, pass it a group and it will eject each of the group members from what ever vehicle they are in. Use that.

Your FOR setup is where everything falls apart.

FOR syntax is

for [{initial index},{while},{index maths per loop iteration}] do {

1. (minor) you reassign _index, you have already made _index equal to the count of _arr previously, you could either place just _index for the initial index or remove the previous code.

2. (error) _index being the count of _arr means it is one greater than the number of index's available from _arr.

For example lets say you pass the script a group that has 3 units, Index available in _arr would be 0,1,2, the count of _arr would be 3. When your script goes to get _arr select _index(3) you will receive an error. _index would need to be initialised as

_index = (count _arr) -1

3. (cause of loop not functioning at all) You have set your while condition to be while _index < 0, sticking with our example of a group of 3 units passed to the script, _index straight away is greater than 0(3) so the for loop will exit with out doing anything.

The while condition really needs to be while _index > -1, greater than -1 as remember _arr's first index is actually 0.

Taking everything above into account your FOR loop should look like this

_index = (count _arr) -1;
for [{_index},{_index > -1},{_index = _index - 1}] do {

A better way (read cleaner, maybe) to write your FOR would be

_index = count _arr;
for[{_loop = 0},{_loop < _index},{_loop = _loop + 1}] do {

Here loop will start at 0 and increase while it is smaller than _index (the count of _arr) giving us the values 0,1,2.

Any way as i stated at the beginning of this use Kylania's example but i hope the above has given you an understanding of why your script was not working.

Larrow

Thanks for the your time and for the explanation.

Got what i did wrong! :)

(to be honest about "For .. Do" statement we hate each other and infact those were the results...).

This:

_index = count _arr;

for[{_loop = 0},{_loop < _index},{_loop = _loop + 1}] do {

Rocks, remind me the good loop in sqs script...

One last question:

is this statement correct?

_loop = 0;

_index = count _arr;

for[{_loop < _index},{_loop = _loop + 1}] do

{

}

Thanks for everyone who has post here.

:)

Share this post


Link to post
Share on other sites
// Ejects a group from whatever vehicle they happen to be in.
// _null = [group player] execVM "ejectGroup.sqf";

_group = _this select 0;

{
sleep 1;
unassignvehicle _x;
_x action ["eject", vehicle _x];
} forEach units _group;

The same script from the previous versions of ARMA, no point in trying to reinvent the wheel.

Share this post


Link to post
Share on other sites
is this statement correct?

_loop = 0;

_index = count _arr;

for[{_loop < _index},{_loop = _loop + 1}] do

No the FOR statement needs all of its statements declared. [{INDEX}, {WHILE}, {MATH/STEP}]
remind me the good loop in sqs script...
Arghh i hated sqs, even though ive been playing arma since OFP days i took one look at sqs and didnt even bother, coming from a C background the syntax was horrible.

Infact sqs can @!\? lol

The same script from the previous versions of ARMA, no point in trying to reinvent the wheel.

No but C&P only helps in the shortterm, helping people to why their script syntax does not work lasts a lifetime. hopefully

Edited by Larrow

Share this post


Link to post
Share on other sites
No but C&P only helps in the shortterm, helping people to why their script syntax does not work lasts a lifetime. hopefully

I completely understand, I think we have all gone through the same frustrations of figuring out scripts. I still use what Kylania posted works great for airdrops, if you come up with something better I would love to try it out.

Share this post


Link to post
Share on other sites

As i stated in my first post

"Kylania's example is a nice solid all round performer, pass it a group and it will eject each of the group members from what ever vehicle they are in. Use that."

short and to the point does exactly what it says on the tin no need to try rewriting that one :D

Share this post


Link to post
Share on other sites
No the FOR statement needs all of its statements declared. [{INDEX}, {WHILE}, {MATH/STEP}]Arghh i hated sqs, even though ive been playing arma since OFP days i took one look at sqs and didnt even bother, coming from a C background the syntax was horrible.

Infact sqs can @!\? lol

No but C&P only helps in the shortterm, helping people to why their script syntax does not work lasts a lifetime. hopefully

Thanks again.

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  

×