Array problem

BlitzMax Forums/BlitzMax Beginners Area/Array problem

Sanctus(Posted 2006) [#1]
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


grable(Posted 2006) [#2]
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


H&K(Posted 2006) [#3]
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]


Sanctus(Posted 2006) [#4]
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


grable(Posted 2006) [#5]
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)


H&K(Posted 2006) [#6]
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


Sanctus(Posted 2006) [#7]
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


H&K(Posted 2006) [#8]
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)



Sanctus(Posted 2006) [#9]
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


H&K(Posted 2006) [#10]
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)


Sanctus(Posted 2006) [#11]
map[i-1,t-1]:Ttile = New Ttile
it said expecting expresion but encountered ":"


H&K(Posted 2006) [#12]
Map.maparry[i-1,t-1] = New TTile
or = TTile.Create ()


Sanctus(Posted 2006) [#13]
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"


H&K(Posted 2006) [#14]
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


Sanctus(Posted 2006) [#15]
-------------------------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


H&K(Posted 2006) [#16]
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


H&K(Posted 2006) [#17]
Global map[xtiles , ytiles]

Whats this? An array of INTS


Sanctus(Posted 2006) [#18]
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)


H&K(Posted 2006) [#19]
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]


Sanctus(Posted 2006) [#20]
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...


H&K(Posted 2006) [#21]
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.


Sanctus(Posted 2006) [#22]
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


Jesse(Posted 2006) [#23]
@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?


H&K(Posted 2006) [#24]
@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.


Jesse(Posted 2006) [#25]
@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:



H&K(Posted 2006) [#26]
@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)


Jesse(Posted 2006) [#27]
check again H&K the xtiles is the number of tiles across not the size of the tiles.


H&K(Posted 2006) [#28]
Oh yes. That shuts me up.


Jesse(Posted 2006) [#29]
maybe.....

any idea on the INPUT command?


H&K(Posted 2006) [#30]
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


Jesse(Posted 2006) [#31]
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.


H&K(Posted 2006) [#32]
Try running it on BLide, after you have turned the console off.


Sanctus(Posted 2006) [#33]
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