Home   Help Search Login Register  

Author Topic: Functions (while...do)  (Read 740 times)

0 Members and 1 Guest are viewing this topic.

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Functions (while...do)
« on: 28 Jan 2005, 05:08:09 »
does anyone know the exactly way that the while...do command works? basically, does it always check for the condition or does in work like a script loop where it can change something and then it goes back and checks

to better explain what i mean

#randomloop
?getpos gl != _pos:exit
_pos = [_pos select 0,_pos select 1 +5,_pos select 2]
gl setpos _pos
goto "loop"

in between changing the variable _pos and setposing the gl, the check at the beginning would show up false, so would the while...do command doing that exact same kind of thing,more or less,(just to stick with the example) stop because it turned false halfway through the function? or does it only check after executing the code?

hope thats understandable

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:Functions (while...do)
« Reply #1 on: 28 Jan 2005, 05:46:35 »
Eh.... I think I understand what you are asking. Basically, you are on the right track.

The "while" function will keep looping thru it's code until it's condition is no longer true. In other words, it will repeat the code "while" the condition is true. It checks the condition just before the BEGINNING of the code is executed. If the condition is true, it executes the code. If it is false, it skips the code and moves on to the next line. Example:

_x = 1;
while {_x < 10}
do
{
_x = _x + 1;
_x = _x + 1;
};
hint format ["%1", _x];

The script will set x to 1. Then it will check to see if x is below 10. It is, so it goes on and executes ALL of the code (adds 1 to x twice). Then it loops back up to the "while", and checks again whether x is less than 10. It keeps doing this until x is greater than or equal to 10, and then it jumps down to the hint (which would end up displaying 11).

So, again, it only checks the condition right before executing ALL of the code. It will not "end" right in the middle of it's code. Basically it works just like the "goto" loop that you used in your example.... with a few restrictions.

First off, in a script, all of the code in the "while" loop must be put on one line, making it hard to read. Secondly, you cannot have delays ~1 in a while loop (or in any codestring, for that matter). Last, there is a limit to the number of times the "while" code can loop, so it cannot be used for an infinite or indefinate loop like goto's can.

So basically, there aren't really any advantages to using "while" if you are already used to looping goto's. Normally, using 'goto' statements is bad programming practice, but with OFP, you take what you get I suppose:) . However, you cannot use goto's in functions, so you must use while's instead. Luckily, you can put the code on multiple lines in functions, so it isn't a problem.
HANDSIGNALS COMMAND SYSTEM-- A realistic squad-control modification for OFP
kexp.org-- The best radio station in the world, right here at home! Listen to John Richards!

Offline Fragorl

  • Coding Team
  • Former Staff
  • ****
Re:Functions (while...do)
« Reply #2 on: 28 Jan 2005, 10:53:19 »
Normally, you'd have flow control statemtns like 'break', whch would end the loop there and then, but not ofp?

Offline MachoMan

  • Honoured
  • Former Staff
  • ****
  • KISS, Keep it Simple Stupid
Re:Functions (while...do)
« Reply #3 on: 28 Jan 2005, 10:58:04 »
That's nonsense Fragorl, you dont need those in most modern OO languages. C++ & Java for example both use while about the same way ofp does.
Get those missions out there you morons!

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Re:Functions (while...do)
« Reply #4 on: 28 Jan 2005, 14:30:33 »
I'm old school like Fragorl. Give me a break.
urp!

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re:Functions (while...do)
« Reply #5 on: 28 Jan 2005, 16:31:24 »
Quote
Give me a break.
;D


Nit pick:
Actually, you should be able to simulate break by having an extra boolen variable in the "while" check...

Altough I really don't see the point in having one... ::)

Some external script would set a global variable true and 'brake' the while/do for some reason...
Or if the values inside "do" would contain false data...

_x = 1;
while "_x < 10 || !(brakeMywhile)"
do
{
_x = _x + 1;
_x = _x + 1;

};

or

_x = 1;
_break = false;
while "_x < 10 || !(_break)"
do
{
_x = _x + 1;
_x = _x + 1;

if (_x > 2 && _x < 3)
 then {_break=true};
};

Syntaxes not guaranteed... :P
« Last Edit: 28 Jan 2005, 16:32:20 by HateR_Kint »
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:Functions (while...do)
« Reply #6 on: 28 Jan 2005, 22:37:18 »
ok, thanks guys.
what I want to do is actually almost exactly what i used in my example.
if it works, the function i'm making will be able to find cover close to a unit
i'm going to move out either a bullet or a gl (prefferably gl but it might not work) in 8 directions from the unit (eight different ones at the same time), and check to see whether it gets displaced due to clipping, or if i use a bullet displaced as well as "dead", either way it will turn up false. then it will analyze which things got moved to tell where there is cover, and how big it is

Offline Fragorl

  • Coding Team
  • Former Staff
  • ****
Re:Functions (while...do)
« Reply #7 on: 29 Jan 2005, 02:11:09 »
I beg to differ, MachoMan! C++ certainly supports the 'break' statement, and there are many cases where it is MUCH easier to use it than to work around the problem! Anyhow, for those who dont already know, here is a basic explanation of how it would work (in C++)
Code: [Select]
Do
{
    for (j=1;j<10;j++)
         {
          if (j > 8)
               Break;
         }
if (i > 7)
    Break;
i++;
} While(i<10)
At the end of the loop, j = 9 (it is repeatedly set to 9, actually 8 times to be exact) AND i = 8. This is because the first 'break' only exits the 'for' loop, whilst the second break exits the do...while loop.

I think basically ofp functions try to simulate some aspects of C++, like the semicolon punctuation to denote the end of a statement, but that functions in ofp are parsed under C++ or whatever, which is why they don't need to be compiled. Possibly, i don't know.

Another aspect that is most definitely simultaed is 'arrays' as they are in ofp script and functions. Im *guessing* that basically they are linked lists of objects where each object contains data of type int, float, double, char, and various classes, basically all the data types you can add to an 'array ' in ofp. As you know an array in its C++ sense must consist of the same data types, and be declared before it is used in order to set aside enough memory (except in the case of the 'new' keyword, anyhow...).

Sorry Trigg, way oT :)
« Last Edit: 29 Jan 2005, 02:13:44 by Fragorl »

Offline Fragorl

  • Coding Team
  • Former Staff
  • ****
Re:Functions (while...do)
« Reply #8 on: 29 Jan 2005, 02:18:55 »
Btw, i think i might have mentioned this before, but looping 'while' in a func to wait for a bullet to die, no matter how fast it travels, will kill performance in the face. Basically, the system expends all its resources waiting for the bullet to die, and not actually moving it. A gl might work, i dont know... ;)

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:Functions (while...do)
« Reply #9 on: 29 Jan 2005, 04:31:49 »
yeah, thats why i wasn't sure if i could make it work. but i'm not waiting for the bullet to die, just to not be at the specified pos (taking advantage of OFP's horrible clipping problems). would that change anything as far as cpu usage and performance?

Offline Fragorl

  • Coding Team
  • Former Staff
  • ****
Re:Functions (while...do)
« Reply #10 on: 29 Jan 2005, 07:59:15 »
You mean setpos-ing and checking in regular intervals? I dont know, might do, might do.
A game logic probably wont work, though as i dont think it physically 'exists' in the game world sense, it probably just contains some position info and thats it. Only things that have coliision would have the clipping problem.

Try it with a soldier instead of a bullet prehaps. No I'm serious, its probably a good bet.

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:Functions (while...do)
« Reply #11 on: 29 Jan 2005, 15:25:53 »
well it can't be used in the final version, because i would look very....odd to see eight guys fly out of an ai for about 5 m