Question about arrays in types and string fields
BlitzMax Forums/BlitzMax Beginners Area/Question about arrays in types and string fields
| ||
I have one type with name my_doorsType my_doors Field x:Int 'x position on the map Field y:Int 'y position on the map Field frame:Int 'the sprite frame Field orientation:Int 'the door is 1=horizontal or 2=vertical Field graphic:String EndType Global doors:my_doors[41, 101] In this type I have 5 fields , 4 are integers and one is string. The code compiles correctly. When I am passing one value to my string field graphic then I have EXCEPTION ACCESS VIOLATION ERROR doors[1, 1].x = 1 doors[1, 1].y = 1 doors[1, 1].orientation = 1 doors[1, 1].graphic = "01" <-This cause the error doors[1, 1].frame = 1 How to pass a value to a string in some type array as I know you just put the value in the string with = "01". .graphic = "01" the 01 is a string is not number 1 Why do I have error when I passing string variable to an type array? All integer fields not cause error!! Thank you :) Last edited 2012 |
| ||
It may be random chance that integer sets don't cause errors. Whenever I index into an array of a type, I have to define an instance of the type (note: You only have to define it initially.. after that you can get and set values in that index all day long)... I'm still newish but I'm pretty sure defining an array of a type simply allocates an array of pointers -- these pointers don't actually point to anything valid until you assign it a new instance. In my map loading logic in my game I define the array, then loop through each element, creating a new instance and then setting the properties. doors[1, 1] = new my_doors doors[1, 1].graphics = "Potatoes!" Last edited 2012 |
| ||
doors[1, 1].x = 1 The error starts here. Turn on debug and set 'SuperStrict' at the top of your code. Useful for finding errors. ;) Global doors:my_doors = New my_doors hint :) |
| ||
The reason being you only told Blitzmax how much room you want, but havent actually created any objects to use, so, it borks:-SuperStrict Type TDoors Field x:Int 'x position on the map Field y:Int 'y position on the map Field frame:Int 'the sprite frame Field orientation:Int 'the door is 1=horizontal or 2=vertical Field graphic:String EndType Global doors:TDoors[10,10] For Local loopx:Int = 0 To 9 For Local loopy:Int = 0 To 9 doors[loopx,loopy] = New TDoors Next Next doors[1, 1].x = 1 doors[1, 1].y = 1 doors[1, 1].orientation = 1 doors[1, 1].graphic = "01" doors[1, 1].frame = 1 Print doors[1, 1].graphic Dabz Last edited 2012 |
| ||
I tried Global doors:my_doors[41,101] New my_doors Compiled But when you initialize some data in the type like doors[1,1].x=1 Unhandled Exception:Attempt to access field or method of Null object like this working but not array is included Global doors:my_doors = New my_doors doors.graphic="01" Only this worked: Type my_doors Field x:Int 'x position on the map Field y:Int 'y position on the map Field frame:Int 'the sprite frame Field orientation:Int 'the door is 1=horizontal or 2=vertical Field graphic:String EndType Global doors:my_doors[41,101] For lvl=1 To 40 For all_doors=1 To 100 doors[lvl,all_doors] = New my_doors Next Next For lvl = 1 To 40 For all_doors = 1 To 100 doors[lvl, all_doors].x = 0 doors[lvl, all_doors].y = 0 doors[lvl, all_doors].orientation = 1 doors[lvl, all_doors].graphic = "01" doors[lvl, all_doors].frame = 1 Next Next As I understood , I need to scan all my array and put "= New array_name" before use it. This take some memory pointer of the array. This worked , thank you very much. |
| ||
Right, since you have that sorted, you can clean up your code by using a Create function in your type:-Type my_doors Field x:Int 'x position on the map Field y:Int 'y position on the map Field frame:Int 'the sprite frame Field orientation:Int 'the door is 1=horizontal or 2=vertical Field graphic:String Function Create:my_doors(newx:Int = 0, newy:Int = 0, newOrientation:Int = 1, newGraphic:String = "01",newFrame:Int = 1) Local newDoor:my_doors = New my_doors newDoor.x = newx newDoory = newy newDoor.orientation = newOrientation newDoor.graphic = newGraphic newDoor.frame = newFrame Return newDoor End Function EndType Global doors:my_doors[41,101] For lvl=1 To 40 For all_doors=1 To 100 doors[lvl,all_doors] = my_doors.Create() Next Next Print doors[1,1].graphic Dabz |
| ||
I didn't know , I am able to put functions in types!! Why the create function is needed? I confused a bit. |
| ||
Yes.. Types are like "Class" in C. You can put fields, types, all kinds of goodies in it. The "Create" function isn't "needed" per-se. But, if you have a chunk of code that does a specific thing for a specific object, it is better to put that code in a function, and use that function where needed. That way in the future if you want to change something, you just have to change it in the function itself. It's also good (and expected) design. You may want to google up on object oriented programming as some people go way more into detail about it. In general, if you find yourself copy-pasting code, that code would generally be a good candidate for being in its own function. |
| ||
The "Create" method is much like a poor mans constructor, I would love to see something like this:-Type TType Field x:int Function New(newx:int) x = newx EndFunction EndType local object:TType = New TType(10) But alas, even that will be a poor variation of traditional OOP, as Blitzmax doesnt support polymorphism, and you'll be stuck with one constructer. Dabz Last edited 2012 |
| ||
Blitzmax doesnt support polymorphism BlitzMax supports polymorphism (polymorphism being the specific thing that gives it OOP - if it didn't have this, it wouldn't be OO at all!). What it doesn't support is overloading, which would let you define New (or any other method) twice with different type signatures. This is not nitpicking - this is an important distinction! |
| ||
No, your right, I should of put overloading! :) Teach me to post straight after dragging my carcass out of the scratcher! ;) Dabz |