Ooops
May 31, 2020, 07:35:56 PM

Author Topic: Question. Is it possible to have a type inside a type?  (Read 179 times)

Offline takis76

  • Full Member
  • ***
  • Posts: 102
Question. Is it possible to have a type inside a type?
« on: May 07, 2020, 06:15:52 PM »
Hello guys,

I liked to ask if it is possible to have a type inside a type?
Something like this:

Code: [Select]
Type type1
   Field var1:byte
   Field var2:byte

   Type type2
   Field var21:byte
   Field var22:byte

   Endtype

Endtype

type1.var1 =1
type1.type2.var21 = 2

Offline hackball

  • Jr. Member
  • **
  • Posts: 26
Re: Question. Is it possible to have a type inside a type?
« Reply #1 on: May 07, 2020, 07:15:33 PM »
Maybe only like this, but i am not sure.And you might need a create method for the scnd type too, i think.

Code: BlitzMax
  1. Type type2
  2.         Field var21:byte
  3.         Field var22:byte
  4. end type
  5.  
  6. Type type1
  7.         Field var1:byte
  8.         Field var2:byte
  9.  
  10.         Field tvar:type2
  11. end type
  12.  
Edit: Formatting doesn't work  ??? ::) 

Edit by Qube : Yes says it does ;D

Offline takis76

  • Full Member
  • ***
  • Posts: 102
Re: Question. Is it possible to have a type inside a type?
« Reply #2 on: May 07, 2020, 08:00:50 PM »
Hmm, I think it worked.
I liked to create one type with name "Party" and inside the party there will be other 3 types with names "Player1","Player2" and "Player3"


Code: [Select]
Type Pl1

Field Hp:byte

EndType
Type Pl2

Field Hp:byte

Endtype
Type Pl3

Field Hp:byte

Endtype

Type My_Party

Field Player1:Pl1
Field Player2:Pl2
Field Player3:Pl3

Endtype


Party.Player1.Hp=100

I wanted to do something like this.

Thank you very much.  :)

Offline takis76

  • Full Member
  • ***
  • Posts: 102
Re: Question. Is it possible to have a type inside a type?
« Reply #3 on: May 07, 2020, 08:29:05 PM »
Unrelated question.

How do you make the code colorful?  :P
I like it and I had asked for it in the past and I do not remember now.  ::)

Offline hackball

  • Jr. Member
  • **
  • Posts: 26
Re: Question. Is it possible to have a type inside a type?
« Reply #4 on: May 07, 2020, 08:54:13 PM »
That was Mod magic, i have no clue, my post editor doesn't show anything other than "code".

Offline Hardcoal

  • Sr. Member
  • ****
  • Posts: 454
Re: Question. Is it possible to have a type inside a type?
« Reply #5 on: May 07, 2020, 08:58:43 PM »
when i want to make a type within a type i do

Code: [Select]
   Type Math_Type
        Field Div:Divide_Type = New Divide_Type
   End Type

   Type Divide_Type
        Method Calculate(X,Y)
            Return X/Y
        End Method
   End Type

   Local Math:Math_Type = new Math_Type

   Print Math.Div.Calculate(6, 2)


Than I can Do Math.Div.Calculate(6,2)
Happy New Year

Offline takis76

  • Full Member
  • ***
  • Posts: 102
Re: Question. Is it possible to have a type inside a type?
« Reply #6 on: May 07, 2020, 09:05:23 PM »
Quote
you type Code:blitzmax .. on top.. or something

I used the tag "[code:blitzmax]" and it didn't work.

About the initial question how to have types inside types. The trick above worked but in the program logic I see the members of each type but when I am passing some value on the members of the variables when I am running the program, it says "Attempt to access field or method of Null object"

My code is:

Code: [Select]
Type My_Character1

Field Hp:Float
Field Max_HP:Float
Field HP_Percent:Float

EndType
Global Player1:My_Character1 = New My_Character1

Type My_Character2

Field Hp:Float
Field Max_HP:Float
Field HP_Percent:Float

Endtype
Global Player2:My_Character2 = New My_Character2

Type My_Character3

Field Hp:Float
Field Max_HP:Float
Field HP_Percent:Float

Endtype
Global Player3:My_Character3 = New My_Character3

Type My_Party

Field Player1:My_Character1
Field Player2:My_Character2
Field Player3:My_Character3

EndType
Global Party:My_Party = New My_Party

Offline Hardcoal

  • Sr. Member
  • ****
  • Posts: 454
Re: Question. Is it possible to have a type inside a type?
« Reply #7 on: May 07, 2020, 09:08:44 PM »
look at my above example.. tell me if thats what you wanted.
sorry i edited it and you posted before ive finished
Happy New Year

Offline takis76

  • Full Member
  • ***
  • Posts: 102
Re: Question. Is it possible to have a type inside a type?
« Reply #8 on: May 07, 2020, 09:20:10 PM »
Excellent the problem was to add the "= New My_Character1" at the end of the field line instead of outside each type.

Thank you very very much.

This one:
Code: [Select]
Field Player1:My_Character1 = New My_Character1
Field Player2:My_Character2 = New My_Character2
Field Player3:My_Character3 = New My_Character3


Offline hackball

  • Jr. Member
  • **
  • Posts: 26
Re: Question. Is it possible to have a type inside a type?
« Reply #9 on: May 07, 2020, 09:24:57 PM »
I looked at it and the tag is (Code=Blitzmax) instead of just (Code)
Anyway
as you need to have your sub-type as a part of the main type you would use:
Code: BlitzMax
  1. Type My_Character1
  2.    Field Hp:Float
  3.    Field Max_HP:Float
  4.    Field HP_Percent:Float
  5.    
  6. EndType
  7. Type My_Party
  8.  
  9.    Field Player1:My_Character1 = New My_Character1
  10.    ...
  11.    
  12. EndType
Otherwise you have that error "Error access NULL object" or something.
It is quite different to C and classic BlitzBasic, but it is somewhat logic.Also, as all players are instances of "a character" the could use the same type, "character".
Code: BlitzMax
  1. Type My_Character
  2.    Field CharacterClass:Byte       'druid, wizard, warrior,
  3.    Field Hp:Float
  4.    Field Max_HP:Float
  5.    Field HP_Percent:Float
  6.    
  7. EndType
  8. Type My_Party
  9.  
  10. Field Player1:My_Character = New My_Character  
  11. Field Player2:My_Character = New My_Character    
  12. ...  
  13. EndType

Quote
you type Code:blitzmax .. on top.. or something
I used the tag "[code:blitzmax]" and it didn't work.

About the initial question how to have types inside types. The trick above worked but in the program logic I see the members of each type but when I am passing some value on the members of the variables when I am running the program, it says "Attempt to access field or method of Null object"

My code is:

Code: [Select]
Type My_Character1

   Field Hp:Float
   Field Max_HP:Float
   Field HP_Percent:Float
   
EndType
Global Player1:My_Character1 = New My_Character1

Type My_Character2

   Field Hp:Float
   Field Max_HP:Float
   Field HP_Percent:Float

Endtype
Global Player2:My_Character2 = New My_Character2

Type My_Character3

   Field Hp:Float
   Field Max_HP:Float
   Field HP_Percent:Float

Endtype
Global Player3:My_Character3 = New My_Character3

Type My_Party

   Field Player1:My_Character1
   Field Player2:My_Character2
   Field Player3:My_Character3
   
EndType
Global Party:My_Party = New My_Party
[/code][/quote]

Offline takis76

  • Full Member
  • ***
  • Posts: 102
Re: Question. Is it possible to have a type inside a type?
« Reply #10 on: May 07, 2020, 10:24:27 PM »
This is very nice it will save me to write the same type 3 times.

I used "code:Blitzmax" instead of "code=Blitzmax"

Let's see:

Code: BlitzMax
  1. Print "Hello World"
  2.  

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal