UDT Question (solved)

New to FreeBASIC? Post your questions here.
Post Reply
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

UDT Question (solved)

Post by leopardpm »

Is it possible to Make an array of UDT within a UDT?

Example:

Code: Select all

Type Action
    as string Name
    as ushort  TimeCost
    as ushort  EffortCost                     ' future use
    as ushort  AnimationPTR
    as ushort  Req_Skill                      ' future use
    as ushort  Req_Skill_Lvl                  ' future use
    as ushort  Req_Object_Location
    as ushort  Req_Object_Location_Distance
    
    as ushort  Req_State(3)
    as ushort  Req_State_Check(3) 
    as ushort  Req_State_Value(3)
    
    as ushort  Affected_State(3)
    as ushort  Affected_State_Change(3)
    as ushort  Affected_State_value(3)
end type
notice the 'mini arrays' within the UDT - Req_State & Affected_State?
I would like to do something like this....

Code: Select all


type test
	obj as ushort
	check as ushort
	value as ushort
end type
	
	Type Action
    as string Name
    as ushort  TimeCost
    as ushort  EffortCost                     ' future use
    as ushort  AnimationPTR
    as ushort  Req_Skill                      ' future use
    as ushort  Req_Skill_Lvl                  ' future use
    as ushort  Req_Object_Location
    as ushort  Req_Object_Location_Distance
    
    Req_State(3) as test
    Affected_State(3) as test
end type
but I don't think I am formatting it correctly and I can't find an example in the Help file as to how to do this....
Last edited by leopardpm on Dec 10, 2018 15:53, edited 1 time in total.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: UDT Question

Post by srvaldez »

sure

Code: Select all

type test
   as ushort obj
   as ushort check
   as ushort value
end type
   
Type Action
    as string Name
    as ushort  TimeCost
    as ushort  EffortCost                     ' future use
    as ushort  AnimationPTR
    as ushort  Req_Skill                      ' future use
    as ushort  Req_Skill_Lvl                  ' future use
    as ushort  Req_Object_Location
    as ushort  Req_Object_Location_Distance
    
    as test Req_State(3)
    as test Affected_State(3)
end type

dim x as Action

x.Affected_State(0).obj=1
x.Affected_State(0).check=2
x.Affected_State(0).value=3

x.Req_State(0).obj=11
x.Req_State(0).check=22
x.Req_State(0).value=33

? x.Affected_State(0).obj
? x.Affected_State(0).check
? x.Affected_State(0).value
?
? x.Req_State(0).obj
? x.Req_State(0).check
? x.Req_State(0).value
my preference is to declare all elements with the type first then the element name
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: UDT Question

Post by leopardpm »

ok... so my initial thought works

yes, I agree that I prefer the to declare all elements with the type first then the element name

or at least whichever way you choose make sure that it is consistent throughout the code (either elementType then elementName, or, elementName then elementType)
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: UDT Question (solved)

Post by Tourist Trap »

leopardpm wrote:Is it possible to Make an array of UDT within a UDT?
It's possible, but be carreful, if you redim dynamic arrays there will be contructors and destructors calls. Depending on the kind of UDT that you have as element of the array, and its size, you may want to keep with dynamic arrays of standard values rather. I'm not an expert here, but it's kind of obvious (it depends on what you put in your constructors).

Code: Select all

type udt1
    declare constructor()
    declare destructor()
        as integer _dummy
end type
constructor udt1()
    ? "hello"
end constructor
destructor udt1()
    ? "goodbye"
end destructor

type udt2
    as udt1 _array(any)
end type

dim as udt2 u2
redim u2._array(5)
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: UDT Question (solved)

Post by leopardpm »

luckily, I am too ignorant to use constructor, destructors and I hopenot to have a need for any dynamic arrays....

but thank you for the tip!
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: UDT Question (solved)

Post by dodicat »

The constructors / destructors are built in.

Code: Select all

type udt1
    as integer dummy
end type


type udt2
    as udt1 array(any)
end type

dim as integer lim=30

dim as udt2 u2(0 to lim)
'set array sizes
for n as long=0 to lim
    if n<lim\2 then
redim (u2(n).array)(0 to n)
else
 redim (u2(n).array)(0 to lim-n)
end if

'fill arrays
for m as integer=lbound(u2(n).array) to ubound(u2(n).array)
     u2(n).array(m).dummy=m^2
    next m
    
next n
'print arrays
for n as long=0 to lim
    for m as long=lbound(u2(n).array) to ubound(u2(n).array)
        print u2(n).array(m).dummy;
    next m
    print
next n

for n as long=0 to lim
    u2(n).constructor ' or destructor  to reset
next

for n as long=0 to lim
    for m as long=lbound(u2(n).array) to ubound(u2(n).array)
        print u2(n).array(m).dummy;
    next m
    'print
next n
sleep


  
Last edited by dodicat on Dec 10, 2018 22:41, edited 1 time in total.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: UDT Question (solved)

Post by Tourist Trap »

[quote="dodicat"][/quote]
I'm just testing 1.06 right today, do I miss something when I test your snippet dodi?

Code: Select all

Aborting due to runtime error 1 (illegal function call) at line 18 of C:\Program Files (x86)\FreeBasic\FBIde0.4.6r4\FBIDETEMP.bas::()
compiler commands in FBIDE are like this:

Code: Select all

"<$fbc>" "<$file>" -exx -mt
edit:
redim (u2(n).array)(1 to lim-n+1)
is better! :)
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: UDT Question (solved)

Post by badidea »

I got the same error, this at line 14 seems to solve it:

Code: Select all

for n as long = 1 to lim-1
Edit: too late
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: UDT Question (solved)

Post by dodicat »

Thanks TT
I just made the array of arrays zero based to fix it.
Post Reply