Could you help me to undestand my error !
BlitzMax Forums/BlitzMax Beginners Area/Could you help me to undestand my error !
| ||
Strict Type Path Field Id Field x[102] Field y[102] End Type Type Ship Field Id Field x Field y Field List:Tlist = CreateList() Field hImage:TImage ' ---------------------------------------------------------------------------------------------- Function Create:Ship (x,y, File$, t:Path) Local n:Ship = New Ship n.x = x n.y = y n.hImage = LoadImage (File$) ListAddLast n.List, t.id '<- Compiler : Unable to convert 'int' to 'object' ???? ListAddLast n.List, 10 '<- Compiler : Unable to convert 'int' to 'object' ???? Return n End Function End Type Hi ! Could you exlplain where is my error ! Thanks ! |
| ||
Lists must contain objects. You can't store ints in a list that way. This is what I do: Then you can do this: ListAddLast n.List, Integer (t.id) ListAddLast n.List, Integer (10) Just remember that to get their values you must cast the list objects to a TInteger and access their field X Local I:TInteger = TInteger (n.List.First ()) Local Myint:Int = I.X It's a brief version of my actual code, maybe I made a mistake somewhere. I hope it helps |
| ||
You need some kind of container that can be cast to Object, like an array or string if you don't want to make a container type. |
| ||
Thanks ! Is there a function to convert an integer to to a string ? 12 -> "12" |
| ||
@Ferminho :strict Type TInteger Field X:Int = 0 Function Create (N:Int) Local I:TInteger = New TInteger I.X = N Return I <- compiler : 'unable To convert from 'TInteger' to 'Int' End Function End Type |
| ||
Your Create function needs to return TInteger. String.FromInt(9) returns the String "9", "9".ToInt() returns the int 9. |
| ||
Type Path Field Id Field x[102] Field y[102] Function Create:Path (Id, x1,y1,vx1,vy1,x2,y2,vx2,vy2) Local n:Path = New Path n.Id = Id Return n End Function End Type Type Ship Field Id Field x Field y Field List_id_path:Tlist = CreateList() Field hImage:TImage ' ---------------------------------------------------------------------------------------------- Function Create:Ship (x,y, File$, p:Path) Local n:Ship = New Ship n.x = x n.y = y n.hImage = LoadImage (File$) ListAddLast n.List_id_path, String.FromInt(p.id) '<- ok Return n End Function Function Add_new_path (n:Ship, p:Path) ListAddLast n.List_id_path, String.FromInt(p.id) '<- ok End Function End Type p1 = Path.Create (1,0,0,0,0,0,0,0,0) p2 = Path.Create (2,5,5,5,5,5,5,5,5) s1 = Ship.Create (1,1,"image.png", p1) Ship.Add_new_path (s1, p2) Now how to display (with a for each ?) s1.List_id_path values (ie '1','2')? Thanks ! Sorry for this stupids questions, but i'm a beginner with bmax notation and List system ! |
| ||
I've change your code to have Add_new_path as a method, and p1,p2 and ship are no longer ints. Add_new_path would make more sense as a method, as you're applying it to an instance of ship; the advantage you have access to the instance without the need of passing it as an argument. Although its up to you to have p1,p2, and ship as ints; they get treated as handles and you need to convert them back to their types to access their fields and methods, I thought it would be easier just to skip the whole integer handles bit. Type Path Field Id Field x[102] Field y[102] Function Create:Path (Id, x1,y1,vx1,vy1,x2,y2,vx2,vy2) Local n:Path = New Path n.Id = Id Return n End Function End Type Type Ship Field Id Field x Field y Field List_id_path:Tlist = CreateList() Field hImage:TImage ' ---------------------------------------------------------------------------------------------- Function Create:Ship (x,y, File$, p:Path) Local n:Ship = New Ship n.x = x n.y = y n.hImage = LoadImage (File$) ListAddLast n.List_id_path, String.FromInt(p.id) '<- ok Return n End Function Method Add_new_path (p:Path) ListAddLast List_id_path, String.FromInt(p.id) '<- ok End Method End Type p1:path = Path.Create (1,0,0,0,0,0,0,0,0) p2:path = Path.Create (2,5,5,5,5,5,5,5,5) s1:Ship = Ship.Create (1,1,"image.png", p1) s1.Add_new_path (p2) For Local id_path:String=EachIn s1.List_id_path Print id_path Next |
| ||
Many thanks for your help ! |
| ||
Thanks for pointing out the mistake - I made it on the fly x) Using a string is actually better, nice idea. I think I will do it that way from now on too ;) |
| ||
If you're really picky, using arrays as a container may result in less memory usage for large multi digit numbers. Edit: For those who are interested this code compares the memory required in bytes by a string container and by an array container. Function Container:Object(a:Int) Return String.FromInt(a) EndFunction Function Container2:Object(a:Int) Return [a] EndFunction i=1000000000 Local s:String=String(Container(i)) Local ar:Int[]=Int[](Container2(i)) DebugLog SizeOf(s)+" "+SizeOf(ar) |