-
-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to access tile data? #10
Comments
There's a few methods you can use to manipulate the objects associated to a tile |
Ah, thanks a lot, this works! |
Ahm, no it doesn't :( Here's my test program: package main
import (
"fmt"
"github.com/kelindar/tile"
)
type Cell struct {
sprite string
}
const (
height int16 = 9
width int16 = 9
)
func main() {
tl := Cell{"topleft"}
tr := Cell{"topright"}
bl := Cell{"bottomleft"}
br := Cell{"bottomright"}
vt := Cell{"vertical"}
h := Cell{"horizontal"}
l := Cell{"lawn"}
matrix := [][]Cell{
{tl, h, h, h, h, h, h, h, tr},
{vt, l, l, l, l, l, l, l, vt},
{vt, l, l, l, l, l, l, l, vt},
{vt, l, l, l, l, l, l, l, vt},
{vt, l, l, l, l, l, l, l, vt},
{vt, l, l, l, l, l, l, l, vt},
{vt, l, l, l, l, l, l, l, vt},
{vt, l, l, l, l, l, l, l, vt},
{bl, h, h, h, h, h, h, h, br},
}
grid := tile.NewGridOf[Cell](width, height)
count := 1
grid.Each(func(point tile.Point, t tile.Tile[Cell]) {
fmt.Printf("Adding cell %d on %d,%d\n", count, point.X, point.Y)
cell := matrix[point.X][point.Y]
t.Add(cell)
count++
})
grid.Each(func(point tile.Point, t tile.Tile[Cell]) {
fmt.Println(point)
t.Range(func(cell Cell) error {
fmt.Printf("Cell pos: %d,%d (%s)\n",
point.X, point.Y, cell.sprite)
return nil
})
})
fmt.Println()
} So, I create a 2d matrix of
As can be seen, 81 cells have been added successfuly but only a subset could be retrieved (25 cells out of 81). The other grid positions have no cells. I'd expect to be able to retrieve all 81 cells I put into the grid earlier. |
PS: and there's a pattern (although I can't see which): it doesn't matter how often the latter loops runs, it's always the same grid positions without a cell. So I suspect, the |
Just spent a couple hours debugging the exact same issue. @TLINDEN The pattern that your seeing is that the "state" in each "page" of 3x3 tiles is stored in a In your case, every "page" that contains more than one instance of non-unique state like I solved this by making my state unique (just added a idx value) and now the map stores the relation of state to tile index correctly because each struct is considered a unique key. @kelindar I believe a fix would be to inverse the relationship of the map from I'm happy to put a fix in if you agree with the solution. |
@g-getsov If we inverse the relationship, we could simply have |
is any progress now? |
@nneesshh I've got a working PR draft with the fix on my home desktop, but I'm AFK for the next week. I'll put it up for review first thing when I get back. |
Hi,
this is possibly a dumb question, but I'm not getting it yet :) Let's say I define grid this way:
When iterating over the grid, like this:
Of course I can create another map of cells like this:
But that way I'd have two grids, the one provided by the tile module and my "shadow" grid holding the cell data (where I'd put things like sprites, properties etc).
What am I doing wrong?
Thanks in advance,
Tom
The text was updated successfully, but these errors were encountered: