Array problem
BlitzMax Forums/BlitzMax Beginners Area/Array problem
| ||
Hi I have a problem How can I have a array as a field in a type? I have Type Tmap Field x , y Field maparray End Type then I use map.maparray = array[x,y] but its not ok pls corect me where I'm wrong |
| ||
This is how you define arrays:Type TArrays Field static:Int[32] Field dynamic:Int[] Field multi_static:Int[32,32] Field multi_dynamic:Int[,] Field arrays_within_arrays_static:Int[32][32] Field arrays_within_arrays_dynamic:Int[][] EndType dynamic arrays can be assigned a static array & other dynamic arrays, feks created with [1,2,3].. etc |
| ||
Type Tmap Field x , y Field maparray End Type map.maparray = array[x,y]1) I really would start to program with "Superstrict" as the first line of code 2) x, and y are of type Int yes? (This is not really a question) 3) Type Maparray is also of type Int Yes? Ahhh But we dont want it of type Int we want it of Type Int Array so Maparray:Int[,] Now we have said Maparry is a pointer to a Int Multidimention array. (The array does not exist, and would need "New" somwhere eg Field MAparray:Int[,] = New Int[32,32] |
| ||
I don't get it... I have Type arraytype field x,y field map_array[,] end type but when i try to acces it map_array[x,y] = somefin it sais that i tryed to acces array beyond boundry... I also tryed to do this map_array[,] = array[x,y] and doesn't work |
| ||
Since its a dynamic array your defining, it needs to contain a reference to something.. either a static array, or an array create via New. for example: Local map:Int[,] = New Int[32,32] or Local map:Int[,] map = New Int[32,32] Note thay you dont use [,] when assigning to an array (unless the assignment is at the point of declaration) |
| ||
I reffer you back to my previos answer eg Field MAparray:Int[,] = New Int[32,32] Field Maparray:Int[,] Is simple giving you a pointer to an array. NEW Actually makes the arry |
| ||
got it working but now I have another problem... map[i-1,t-1]:Ttile = New Ttile map[i-1,t-1].x = (i-1)*32 map[i-1,t-1].y = (t-1)*32 map[i-1,t-1].solid = 0 map[i-1,t-1].img = 1 er.. i don't know how to do this... could you tell |
| ||
Thats nice. Why not, just for a laugh write in like real words what it is you cannot do? I imagine its Map[i-1,t-1].img = imagearray[1] Also make a function in type TTile Function Create:Ttile(Params) local Temp:TTile = new TTile Stuff = stuff return temp endmethod map[i-1,t-1]:Ttile = TTile.create (params) |
| ||
I actualy can't do the type thig as in yes:TTile = new Ttile and yes.x = x if I try to do it with arrays |
| ||
Yes you can. Bear in mind that two hours ago you werent using array proply at all, I would sit and think about your program. And if you still have a problem. RePost it. (But dont just post a code snippit, explain it as well) |
| ||
map[i-1,t-1]:Ttile = New Ttile it said expecting expresion but encountered ":" |
| ||
Map.maparry[i-1,t-1] = New TTile or = TTile.Create () |
| ||
it worked but.. when i try to acces field... map[i-1,t-1] = New Ttile map[i-1,t-1].x = (i-1)*32 "indetifier x not found" |
| ||
Thats because you have messed it up Map.maparry[i-1,t-1] so Map[i-1,t-1] doesnt exist What is Map? An array of tiles? No. Map.x Ie each Map has ONE x and ONE y X and y really need to be part of TTile and not partof TMap If you have another problem Post TTile and TMap |
| ||
-------------------------the main code Include "types.bmx" Include "resources.bmx" Global xtiles = Int (Input("How many x tiles?")) Global ytiles = Int (Input("How many y tiles?")) Global map[xtiles , ytiles] For i = 1 To xtiles For t = 1 To ytiles map[i-1,t-1] = New Ttile map[i-1,t-1].x = (i-1)*32 map[i-1,t-1].y = (t-1)*32 map[i-1,t-1].solid = 0 map[i-1,t-1].img = 1 Next Next Graphics 800 , 600 , 32 While Not KeyHit(key_escape) For a = 1 To xtiles For b = 1 To ytiles DrawImage terrain_img , map[a - 1 , b - 1].x , map[a - 1 , b - 1].y , map[a - 1 , b - 1].img Next Next Wend -----------the types code Type Tmap Field x , y Field maparray[,] End Type Type Ttile Field x , y Field solid Field img End Type Type Tdoor Field x , y Field map:Tmap Field img End Type Type Tnpc Field x , y Field shop:Tshop Field img End Type Type monster Field x , y Field life , armor , dmg , dmgtype Field img End Type Type Tshop Field slotx , sloty Field slots[slotx , sloty] End Type Type Tinventory Field slotx , sloty Field slots[slotx , sloty] End Type Type Tplayer Field name$ Field tilex Field tiley Field x Field y Field inventory:Tinventory Field head:Titem Field torso:titem Field legs:Titem Field boots:Titem Field lhand:Titem Field rhand:Titem Field str Field agility Field dexterity Field inteligence Field charisma Field healthP Field manaP Field goldP Field img End Type Type Titem Field name$ Field kind Field class Field armor Field dmg Field str Field ag Field dex Field Intl Field char Field gold Field weight End Type |
| ||
AMap.MapArray[a,b].X exists AMap.X exists but AMap[a,b].x doesnt (Because you dont have an array of Maps) 1) [codebox][/codebox] 2) JUST Tmap and TType |
| ||
Global map[xtiles , ytiles] Whats this? An array of INTS |
| ||
map[a,b] is a global witch i use only on the mape editor here there isn't any Tmap look I just wanth a array withc is filled with objects...(Ttile) |
| ||
Gloabl Map:Ttile[Xtiles ,Ytiles] Whats this? An array of Ttiles (Well ok so it a pointer to an array of tiles. so Global Map:TTile[,] = New Ttile[Xtiles ,Ytiles] |
| ||
Global Map:TTile[,] For i = 1 To xtiles For t = 1 To ytiles Map:TTile[,] = New Ttile[Xtiles ,Ytiles] map[i-1,t-1].x = (i-1)*32 map[i-1,t-1].y = (t-1)*32 map[i-1,t-1].solid = 0 map[i-1,t-1].img = 1 Next Next Graphics 800 , 600 , 32 While Not KeyHit(key_escape) For a = 1 To xtiles For b = 1 To ytiles DrawImage terrain_img , map[a - 1 , b - 1].x , map[a - 1 , b - 1].y , map[a - 1 , b - 1].img Next Next Wend could you reconstruct the code? i can't understand... |
| ||
Graphics 800 , 600 , 32 Global Map:TTile[,] =New Ttile[Xtiles ,Ytiles] For i = 0 To xtiles-1 For t = 0 To ytiles-1 Map[i,j].set(i*32,t*32) Next Next While Not KeyHit(key_escape) For a = 0 To xtiles-1 For b = 0 To ytiles-1 Map[a,b].Draw (terrain_img) Next Next Wend Type Ttile Field x , y Field solid Field img Method set (X:Int , y:Int , S:Int = 0 , Img:Int = 1) Self.x = x Self.y = y Self.s = s Self.Img = Img End Method Method Draw (PImg:TImage) DrawImage (PImg , Self.x , Self.y, Self.Img) End Method End Type And no I havent run it. |
| ||
Hmm it doesn't work... Do you know a tutorial where I could learn about this stuff? I know how to handle types but not with arrays |
| ||
@Sanctus your sample compiles ok if you declare your map array like this: Global map:Ttile[xtiles , ytiles] the other problem I got, while running your code, is that the input function does not work. you may need to do a work around it. it appears to be another Bmax bug. but maybe it works on your computer. it definitely does not work in mine (I looked it up in the bmaxIDE and tryed the sample too and it doesn't work). Is there a modification to it I don't know about? anybody? |
| ||
@Jesse the sample code might compile, but Map:TTile[,] = New Ttile[Xtiles ,Ytiles] will just bugger it up as its run Xtiles*Ytiles times @Sactus Of corse it doesnt work Xtiles ,Ytiles and terrain_img are undefined. |
| ||
@H&K I got it to work with some minor modifications. just create or use a tile 32x32 name it tile6.png or change tile6.png to whatever tile you are using and try. He is really green, so I was trying to work with his code so he could understand it. @Sanctus no offence inteded here it is modified: |
| ||
@Jesse I wasnt having a go at you, but when someone tells me it doesnt work, and the reason is that they have neglected to give relevent data, just winds me up. (Oh And I imagine that the (i)*32 bit is supposed to be i*Xtiles, but I didnt bother to change it either) |
| ||
check again H&K the xtiles is the number of tiles across not the size of the tiles. |
| ||
Oh yes. That shuts me up. |
| ||
maybe..... any idea on the INPUT command? |
| ||
Global xtiles:Int Repeat xtiles= Int (Input("How many x tiles?") ) Until (Xtiles <> 0) I only use Input for debug data entry, so Im not sure how to change its focus. As long as you click on the console, this works |
| ||
I guess that is what my problem was. I never clicked on the console. I assumed the program automatically jumped to it. Thank's H&K. |
| ||
Try running it on BLide, after you have turned the console off. |
| ||
Wow thx guys... srry for not posting for so long but I was working on some graphics and didn't got the time to look here Anyway I'm done with them so I will try that code that Jesse remade |