Working with Types

Blitz3D Forums/Blitz3D Programming/Working with Types

stayne(Posted 2015) [#1]
Messing with types recently and ran into something I can't figure out. Using just one type (stripped down for simplicity):

Type Bot
Field Guard
Field Mesh
End Type

b.Bot = New Bot
b\mesh = CreateCube()
b\guard = true

b.Bot = New Bot
b\mesh = CreateCube()
b\guard = false

My question is how would you make the guard bot attack the non-guard bot if it gets close enough? I can handle the entitydistance and all. Here's what I mean:

For b.bot = each bot
If b\guard = True
If entitydistance(b\mesh,?) < 10

That's where I get lost! Man I'm getting old.


Who was John Galt?(Posted 2015) [#2]
You need a variable, say b1, that points to the second robot. Either assign your bots directly to variables b and b1, say, and don't use a loop, or have nested loops, one looping over b and one looping over b1. Then use b and b1 for your entity calculations.


Stevie G(Posted 2015) [#3]
Something like this ...

Type Bot
  Field Guard
  Field Mesh
  Field Target.Bot
End Type

Local b.bot, c.bot, CloseBot.bot
Local Dist#, CloseDist#

for b.bot = each bot
  if b\guard
    ;find the closest non guard bot
    CloseBot = null
    CloseDist# = 100000
    for c.bot = each bot
       if c <> b and (not c\guard)
          Dist# = entitydistance( b\mesh, c\mesh )
          if Dist < CloseDist
              CloseDist = Dist
              CloseBot = c
          endif
       endif
    next
    ;target found?
    if CloseBot <> null
      b\Target = CloseBot
    endif           
  endif
next   



stayne(Posted 2015) [#4]
Thanks Stevie. I'm getting an Illegal Operator for Custom Type Objects when I try "If b\Target > 0" later on in my code.


Stevie G(Posted 2015) [#5]
The check you should be doing is "If b\target <> null"


Floyd(Posted 2015) [#6]
The built in data types are numbers and strings. They have operators such adding, comparing etc.

User defined types have none of these. A comparison operator like > is undefined. The only comparison you can make is for equality or inequality. The variables of the same user defined type can be compared with = or <> to see if they refer to the same instance.

There is a special value called Null which can be compared to any user defined type variable. The test t <> Null will be True if t exists.


_PJ_(Posted 2015) [#7]
Whilst it is perhaps best to compare against "Null" for Type instances, if for whatever reason you require to check for whether the result is '0' when the instance is invalid (non-existant/deleted etc.) then you can use Handle() which will return a specific handle integer for the instance or 0 for Null instances.

If (Handle(b\Target) > 0)