Jump to content
Sign in to follow this  
Ruckus

Question about exitWith command

Recommended Posts

I've been trying to convert several of my .sqs scripts to

.sqf scripts as it is a bit more readable for me.

The problem I'm having is we cant use "exit" in .sqf files

so the wiki says to use the "exitWith" command.

This command does not seem to exit the .sqf script.

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">private["_count"];

_count = 0;

for [{}, {true} , {true}]  do

{

   sleep 0.01;

   _count = _count + 1;

   if(_count>10) exitWith{};

   hint format["count is - %1",_count];

};

hintC"Did Not Exit";

I tried a simple test and it will display the text "Did Not Exit". Is this because "exitWith" only exits from the current scope? Or is this broken?

Share this post


Link to post
Share on other sites

It just exit the current scope. So you have to do a second check after the for-to-do structure.

Share this post


Link to post
Share on other sites

Look here: .sqs to .sqf

I would say:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">private["_count"];

_count = 0;

for [{}, {true} , {true}] do

{

sleep 0.01;

_count = _count + 1;

if(_count>10) exitWith{hint format["count is - %1",_count]};

};

hintC"Did Not Exit";

But i'm just a noob in scripting so whistle.gif

Share this post


Link to post
Share on other sites
It just exit the current scope. So you have to do a second check after the for-to-do structure.

Thanks for the reply. I assumed this was the case but for me It wasn't clear in the wiki.

You cant have commands that span more than one line in .sqs , so I goto .sqf and you cant exit a script with a command.  Sheeesh

crazy_o.gif

Share this post


Link to post
Share on other sites
Look here: .sqs to .sqf

I would say:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">private["_count"];

_count = 0;

for [{}, {true} , {true}]  do

{

  sleep 0.01;

  _count = _count + 1;

  if(_count>10) exitWith{hint format["count is - %1",_count]};

  };

hintC"Did Not Exit";

But i'm just a noob in scripting so  whistle.gif

I'm not a scripting wizzard either  biggrin_o.gif ,  but I think that might do the same thing as the one I put up.  It will only depart the current scope.  If there were lines after that scope,  they would execute I believe.

Share this post


Link to post
Share on other sites

I'm not sure what you're trying to do in your script, all you're doing is counting... why?

Share this post


Link to post
Share on other sites
I'm not sure what you're trying to do in your script, all you're doing is counting... why?

Hi Kyle, I was having alot of trouble debugging another script

that I had converted from a .sqs

I eventually found that the problem resulted from the script never exiting and messing things up.

To prove the problem, I just put that together to see if that

was the case. It appears that the exitWith command does not exit the .sqf script like the exit command did with .sqs files.

There is no other command that I'm aware of that will

directly depart a .sqf file like exit did.

Share this post


Link to post
Share on other sites
I'm not sure what you're trying to do in your script, all you're doing is counting... why?

Hi Kyle,  I was having alot of trouble debugging another script

that I had converted from a .sqs

I eventually found that the problem resulted from the script never exiting and messing things up.

To prove the problem,  I just put that together to see if that

was the case.  It appears that the exitWith command does not exit the .sqf script like the exit command did with .sqs files.

There is no other command that I'm aware of that will

directly depart a .sqf file like exit did.

Well the point I was trying to make is that it's not just the commands, but the way you structure it. There's probably a way to do this without exiting. Post your real script and tell us what you want to do.

Share this post


Link to post
Share on other sites

What I normally do in these cases is something like this:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_exit=false;

_count = 0;

while {!_exit} do

{

_count = _count + 1;

if(_count>10) then {_exit=true};

};

if (_exit) exitWith {hint format["count is - %1",_count]};

Share this post


Link to post
Share on other sites
What I normally do in these cases is something like this:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_exit=false;

_count = 0;

while {!_exit} do

{

 _count = _count + 1;

 if(_count>10) then {_exit=true};

};

if (_exit) exitWith {hint format["count is - %1",_count]};

I ended up using the While method and changing the state of the While variable,  but thanks to all for your replys.

The Script was fairly large and had other calls in it, so it would have been to big to really post here.

Share this post


Link to post
Share on other sites

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

for [{_count = 0}, {_count < 10} , {_count = _count +1}] do

{

your code here

}

The for-loop was invented specifically for doing a set of actions a specific number of times, why not take advantage of it.

Share this post


Link to post
Share on other sites
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

for [{_count = 0}, {_count < 10} , {_count = _count +1}]  do

{

   your code here

}

The for-loop was invented specifically for doing a set of actions a specific number of times, why not take advantage of it.

This was only a test to understand what was happening in one of my larger scripts.  My other script doesn't cycle a determined amount of times.  What I was trying to understand was that

"exitWith" does not leave the script like "exit" did, it only departs the current scope inside the script.

When I read the wiki regarding converting .sqs to .sqf,  it mentioned that "exit" should not be used. The "exitWith" should be used in its place according to the wiki.  I just didn't know that it's functionality was not the same.

I could have created a endless loop using the "for" command as you suggested,  however it was easyier to depart the script by using the "while" statement.

Share this post


Link to post
Share on other sites

The endless for-loop was only a crutch to get over the 10,000 cycle limitation the while command had initially.

But since that restriction has been removed now there's really no point in using that syntax anymore.

Share this post


Link to post
Share on other sites

Kronzky, do you know if in the future they are concidering allowing "exit" functionality in .sqf ? The only reason I ask is that i've converted to .sqf for readability and getting away from the single line instruction limitation.

From my noob experience with .sqf and scripting in general, is that we gain some funtionality, however we loose some as well.

For instance, I'm no fan of goto's, but sometimes that was the easyest way. I started writing software back in the 70's (hardly any today). Back then there was a palet of instructions for the programmer to use. It was up to him to decide what was the best to use. I just hate to see gaining one thing and loosing others.

Share this post


Link to post
Share on other sites

I'm not aware of any plans of ever making "exit" leave the whole SQF script (like it does in SQS), but perhaps we should start a feature request in the bug tracker.

I'm sure it would get lots of votes!

As far as switching from SQS to SQF - I was a bit hesitant at first too - mainly because SQF is a lot "greedier" (i.e. it grabs too many CPU cycles unless you actively prevent that), but once you've done the switch you'll never go back...

The structural commands that come with SQF, the way you can modularize your code and get away from spaghetti-GOTOs is definitely worth the minor hassles that come with switching and figuring out the quirks.

Share this post


Link to post
Share on other sites
I'm not aware of any plans of ever making "exit" leave the whole SQF script (like it does in SQS), but perhaps we should start a feature request in the bug tracker.

I'm sure it would get lots of votes!

As far as switching from SQS to SQF - I was a bit hesitant at first too - mainly because SQF is a lot "greedier" (i.e. it grabs too many CPU cycles unless you actively prevent that), but once you've done the switch you'll never go back...

The structural commands that come with SQF, the way you can modularize your code and get away from spaghetti-GOTOs is definitely worth the minor hassles that come with switching and figuring out the quirks.

I'm sorry I've really derailed the origonal discussion.  I was only using "goto" as a example.  There are others.

I'm more a fan of "gosub" (from days way past  biggrin_o.gif )  I know that can be done by calling another script,  I just like looking at 1 page of code vs having several different scripts open to

follow.  I'll look into starting a topic in the bug tracker.....never done that before. Thanks again.  You can close the topic if you like as the solution has been covered.

Share this post


Link to post
Share on other sites

@Ruckus

Since you are converting your scripts, now would be a good time to possibly restructure them according to better practices. IMO if you need to exit an sqf script prematurely, you can refine the script such that it branches to other scripts or calls appropriate functions. I believe exitWith is in reference to functions or to offer the ability to break a loop with a value returned. I do not believe it was to replace "exit".

We, as a community, must learn that SQS is truly different in both style, structure, and capabilities. There is no 1-1 conversion of SQS to SQF. Generally, we should convert SQS to SQF by re-examining the solution and re-develop it from the ground up....especially if it is from OFP. There are far too many new commands and features available now. People might think I am crazy, "that is way too much work...blah blah .. reuse code..blah blah...", but I say it is necessary from a quality standpoint and the time you spend trying to work around these differences (such as with exit and exitWith...) you could have rewritten it all anyways.

Share this post


Link to post
Share on other sites

Just the simple fact that control structures can return values now will save dozens of lines of code, and make things more readable.

Share this post


Link to post
Share on other sites
Quote[/b] ]I'm more a fan of "gosub" (from days way past biggrin_o.gif ) I know that can be done by calling another script, I just like looking at 1 page of code vs having several different scripts open to

follow.

Same here. As a solution, you can simply define private functions within a script, much like you would in C:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">// code

private"_myfunc";

_myfunc=

{

private["_a","_b"];

_a=_this select 0;

_b=_this select 1;

_a+_b

};

// more code

_sum = [1,2]call _myFunc;

As for the topic of exiting and switching scopes, also check out the functions 'scopeName', 'breakTo' and 'breakOut' to assign a name to a scope, and selectively breaking from/to a given scope.

Share this post


Link to post
Share on other sites

All of you have valid points,  however...  I would never have converted over to the use of .sqf at all if it wasn't for just a few things.

Firstly,  all of the scripts I had written in .sqs functioned as I wanted.  There were only 2 things that made me want to convert them.  Readability of sqs with the single line limitations were the main factor for me.  Additionaly using the { and } brackets allowed me to structure my code in a more familier format as I was a Pascal and C programmer back in the 80's.

If I was able to do this in .sqs,  I would never had converted as I didn't need to frequenly return values to calls.  To me this isn't about wholesale conversion to gain functionality,  it is almost strictly for formating.  It just seems a touch lame to gain alot of other good features,  only to stumble on one thing.

I completely agree that structure would solve the problem as that was what I did.  But doesn't it seem that you should only worry about getting the funtionality from the script and not have to worry about how to depart?

From what I've seen from other peoples works,  that they don't depend on the "Function" portion (returning values) of .sqf,  but more for format and readability.

I think that most, if not everyone here who is good with scripting has spent many hours viewing others work,  and going the next step to either improve or rethink a solution,  or redefining what can be done.  While everyone who has released scripts to the public, knows that other scripters will view and learn and improve what they have done.  So for me that is the sole reason to go this route.

I know I've been out of direct programming for quite a few years.  My views may differ from yours,  and your views are well taken.  To be honest,  I don't think what we do with .sqf is really what was intended anyway.  Functions are just what the name discribes.

Share this post


Link to post
Share on other sites
Quote[/b] ]I'm more a fan of "gosub" (from days way past  biggrin_o.gif )  I know that can be done by calling another script,  I just like looking at 1 page of code vs having several different scripts open to

follow.

Same here. As a solution, you can simply define private functions within a script, much like you would in C:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">// code

private"_myfunc";

_myfunc=

{

  private["_a","_b"];

  _a=_this select 0;

  _b=_this select 1;

  _a+_b

};

// more code

_sum = [1,2]call _myFunc;

As for the topic of exiting and switching scopes, also check out the functions 'scopeName', 'breakTo' and 'breakOut' to assign a name to a scope, and selectively breaking from/to a given scope.

Thanks for that little tidbit.....I never knew that could be done either. I have a long way to go with this stuff. biggrin_o.gif

Share this post


Link to post
Share on other sites
Quote[/b] ]From what I've seen from other peoples works, that they don't depend on the "Function" portion (returning values) of .sqf, but more for format and readability.

Just because they do it that way, doesn't make it right.

Consequently, what I get from you argument is that you are frustrated because you don't want to use either convention (SQS or SQF) as they were intended to be used. This seems a bit anti-progressive to me. You are only setting yourself up for these pitfalls because you want to go Left instead of Up or Down.

Share this post


Link to post
Share on other sites
Quote[/b] ]From what I've seen from other peoples works,  that they don't depend on the "Function" portion (returning values) of .sqf,  but more for format and readability.

Just because they do it that way, doesn't make it right.

Consequently, what I get from you argument is that you are frustrated because you don't want to use either convention (SQS or SQF) as they were intended to be used. This seems a bit anti-progressive to me. You are only setting yourself up for these pitfalls because you want to go Left instead of Up or Down.

No, not frustrated. Both .sqf and .sqs are very good. Its just that .sqs seems to be going away, or they would like us to move tward .sqf format. I just hate to loose capibility while moving ahead.

Your earlyer points are well taken. As I'm learning this stuff I'll have to forget old habits I guess. Again, I'm just a noob at this stuff anyway.

Share this post


Link to post
Share on other sites

No, we're not losing capability by switching from .sqs to .sqf, only gaining. Any functionality that could have been done with .sqs can now be restructured for .sqf. I find the best thing to do at this point is forget all about .sqs, forget that it ever existed, and start from the ground up with .sqf. It may not be as straight forward or newb-friendly, and it certainly requires more planning, but it's technically superior to .sqs.

Back to the specific topic, there really isn't any technical need to exit in .sqf, an entire .sqf script can be written without exiting if you structure it right, with no less functionality than an .sqs script filled with exits. Also, exitWith fulfills its function, IMO it does just what we need it to, no more, no less.

Share this post


Link to post
Share on other sites

The script I was debugging was a script to handle I/O from and to a Dialog box. Therefore it needed to end once the dialog was completed. In the case of the .sqs, exit worked perfectly.

I ended up restructuring the layout and exited the loop without further problems. What I read in the wiki wasn't clear for me, so I expected one thing, and got another. I just need to learn the bounds of the newer format. Several have suggested other methods to use in the .sqf format that I didn't know were available either.

I'll get there some day biggrin_o.gifcrazy_o.gif

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  

×