Defining array size on the fly
BlitzMax Forums/BlitzMax Beginners Area/Defining array size on the fly
| ||
Hey everyone! I recently picked up Blitzmax again... I'm 18 now but I used to code in Blitzmax when I was like 11-12. Yeah, I started kinda young lol. I feel like I definitely have a better grasp on a lot of basic concepts now :P. Long story short, I've been working through iterations of a basic 2D rect-collision based engine. I'm just trying to get back into the flow of Blitzmax, learn some stuff I didn't know before, and relearn what I DID already know. The problem I've been having, though, seems ridiculously easy though that I'm PROBABLY just missing something obvious. I have a type (called TMap) which has an array as one of its fields. Fairly standard, right? But I didn't want to define the dimensions of this array until an instance of TMap has been created. I thought, "no problem, I'll just check the documentation on how to do this!" The documentation gave me, of course, this: "Arrays may also be created 'on the fly' using the syntax: New [ Dimension1 , Dimension2 etc... ] This returns an array of the specified dimension(s) with each element initialized to Null. For example: Local int_array:Int[] int_array=New Int[10]" That's perfect! So I tossed it into my code, like so: Field tilearray:TTile[]'This is located with all the other fields for TMap. TTile is another type, which is defined just after TMap in my source code. Then, in my init:TMap() method... tilearray = New TTile[xcount, ycount] I got the following error: "Compile Error Unable to convert from 'TTile Array' to 'TTile Array' " xcount and ycount are two other fields in my TMap type. Not that it matters. When I first hit the error, I started substituting both my variables and the TTile type with constant numbers and 'int', respectively. No luck. The problem comes up even when I C&P the actual example into my code. It runs fine in its own program though. So the problem's gotta be somewhere else in my code, right? Except that removing the trouble line solves the problem. What am I missing? edit: Also, if it helps any, I get an error when I try calling "map.init()" as well. It says "Identifier 'init' not found". Init() is definitely a method in my TMap type, and 'map' is definitely an instance of TMap and I definitely created it. Thanks, Nate Last edited 2011 |
| ||
you can do it like this:field tilearray:Ttile[xcount,ycount] or like this: field tilearray[,] = new TTile[xcount,ycount] |
| ||
Ok thanks, I'm not getting the error anymore. Buuuuut, I don't get why that fixed it since the documentation example didn't work in my code either. Also I'm still getting an error trying to call map.init(). map = New TMap map.init() I see nothing wrong with these two lines..... lol. Thanks! Nate |
| ||
You don't need to call the init function, didn't even know it existed! It's enough to create a new TMap, just start using! |
| ||
Init() is my own method. I use it to set up the object's properties. In this case, things like the number of tiles, assigning a TTile object to each element of the array I was having trouble with, stuff like that. But for some reason the compiler isn't recognizing the method even after I've created an instance of TMap! |
| ||
map:tmap=new tmap |
| ||
See how did I miss that? I had it before lol. Thanks guys =). Nate |
| ||
z80jim: There's already a type defined call "TMap". It's a map for mapping keys to objects provided by blitz, and unless you force your code to NOT include it using specific a "framework"/"import" it'll be included by default, meaning..map = New TMap map.init() won't work because the blitz TMap has no init() method. The easiest thing to try is renaming your TMap to TGameMap or something. |
| ||
I definitely did NOT know that. I've been using it before without any problem though... I must automatically be overriding it? This is actually my third iteration of the same general 2D concept, starting fresh each time. TMap hasn't given me any trouble yet. |
| ||
I don't think TMap has been in Blitz since the very beginning, but now it is definately part of the standard language so you'll need to name your type something else I'm afraid. |
| ||
Thanks for letting me know! :) |