This is a BUG?

Blitz3D Forums/Blitz3D Beginners Area/This is a BUG?

pexe(Posted 2005) [#1]
I was coding my game and i put this code
If vivo = 1 and EntityCollided(player,2)
text 0,0,"You Are Dead"
endif

The VARIABLE vivo value is 1, but it dont works, when the object collide, the message didnt apear on the screen.. then i change to this

If vivo = 1
If EntityCollided(player,2)
text 0,0,"You Are Dead"
endif:endif


And it WORKS!!! im confused i dont know how 2 codes, making the same thing can have diferent results...

This is a bug in B3D Compiler?
Im really confused..


WolRon(Posted 2005) [#2]
They should both provide the same results. Something different must be happening in your code. Are you sure that nothing else changed? Are you sure that the player did collide with a type 2 object (while vivo = 1)?


skidracer(Posted 2005) [#3]
In Blitz3D the And is a binary And not a logical And and has precedence over the logical equals. So the subexpression

1 And EntityCollided(player,2)

will be evaluated first, resulting in 1 if the handle of the entity that was involved in the collision was odd or 0 if no collision or an even handled entity was involved

The result would then be compared with the value in vivo to determine if the entire expression was true or false.

So you can't fix it just by adding brackets like this:

If (vivo = 1) and EntityCollided(player,2)

which will still fail for even handled collision sources, you need to actually do this

If (vivo = 1) and (EntityCollided(player,2)<>0)


WolRon(Posted 2005) [#4]
I see what skids trying to say. He's stating that your code is being evaluated like so:
If (vivo) = (1 and EntityCollided(player,2))
text 0,0,"You Are Dead"
endif

and not like so: (which is what you wanted)
If (vivo = 1) and (EntityCollided(player,2))
text 0,0,"You Are Dead"
endif

Of course, you could make it work by just adding the parenthesis like I did above.



I've had trouble myself sometimes using the And operator. Especially in situations with Not.
So something like this:
If this And Not that
  ;do something
endif

won't compile unless I add parenthesis like this:
If this And (Not that)
  ;do something
endif



Strider Centaur(Posted 2005) [#5]
Hmm, shouldn't If statement always be evaluated from left to write and shouldn't we have a Logical AND equivalant? If so what would it be, perhaps "&&"?


Ross C(Posted 2005) [#6]
Hmm, that helps me out too. I wasn't aware of that :D


big10p(Posted 2005) [#7]
Yes, this 'feature' had me scratching my head the other day while trying to track down a bug. I'm in the habit of just doing 'If var Then ...' when checking a variable isn't zero, instead of 'If var<>0 then ...'. This is fine until you add another check into the If statement. 'If var And var2=100 Then ...' fails when var=100 but var holds an odd value.

Definately something to keep in mind!


Dazman(Posted 2005) [#8]
Same here big10p, gets me everytime :(


_PJ_(Posted 2005) [#9]
Oh wow! If Id only known this about a year ago!!! Thanks, skidracer. kinda awkward, but great to know!


Damien Sturdy(Posted 2005) [#10]
Lol, my god that sorts about several thousand of my dead projects out!! Shoulda known this as ive been playing with binary

Il have had several hundred collision problems ;)


Drekinn(Posted 2005) [#11]
So as long as a value is specified for each expression the result will be as expected?

Eg.
If MouseDown(1) = 1 And mCheck = 1 then
    ;do as expected
Else
    ;hair-pulling and cursing ensues
End If

I always tend to specify the values in my code in these situations, but have seemingly not had any problems arise when I don't.
Thanks though for the extremely handy tidbit of information. :)


Zach3D(Posted 2005) [#12]
I noticed this bug alot, just get in the habit of not using and, just do this
{code}
if zero = 0
if one = 1
if two = 2
print 0,0," HEY!"
endif
endif
endif


jfk EO-11110(Posted 2005) [#13]
I didn't read the whole thread, just want to mention:

to prevent any troubles with logical operators and boolean algebra, use brackets!


If (condition 1) and (condition 2)

or

If (condition 1) or ((condition 2) and (condition 3))