Need help on type indexing
Blitz3D Forums/Blitz3D Beginners Area/Need help on type indexing
| ||
Anyone can help.. How do we alter cetern value on spacific type without know their name. By using index for example. This is example of using DIM Dim Variable(100) For k=1 To 100 Variable(k)=Rand(1,100) Next Variable(33)=100 On the case of type I have some code like this: Type Var Field Variable End Type For k=1 To 100 a.var=New Var a\Variable=Rand(1,100) Next a\Variable(33)=100 <---This not work |
| ||
Type Var Field Variable End Type Dim a.Var(100) For k=1 To 100 a(k)=New Var a(k)\Variable=Rand(1,100) Next a(33)\Variable=100 |
| ||
Each type variable that is created has a handle which is assigned to the variable you specify - in your example, "a" is the handle, but because you re-use it each time round the loop, it will only ever point to the LAST one you created (ie number 100). You can create an array and assign each array element to the type variable, like this: type Var field Variable end type dim my_type.var(100) for k=1 to 100 mytype(k)=new var mytype(k)\variable = rand(1,100) next ; Now you can refer to each individually- mytype(33)\variable=100 So, you might ask, if I still have to use an array, why bother with types at all? Well, the first answer is that each type can hold more fields than just the "variable" you have defined. type Var field Variable field X_Coord field Y_Coord field name$ end type dim my_type.var(100) for k=1 to 100 mytype(k)=new var mytype(k)\variable = rand(1,100) mytype(k)\x_coord = rand(1,1000) mytype(k)\y_coord = rand(1,1000) next ; Now you can refer to each individually- mytype(33)\variable=100 mytype(33)\name$="Foo" mytype(62)\x_coord=123 mytype(62)\y_coord=101 mytype(62)\name$="Bar" Hope this helps! |
| ||
Oops - sorry GitTech, you beat me to it! |
| ||
Thanks guys. It's help a lot. But I still can make them dynamic enought since we use array in order to control each of them. dim my_type.var(100) is fix to 100 object. Refer to my case, I have a list of contact information that I want user to enter. All this information I put in about 10 field like name,address,.... User can add as many as possible this contact information. This is control by index variable 'TotalData' which is increase everytime user add new information. Using array, I have to say how much is the data limit. But using type, I can't access and modify them easily. How could I access the third data for example. In array just use name$(3) where 3 is the index of data that I want to access. I love type because it's dynamic but can't access its eliment fast. Or anyone know any other trick. Btw, thanks a lot for your help. |
| ||
You could have an index field and increment it by one each time you add a new object. Then you can create as many as you want. *EDIT* i.e. Type myType Field X Field Y Field Index End Type Global INDEX = 0 while not keydown(1) if keydown(203) then a.myType = new myType a\x = 10 a\y = 10 a\Index = INDEX INDEX=INDEX + 1 endif if keydown(205) then for a.myType = Each myType if a\index = DESIRED_INDEX then ;do code for desired index endif next wend end |
| ||
Look like, each time I want to change something I have to loop through all object. Any other fast method? Btw, thank a lot PERTURBATIO. I will use your code style for now. That the best for my case. |
| ||
Just make a really big array, 50000 indices or so. It's not going to take up all that much RAM these days. A few hundred KB at most. |