Questions about Types
Blitz3D Forums/Blitz3D Programming/Questions about Types
| ||
When you have a group of objects of a Type, I know it's possible to create a code that affects every object in a group of Types, but is it possible to write code that affects only one object in a group of types? Like how with an array, if you want to change the value of the first object in an array, you say Object(1) = x. |
| ||
Yes..you may want to be more specific though in your question. For example while looping through a list of types there is no reason why you cannot only perform an operation on one of the instances. However, I think you are perhaps more interested in if it is possible to jump to a specific type instance without looping through them. This is possible with the object and handle commands: (which are undocumented bizarrely enough given how useful they are) Type mytype Field x,y,hitpoints ;etc etc End Type For i=1 To 100 monster.mytype = New mytype monster\x=Rand(100) monster\y=Rand(100) monster\hitpoints=Rand(0,1)*10 Next ;you can process only those monsters that have a x value less than 10 for example: For monster.mytype = Each mytype If monster\x < 10 Then ;do something EndIf Next ;or if you want to jump to a specific type for example with the object handle commands ;I'll use a bank to demonstrate bank=CreateBank(0) offset=0 For monster.mytype = Each mytype ResizeBank bank,BankSize(bank)+4 PokeInt bank,offset,Handle(monster) offset=offset+4 Next ;then if we wanted to get say the 51st monster you could do this: monster.mytype=Object.mytype(PeekInt(bank,50*4)) ;first offset = 0, last offset = 99 * 4 since we've just added 100 monsters. If monster.mytype<>Null Then ;process this monster here.. EndIf Last edited 2011 |
| ||
You can use an array of types also. Create the type type MyType field x, y ;etc.. end type Think of Mytype as being a user defined variable type and define the array as MONSTER.MyType( 100 ), as apposed to a string array of MONSTER$( 100 ) or a float array of MONSTER#( 100 ). const Monsters = 100 dim MONSTER.MyType( Monsters-1 ) To create some instances of this type to assign to the array for No = 0 to Monsters-1 MONSTER(No) = new MyType MONSTER(No)\x = rand(-100,100) MONSTER(No)\y = rand(-100,100) next To assign Monster 6 with new values: MONSTER(5)\x = 0 MONSTER(5)\y = 0 |
| ||
Nice examples. Stevie G's method is the more common and easier method, but Matty's may be the purist's way and may run faster, which is often critical in games. If that's important for your game then benchmark both methods before deciding. The general theme here is that type objects can be referenced using multiple names in the same way that a person named "Watson, Jim" can also be called "Employee #53" and indexed that way. However, if you are asking whether you can add fields to just one monster, or change the datatype of a field for just some instances of a type, the answer is no. The type definition is fixed, but the values in them are individually changeable. |
| ||
It's a pity there doesn't exist a command to change the same fields of every type object. I know this is easily done via shorter code and you could code a function to do this. Still would be handy though :D |