TypeinsideType ..what's wrong?
Blitz3D Forums/Blitz3D Programming/TypeinsideType ..what's wrong?
| ||
-------------------------------------------------------------------- Type creature Field name$ Field subent.ent End Type Type ent Field x Field y Field z End Type cr.creature=New creature cr\name="creature1" cr\subent.ent = New ent cr\subent\x=1 cr\subent\y=2 cr\subent\z=3 cr.creature=New creature cr\name="creature2" cr\subent.ent = New ent cr\subent\x=4 cr\subent\y=5 cr\subent\z=6 For cr.creature=Each creature Print cr\name For cr\subent.ent = Each ent Print cr\subent\x Print cr\subent\y Print cr\subent\z Next Next While Not KeyDown( 1 ) Wend End -------------------------------------------------------------------- why the "each loop" displays all ENT and not only the ones inside that creature? thanks |
| ||
Just change your loop to :For cr.creature=Each creature Print cr\name Print cr\subent\x Print cr\subent\y Print cr\subent\z Next You dont need to loop through all the ents - just those in the creatures. |
| ||
uhm ..I need to define more ENT in each creature..and I need to loop through them.. what should i do to take only the ENTs defined in the first creature? (p.s. how to display the code on the forum in that way? i'm new here:)) --------------------------------------------------------------------- Type creature Field name$ Field subent.ent End Type Type ent Field x Field y Field z End Type cr.creature=New creature cr\name="creature1" cr\subent.ent = New ent cr\subent\x=1 cr\subent\y=2 cr\subent\z=3 cr\subent.ent = New ent cr\subent\x=4 cr\subent\y=5 cr\subent\z=6 cr.creature=New creature cr\name="creature2" cr\subent.ent = New ent cr\subent\x=7 cr\subent\y=8 cr\subent\z=9 For cr.creature=Each creature Print cr\name For cr\subent.ent = Each ent Print cr\subent\x Print cr\subent\y Print cr\subent\z Next Next While Not KeyDown( 1 ) Wend End --------------------------------------------------------------------- |
| ||
For your need, you have to create an own linked list type structure and use that. Otherwise you have to loop through all ent and see if the owner (a field you have to add) is the type you are searching from. This is, naturally, not a that good idea on many objects. |
| ||
how to display the code on the forum in that way? Just use the tags (code) (/code) around your code but use square brakets. If its very long you can also use (codebox) (/codebox) - square brackets. |
| ||
an easy example of linked list? please :) sorry i'm a bit neewbi since i'm a graphic artist mainly |
| ||
Types can't be used in this way, but I sometimes use them the other way around: It might be slow, depending on what you need it for. You can also define an array of types: However, using this will limit the flexibility of the type. |
| ||
exactly i'm working on a virtual sea simulation. i want to have a type eg:SHARK containing all main characteristics of the creature, like the mesh, the color, speed, behaviour etc and many subtypes that are the fishes on the screen linked to that particular type(shark). They have to be types, because i have to store many parameters, like currentspeed, currentposxyz, currentanim etc... Isn't a way to iterate only through each subtype eg:sharks without any compare instruction and at the same time, continuing using types collection? type:Shark ------shark 1 ------shark 2 ------shark 3 type:Whale ------whale 1 ------whale 2 ------whale 3 sorry for my poor english :) |
| ||
So, you need to generate a lots of different kinds ? Because otherwise, you could to more or less what you describe: Type Shark field x,y,z field speed End Type Type Whale field x,y,z field speed End Type .. etc. And then, use For s.Shark = Each Shark and For w.Whale = each Whale ? |
| ||
Thats possible without problems. You just have to make Shark, Whale etc all seperate types. The other possibility is to implement your own linked list. A linked list is a quite easy structure: it contains out of containers that point to the next container and to the value they hold. For creature this would be like: Type TList field next.TList field creature.TCreature end Type And now you add a field of type TList to your structure. Whenever a creature of a given type is added you look for this creature type, go manually through the whole list and add the new instance at the end of the list. I think there is a codearchiv entry on that. If not, there are several tousand sites on the net that explain it even with nice Java animations, so you can easily understand how it works if my short description wasn't enough :) |
| ||
Say, I was thinking about if it is possible to use the type-like properties of Entities. Using the parent-child structure, you can link items very flexible. If you combine this method with types, it is possible to create types within a type: |
| ||
I need to store many parameters for each creature so i must use types.. it would be: Type CreatureDefinition field name field howmany field mesh field color field weight field behaviour field weight field maxspeed etc.. end type Type Creature field mesh field curposx field curposy field curposz field curspeed end type ;shark crdef.CreatureDefinition = new CreatureDefinition crdef\name="shark" crdef\howmany=10 crdef\mesh=loadanimmesh("shark.b3d") etc.. ;whale crdef.CreatureDefinition = new CreatureDefinition crdef\name="whale" crdef\howmany=2 crdef\mesh=loadanimmesh("whale.b3d") etc.. ;create the creatures for crdef.CreatureDefinition = each CreatureDefinition for i=1 to crdef\howmany cr.creature = new creature cr\mesh=copyentity(crdef\mesh) next next now i want many sharks and whales on the screen and each of them will have his own cr\speed, cr\posx, cr\posy, cr\posz etc... I thought it was possible to nest many types inside another type...I was wrong :) |
| ||
Try to use Blitz-arrays within objects:Type creature Field name$ Field subent.ent[5] End Type Type ent Field x Field y Field z End Type cr.creature=New creature cr\name="creature1" cr\subent.ent[1] = New ent cr\subent[1]\x=1 cr\subent[1]\y=2 cr\subent[1]\z=3 cr\subent.ent[2] = New ent cr\subent[2]\x = 4 cr\subent[2]\y = 5 cr\subent[2]\z = 6 cr.creature=New creature cr\name="creature2" cr\subent.ent[1] = New ent cr\subent[1]\x=7 cr\subent[1]\y=8 cr\subent[1]\z=9 For cr.creature=Each creature Print cr\name ; Run through all ent's within the current creature (loop through all indexes of the Blitz-array For i = 1 To 5 ; Check to see if a valid ent-object exists If cr\subent[i] <> Null Then Print cr\subent[i]\x Print cr\subent[i]\y Print cr\subent[i]\z EndIf Next Next While Not KeyDown( 1 ) Wend End Now you can loop through the Blitz-array within a type, which just holds all ent-objects for that creature. Also, in your second example: Type creature Field name$ Field subent.ent End Type Type ent Field x Field y Field z End Type cr.creature=New creature cr\name="creature1" cr\subent.ent = New ent cr\subent\x=1 cr\subent\y=2 cr\subent\z=3 cr\subent.ent = New ent cr\subent\x=4 cr\subent\y=5 cr\subent\z=6 When you declared your first creature, you were trying to add 2 objects to one field-variable. When you create an object using the New keyword, you're creating it and placing the handle (memory-address of the object) in the field-variable. When using it again on the same field-variable, you are overwriting the address of the first object with the address of the second object, leaving the first one somewhere in memory, but you don't have a reference to it, unless you loop through all the objects of that kind (ent in your example). That's called a memory-leak. You had created an object, but you've lost the reference (memory-address) to it, so it's actually floating around somewhere. So, in your example, cr\subent.ent will only hold the address to the second ent-object, and lost the address of the first one. You cannot store 2 values in the same field or variable. Like in the real world, you cannot place 2 objects in exactly the same spot, can you? |
| ||
A non 'array of types' example using nested types ... This is the kind of thing I always use and it works well. Not tested so may be a few typos in there .. sorry.Type CreatureDefinition Field name$ Field mesh Field weight# Field behaviour Field maxspeed# End Type Type Creature Field mesh Field curposx# Field curposy# Field curposz# Field curspeed# Field CD.CreatureDefinition End Type ;create definitions Global SHARK.creatureDefinition = CREATUREdefinition( "Shark", LoadAnimMesh("shark.b3d") , 10.0 , 2.0 ) Global DOLPHIN.creatureDefinition = CREATUREdefinition( "Whale" , LoadAnimMesh( "whale.b3d") , 80.0 , 1.0 ) ;create 10 sharks at random positions For NO = 1 To 10 CREATUREcreate( SHARK , Rand(-10,10), Rand(-10,10), Rand(-10,10) ) Next ;create 5 whales at random positions For NO = 1 To 5 CREATUREcreate( WHALE, Rand(-10,10), Rand(-10,10), Rand(-10,10 ) ) Next ;MAIN LOOP ETC ....... CREATUREupdate() ;==================================================== ;==================================================== ;==================================================== Function CREATUREupdate() For c.creature = Each creature ;max speed = c\CD\MaxSpeed ;weight = c\CD\Weight Select c\CD\Name Case "Shark" ;do shark specific stuff... Case "Whale" ;do whale specific stuff End Select PositionEntity c\Mesh, c\CurposX, c\CurposY, c\CurposZ Next End Function ;==================================================== ;==================================================== ;==================================================== Function CREATUREdefinition.creaturedefinition( name$, mesh, weight#, maxspeed# ) cd.CreatureDefinition = New creaturedefinition cd\Name = Name cd\Mesh = Mesh : HideEntity cd\Mesh cd\Weight = Weight cd\MaxSpeed = MaxSpeed Return CD End Function ;==================================================== ;==================================================== ;==================================================== Function CREATUREcreate( CD.creaturedefinition , curposx#=0, curposy#=0, curposz#=0, curspeed#=0 ) c.creature = New creature c\Mesh = CopyEntity( CD\Mesh ) c\Curposx = curposx c\Curposy = curposy c\Curposz = curposz c\Curspeed = 0 End Function Give me a shout if there's anything you don't understand. Stevie |
| ||
If ou can define the maximum Ent type included in the creature type, you could use the command Object to obtain what you want. let 's have a seen : |
| ||
thx everybody for the help :) |