Casting Function Arguments

BlitzMax Forums/BlitzMax Beginners Area/Casting Function Arguments

Bosco(Posted 2013) [#1]
Hi.

Is it possible to cast an argument within a function call?

In the example below the array passed to the Test function is integer, by default, which cannot be converted to the Test function's Byte array parameter.

My work-around is to use Int arrays but it feels like bad practice when the range of values I'm interested in require eight bits or less.

Cheers, Bosco.


Test ([1,2,3,4,5])

Function Test(a:Byte[])
' do stuff
End Function


col(Posted 2013) [#2]
Is your real problem not knowing how to make an array of bytes to pass in ( admittedly it is unintuitive )

' Define an array of Bytes
Local ByteArray:Byte[]

' Resize the array dynamically
ByteArray = ByteArray[..5]

' Fill the array
For Local i = 0 Until ByteArray.Length
	ByteArray[i] = i + 1
Next


'
Test (ByteArray)

Function Test(a:Byte[])
 For Local i = 0 Until a.Length
	Print a[i]
Next
End Function


Or do you really want to cast?
If you give us the bigger picture of why you want to cast, we may be able to offer another solution.


Bosco(Posted 2013) [#3]
Sure, I'll try and elaborate. : ) (Sorry, I don't know how to insert code boxes)

I have a base type called TGameObject which has very few member fields. To increase functionality I optionally add components to handle duties such as control, physics, animation etc.

Most component types have an AddComponent() function which can be passed arguments. Condensed, the TSpriteComponent looks something like this;



Type TSpriteComponent Extends TComponent

Field spriteSheet:TImage
Field frameList:Byte[] = Null
Field f:Byte = 0

Field flags:Byte = 0


Function AddComponent:TSpriteComponent(various parameters)

Method Update ()

Method Draw()

End Type



When I call the TSpriteComponent's AddComponent() function I can optionally pass it an image and a list of cel numbers, in the form of a byte array, which the TSpriteComponent's Update() method can animate.

The following lines create a new TGameObject and add a TSpriteComponent.



gameObj01:TGameObject = TGameObject.Create("Fred",160,150,150)
gameObj01.spriteComponent = TSpriteComponent.AddComponent(gameObj01,spriteSheet,[0,1,2,1,3,1],0)



Passing a previously undeclared array in this way results in an Int array so I was just wondering if there was some syntax to say this is an array of Byte values.

This isn't causing me any major headaches, I'm just curious to know if it is possible?


Yasha(Posted 2013) [#4]
This syntax will create a Byte[]:

Local a:Byte[] = [1:Byte, 2:Byte, 3:Byte, 4:Byte, 5:Byte]


The suffix needs to be present on every value, so if you simply want something concise to make your source more readable, you might achieve much prettier results by writing a simple cast function yourself that accepts an int array and recreates it element by element as a byte array. For small arrays it'd be unlikely to have any noticeable effect on performance.

e.g.

a = bytes([1, 2, 3, 4, 5])



Brucey(Posted 2013) [#5]
Like Yasha says, you can do things like this :

Test ([1:Byte,2:Byte,3:Byte,4:Byte,5:Byte])

Function Test(a:Byte[])
' do stuff
End Function 



Nice and verbose. Just the way we like it :-)


col(Posted 2013) [#6]

Nice and verbose.



:D

Its strange there's no short cut symbol for the Byte, could save your fingertips.


Bosco(Posted 2013) [#7]
Cool, thanks everyone. : )

Very much appreciated!!!