How do Blitz3D assigns work?

Blitz3D Forums/Blitz3D Programming/How do Blitz3D assigns work?

Shifty Geezer(Posted 2004) [#1]
I had a problem where I assigned a function result to a variable, and then inverted that variable to get a division by zero. While writing a query on this forum I just twigged what might cause this and found the problem.

magnitude#=EntityDistance(source,target)
magnitude#=1/magnitude#

The above statement apparently generates a blank magnitude# variable, of 0 value, and then uses that in the 1/magnitude# expression, instead of creating a temp, performing 1/magnitude# to that temp, and copying back to magnitude#.

Are there any details on Blitz's operation that would explain this? It seems a very non-standard way of dealing with variable assigns. I don't think I know any other language that doesn't produce the result x=1/x that you'd expect.


Duckstab[o](Posted 2004) [#2]
if both Source and target are assigned as being Global Variables this Should work fine


Duckstab[o](Posted 2004) [#3]
Graphics3D 1024,768,32,1

Global cube1,cube2
camera=CreateCamera()
light=CreateLight()

cube1=CreateCube()
cube2=CreateCube()
PositionEntity cube1,0,3,10
PositionEntity cube2,2,4,10

SetBuffer BackBuffer()


While Not KeyHit(1)
Magnitude#=EntityDistance(cube1,cube2)
Magnitude#=1/Magnitude#

If MouseDown(1) Then MoveEntity cube1,.1,0,0
If MouseDown(2) Then MoveEntity cube1,-.1,0,0


UpdateWorld
RenderWorld
Text 100,100,Magnitude#
Flip
Wend
End


Tom(Posted 2004) [#4]
Blitz can use the target variable in an expression. I suspect your source and target entities are at the exact same position so the EntityDistance() returned is 0.

As you can't divide by 0, you need to add some checking before using divide:

Magnitude# = EntityDistance(source,target)
If Magnitude > 0
Magnitude = 1/Magnitude
Else
; do something else
End If

Or make a function to suit your needs:

Function div#(a#,b#)
If b=0.0 Return a#
Return a/b
End Function


Duckstab: No need to assign cube1 & cube2 as Globals in that case.

Cya!
Tom


Shifty Geezer(Posted 2004) [#5]
Hi Tom,

I was following the variables in Debug and as I said, the returned value was 110.596 or whatever it was. It definitely wasn't zero, but the subsequent 'mag = 1/mag' returned Inf. I'll have to run a test if there's a difference with assigning variables in an expression to itself within the main code and a function...

But thanks for the error-trap reminder. I'm assuming that won't happen with collision detection but I ought to have that check. I ought to have a lot more exception checking I guess!


big10p(Posted 2004) [#6]
Do you get the error every time that bit of code is run? If so, it might just be a typo, like:
magnitude# = 1/magntude#



Shifty Geezer(Posted 2004) [#7]
Can't be a typo as keeping that statement the same but replacing 'Magnitude# = EntityDistance(x,y)' with 'Magnitude# = 110.596' works.


Damien Sturdy(Posted 2004) [#8]
then ze error, surely, iz to do wiz ze pozitioning of ze entities?


Shifty Geezer(Posted 2004) [#9]
But EntityDistance() returned a real value! I've tried recreating the problem ina simple test app and failed, so either it's something really complicated or just some wierd error.

The workaround's fine and works. I might dig a bit deeper but to be frank it's not really worth fretting over.