Functions in Types

Blitz3D Forums/Blitz3D Programming/Functions in Types

Elendia Starman(Posted 2009) [#1]
Is it possible to put functions in a type so that you can call the function from within the type?

Example:
Type something
    Field x, y
    Field AlterLocation()
End Type

For s.something = Each something

    s\AlterLocation(s\x, s\y)

Next



Who was John Galt?(Posted 2009) [#2]
Not in B3D. It can be done in BlitzMax.


Elendia Starman(Posted 2009) [#3]
Is there a work-around? I'm sure there's gotta be one...


GIB3D(Posted 2009) [#4]
Why would you want to do something like that?


Ked(Posted 2009) [#5]
Type something
	Field x,y
EndType

For s.something=Each something
	s\x=newx
	s\y=newy
Next

Other than that, it can only be done in BlitzMax.


Elendia Starman(Posted 2009) [#6]
I'm currently coding my own GUI (this is for the buttons) for one of my games (Martian Chess for 4, it's on SourceForge) and it would be great to have a function encased in a type. I've actually already figured out a work-around, but I'm still interested in any possibly better ways.


LineOf7s(Posted 2009) [#7]
I don't know enough about these things to know if this answers your question specifically, but you may well get something out of this Applied Object-Based Programming with
Blitz3D
tutorial (in .pdf form) by Frank Taylor.


_Skully(Posted 2009) [#8]
I kept doing this too... unfortunately its always some kind of work-around to mimic more advanced coding techniques such as methods and functions in types/classes. I just switched up to BMax... I've had it a week and couldn't imagine going back already...

However, a function in a type is not THAT much different than having a function outside the type that is passed a type (other than polymorphism). A function in a type is not associated with the instance but rather the definition so you still need to pass it an instance of a type anyway.
Type This
  field a
End Type

Function This_Doit(t.this)
    t\a=1
End Function

compared to...

Type This
   Field a

   Function Doit(t:this)
      t.a=1
   End Function
End Type


Methods are a different story altogether...

cheers.


_PJ_(Posted 2009) [#9]
I gotta admit, I'm not sure why it would be necessary to have a function within a type,

It is possible at least, to declare Fields with initial, default values.

So maybe something could be done like:

xloc=AlterLocationX()
yloc=AlterLocationY()

NewType.Blah = New Blah

Type Blah
 Field X=xloc
 Field Y=yloc
End Type



Or
Function AlterBlahLocation(Move.Blah)
AlterLocation(Move\x,Move\y)
End Function


Or

Function AlterAllBlahLocations()
For Move.Blah = Each Blah
AlterLocation(Move\X,Move\Y)
Next
End Function


I suppose there's a reason for 'remembering' the type instance here, so perhaps an additional parameter for a function might be included making use of the 'Handle' / 'Object' commands perhaps?


Stevie G(Posted 2009) [#10]

It is possible at least, to declare Fields with initial, default values.



Not true, at least for Blitz3d.


_PJ_(Posted 2009) [#11]
Ah sorry, yes - you're right, Stevie.

Type MyType
	Field Constant%=6
	Field Variable%=n
	Field StringVar$="Hello"
End Type

Instance.MyType = New MyType


Print Str(Instance\StringVar$)


Shows that although the declaration of the fields allows for a default value, it's not retained or considered when creating a new instance.


GIB3D(Posted 2009) [#12]
Well the way I set up things is

CreatePlayer(0,0,0)

Type Player
 Field Point ; The Model
 Field X#,Y#,Z#
End Type

Function CreatePlayer.Player(x=0,y=0,z=0)
 Local a.Player = New Player
  a\Point = CreateCube()  
  
  a\X = x
  a\Y = y
  a\Z = z
  
  PositionEntity a\Point,a\X,a\Y,a\X ; Could be done using x instead of a\X etc.
 Return a
End Function



_Skully(Posted 2009) [#13]
GIA_Green_Fire_

Thats pretty much the standard way of doing things in B3D

In Max you would go


Players:TLIST=New TLIST

Type Player
 Field Point ; The Model
 Field X#,Y#,Z#

Function CreatePlayer:Player(x=0,y=0,z=0)
 Local a:Player = New Player
  a.Point = CreateCube()  
  
  a.X = x
  a.Y = y
  a.Z = z
  
  PositionEntity a.Point,a.X,a.Y,a.X
  Players.AddLast(a)
  Return a
End Function

End Type

a:Player=Player.Create()




Malice,

Functions in types allow you to encapsulate a module without risking function duplication when someone imports your mod... Saves having to prefix the functions with the module name as you have to do in B3D