an array of objects?
BlitzMax Forums/BlitzMax Programming/an array of objects?
| ||
hello all, I am new to max and have been away from programming for a while, just bought blitzmax and think it is wonderful. I have stumbled upon this: can I have an array of objects? for example I have a "circle" object ( using william ngan's circle intersection excellent example) and would like to make some circles using a loop like this: For t=1 To 10 Local Circ[t]:Circle = Circle.create(Rnd(300),Rnd(300) , 30) Next but this is not possible. this ofcourse works fine: Local Cir2:Circle = Circle.create(100 , 100 , 50) but I would like to be able to go through my circle collection and have a "name" for each one of them. I hope I am making my question clear. thanks very much in advance, nice to see you all. aristides. |
| ||
try: Local Circ:Circle[t] You'll then need to iterate through using Circ[t] = New Circle (with For/Next). |
| ||
thank you for your reply. I managed to make it work, but is quite awkward.. I cannot use the circle.create function any more? Function create:Circle( px#, py#, pr# ) Local Circle_:Circle = New Circle Circle_.x = px Circle_.y = py Circle_.r = pr Circle_.r2 = pr*pr Return Circle_ End Function |
| ||
It will depend what you want to do next but this worksLocal circ:circle[10] For t=0 To 9 Circ[t] = Circle.create(Rnd(300),Rnd(300) , 30) Next Type Circle Field x Field y Field r Field r2 Function create:Circle( px#, py#, pr# ) Local Circle_:Circle = New Circle Circle_.x = px Circle_.y = py Circle_.r = pr Circle_.r2 = pr*pr Return Circle_ End Function End Type If you don't want to predefine the size of circ array then use a slice to increase it Local circ:circle[] For t = 0 To 9 circ=circ[..len.circ+1] Circ[t] = Circle.create(Rnd(300) , Rnd(300) , 30) Print circ[t].x + " " + circ[t].y Next |
| ||
The Type Circle can hold the Array and the array can be resized on the fly as was mentioned as well. |
| ||
Hello. The problem is not in create The problem is in local try it: local circ:Circle[]=new Circle[......] for t=1 to 10 circ[t]=Circle.create(..... next Bye, Paposo |
| ||
thank you all for your replies. I have managed through, and learned a lot. ;-) now, on to my circle to line collision request.. :-) |