Why are Types so weird?

Blitz3D Forums/Blitz3D Beginners Area/Why are Types so weird?

Seivad Noj(Posted 2004) [#1]
Hello. I've got more of a query rather than a problem, but it's one I've been having trouble understanding for quite some time.

I understand how Types work (sort of) and their uses, but I can't understand how you access/modify any of the field values in any particular type.

For example, whenever I attempt to alter a type field's values inside a Function (a pre-existing type that already has an instance made outside of this function), it always returns the error message "Variable must be a type". If I put this in an extra "For...Next" loop, it solves it - but this seems a bit extreme to me for just altering one type field's value...

Am I doing this wrong? Is there a more efficient way of doing this? Or am I looking into it too much?

Thanks in advance for any advice on the matter.


EOF(Posted 2004) [#2]
Here is something to play with:
Type FruitType
 Field name$,price#
End Type

f.FruitType=New FruitType
f\name$="apple"
f\price=0.45
f.FruitType=New FruitType
f\name$="pear"
f\price=0.52

Print "Current Price of Pear = "+f\price
SetPrice f,0.32
Print "New Price of Pear = "+f\price
f.FruitType=FindFruit("apple")
Print "f is pointing to "+f\name$

a$=Input$("RETURN to end ...")
End

Function SetPrice(fruit.FruitType,value#)
 fruit\price=value
End Function

Function FindFruit.FruitType(fruitname$)
 For fruit.FruitType=Each FruitType
  If fruit\name$=fruitname$ Return fruit
 Next
 Return Null
End Function


It's best to imagine the .FruitType extension as the bit which tells the variable what 'kind' of variable it is.

Look at the SetPrice() function:
SetPrice(fruit.FruitType,value#)
The fruit variable is turned into a 'FruitType' just as the value is turned into a float by adding the #

Now for the FindFruit() function:
FindFruit.FruitType(fruitname$)
This returns a 'type' hence the .FruitType() part.
Again, this is just like doing:
GetFruitName$(fruit.FruitType)
In this case the function is set up to return a string because of the $ extension.

Hope that helps a little. There is a good explanation about types here:

Definitive Guide to Types


soja(Posted 2004) [#3]
...whenever I attempt to alter a type field's values inside a Function (a pre-existing type that already has an instance made outside of this function), it always returns the error message "Variable must be a type"

It sounds to me as if your type instance is not declared as a Global variable. If you don't want it to be global, another solution would be to pass the type instance into the function as a parameter.


Seivad Noj(Posted 2004) [#4]
Thanks for your help you two. I had a feeling soja's talk about passing the type instance into the function as a paremeter would work, and it did! Thanks, soja. I didn't know Types could be declared as local and global the same as variables.

I'll give Syntax Error's advice a shot though, since it'd be a waste to ignore it since he obviously spent a bit of time typing it out. Plus it'll make me feel more confident about types in general.

Thanks once again!


Anthony Flack(Posted 2004) [#5]
The types are global; the pointer to the type instance is just a variable though, and can be global or local.


Seivad Noj(Posted 2004) [#6]
Ahhhh, right! Thus the grand veil on Types is more the cleared.

Thanks Anthony.