Difference between revisions of "Talk:If"
imported>HawkFest m (→Gotchas) |
imported>HawkFest m (→Gotchas) |
||
Line 43: | Line 43: | ||
<pre>if (angleA >= 0 && angleA <= 90) || (angleA >= 180 && < 270)</pre> | <pre>if (angleA >= 0 && angleA <= 90) || (angleA >= 180 && < 270)</pre> | ||
Once my script hit this line, it stopped working completely. No indication that anything was wrong. --[[User:Mrflippy|Mrflippy]] 21:06, 15 April 2006 (EDT) | Once my script hit this line, it stopped working completely. No indication that anything was wrong. --[[User:Mrflippy|Mrflippy]] 21:06, 15 April 2006 (EDT) | ||
: That's | : That's a CS "Gotcha", but a "normal" behaviour in regards to OB's engine: you have 2 operators following each other, "&& <", without their required right or left operands. Although the CS "compiler" itself does not seem to have the required verification functionality in this regard, thus letting you package a mod with a blatant error, it will bug when playing the game. --[[User:HawkFest|HawkFest]] 17:58, 26 May 2008 (EDT) | ||
---- | ---- |
Revision as of 17:04, 26 May 2008
Do brackets help in logical operations?
IE: elseif Random > 75 && ( LilRandom == 4 || LilRandom2 == 4 )
That should be if Random is over 75, and LilRandom or LilRandom2 equals 4, but what I gather is that Oblivion will interpret it as:
if Random is over 75 and LilRandom is equal to 4, OR LilRandom equals 4.
Logically it should interpret it the prior, but with Oblivion, I'm not so sure. --MaXiMiUS 21:42, 9 April 2006 (EDT)
--JOG 15:21, 14 April 2006 (EDT)Doesn't help as far as I've tested it.
--JOG 04:51, 17 April 2006 (EDT) Didn't really read and thought you asked about AND/OR as bitwise operators. Of course brackets work but since OR always has priority over AND you need to include the AND part.
tabs and spaces
I just spent an hour searching for a bug in a script using &&:
Apparently certain combination of tabs and spaces between the two parameters cause the IF-Block to break processing of the script. No commands within the IF-Block or after the IF-Block are executed.
Here is an example:
http://home.tiscali.de/jo.ge1/tabs_working.txt
This one works fine: for each of the three objects you get a Journal entry when you activate it and can activate it as often as you like.
http://home.tiscali.de/jo.ge1/tabs_not_working.txt
The second one doesn't work: You just get the journal entry when you activate the first object, no activation, And the second and third object can't be activated at all.
--JOG 15:27, 14 April 2006 (EDT)
- This is verified that the problem in the second script is due to using spaces instead of tabs. To avoid this problem ensure that all operators, numbers, identifiers, and brackets in expressions have a space on each side of them. -- Daveh 19:24, 22 September 2006 (EDT)
- Actually you need to use the same separator on each side (either space or tab) and seperators are only necessary for a minus operator to differentiate it from a negative sign. The first example script uses 07 & & 07 and the second one 07 & & 20--JOG 03:27, 23 September 2006 (EDT)
Gotchas
The following if condition compiled (I forgot to type the last angleA):
if (angleA >= 0 && angleA <= 90) || (angleA >= 180 && < 270)
Once my script hit this line, it stopped working completely. No indication that anything was wrong. --Mrflippy 21:06, 15 April 2006 (EDT)
- That's a CS "Gotcha", but a "normal" behaviour in regards to OB's engine: you have 2 operators following each other, "&& <", without their required right or left operands. Although the CS "compiler" itself does not seem to have the required verification functionality in this regard, thus letting you package a mod with a blatant error, it will bug when playing the game. --HawkFest 17:58, 26 May 2008 (EDT)
Also,
Sometimes a runtime error will occur where:
if cond1 && cond2 ;code endif
is not evaluated correctly in scripts. If you find a similar conditional statement is not functioning correctly, try:
if cond1 if cond2 ;code endif endif
The reason for this anomaly is currently unknown.
A confusing text
The paragraph about Combining comparisons: « Note that "||" is evaluated before "&&", just like "*" is evaluated before "+" in normal algebra. » is a confusing argument, and should give an explanation for a matter of major consequences:
- In arithmetic and algebra, from the earliest use of mathematical notation, multiplication took precedence over addition. The standard order of operators is: 1-exponents and roots; 2-multiplication and division; 3-addition and subtraction.
- In terms of computing, we're talking about precedence here. The precedence is a number order, and operator precedence is usually ordered with the corresponding number order. For expressions where two operators of different precedences compete for the same operand, the operator with the higher precedence wins.
- In Common operator notation, involving "normal" alegebra or boolean algebra, "*" is evaluated before "+", it has a higher precedence number than the "+" operator. For example, 3×4+5 = ((3×4)+5), not (3×(4+5))
However, from what I've read this is not the case here. It seems that OR has a higher precedence than AND, since the article mentions the following: if myVar1 == 1 && myVar2 == 1 || myVar2 == 5 is equivalent to (if myVar1 == 1 && (myVar2 == 1 || myVar2 == 5)). Else, we'd have ((if myVar1 == 1 && myVar2 == 1) || myVar2 == 5).
Which explains why one has to be very careful in positioning conditions in a condition list of an editor item: for the CS/OB's engine, OR has order preference, has precedence over AND. For example, the condition items (A AND B OR C AND D) are evaluated as (A AND (B OR C) AND D), and not (( A AND B) OR (C AND D)), as opposed to common operator notation for most languages: we call this an inversed or negative notation. Always keep the later in mind when applying boolean algebra for evaluating some given expression, as standard operator notations will lead you to errors. --HawkFest 17:37, 26 May 2008 (EDT)