Indexes, how do they work

FreshlyMintedFreshlyMinted Member Posts: 14
edited November -1 in Working with GS (Mac)
So for my game I need to make a Hex Grid which is turning out to be really difficult in GS since the actor parameters in editor all assume basically a cubic shape, but my life would be much improved if I could superimpose a index-based coordinate system on the whole shebang, trouble is, I'm not sure how to operate indexes in GS, it appears as if it is just another integer attribute so yeah; any help out there?

Comments

  • JohnPapiomitisJohnPapiomitis Member Posts: 6,256
    this is what the differnt types of attributes are. There just differnt conditions to store numbers. Depening on the value of the number depends on the type of attributee you wanna use, or if you want true or false

    Boolean - true/false values.
    Text - text values. e.g. This is a text value
    Integer - Positive and negative whole numbers. e.g. 42,-42
    Real - Positive and negative numbers which can have decimal numbers. e.g. 3.14159,-3.24159
    Angle - Positive whole numbers between and including 0 and 359.
    Index - Positive whole numbers greater or equal to 0. e.g 0 and anything greater
  • madmasseymadmassey Member Posts: 55
    Yeah, the whole hex based thing will probably be a problem, lol. But for setting up the grid itself, you probably want to put invisible entities across the whole screen... either that, or do this for the tile images themselves if they're not going to change. Then go into each actor and make two Self.Value integers... something like, Self.Index.X = 1 and Self.Index.Y = 5. So that would make that object row 1 column 5.

    Then, have something happen everytime you click one of these actors... set Game.Current.Index.X = Self.Index.X and Game.Current.Index.Y = Self.Index.Y. Then the game knows exactly where the player touched and can respond accordingly.

    That wouldn't support multi-touch too well, but then again I don't know exactly what you're trying to do with this, lol.
  • SnowSnow Member Posts: 124
    I came up with this real quick since I'm finishing some programming homework. It's a simple, probably a bit verbose ergo sloppy. You could use this with the boolean to snap pieces to even and odd rows. With a hex pattern, every second row is shifted 1 half tile width over. With this code, you could constrain the positions of pieces to index numbers. However, if you think about it, index numbers aren't necessary. There is always a concise way of telling a tile where to go and how to get there. Sometimes you may have to pass them a $5 just to get them out of your sight.

    Starting at x-80 and y-280 just as an example. Grid size is: 160X160px

    pseudo code:

    game.gridsize = 40;

    code for tiles on even lines:

    (local boolean)
    even = true;

    x positioning:

    if this._x >= 80 && this._x <= 240
    then ceil((this._x/game.gridsize)*game.gridsize)

    y positioning:

    if this._y >= 120 && this._y <= 280
    then ceil((this._y/game.gridsize)*game.gridsize)

    if even == false

    x positioning:

    if this._x >= 100 && this._x <= 220
    then (ceil((this._x/game.gridsize)*game.gridsize)) + gridsize/2

    y positioning:

    if this._y >= 140 && this._y <= 260
    then (ceil((this._y/game.gridsize)*game.gridsize)) + gridsize/2
  • MotherHooseMotherHoose Member Posts: 2,456
    Wow! that is beautiful @Snow... what a grand programmer you are!

    @FreshlyMinted

    I would say you need 3 indexes...
    1. HexPicked
    2. HexX
    3. HexY

    a controller off-screen Actor
    with [Rules] when HexPicked = #
    changeAttribute HexX to: correct #
    changeAttribute HexY to: correct #

    then make the size of the Hex area you want as an image (all the possibilities in that image)
    (it is a good idea to have a small 5x5 colored block in the center of each hex)
    for each Row the Y will be constant
    for each Column in alternating rows the X will be constant.

    in a new project import that image
    create a crossHairs Actor
    create to Display Text Actors.... one for Devices>Mouse.Position.X and one for Y
    create an Actor and add a note to it...numbering the hexes from left to right (1 to whatever)
    Preview and write down each X, Y of each hex

    then in your real project: (this is for drag-able actors... )
    Actor [Rule] All: when touch is released; when self.Position.X > ; when self.Position.X < ; when self.Position.Y > ; when self.Position.Y <
    changeAttribute HexPicked To:
    changeAttribute self.Position.X To: HexX
    changeAttribute self.PositionY to HexY

    for each Hex in your grid you will have to have the correct selectable area and write that rule.
    but you only need to do that once on one actor... then drag the rule to myBehaviors and drag to the other Actors...

    now that your are totally confused... you can try it out and try out a way for the game to control grid placement.

    so many ways to skin this cat! ;D

    MH
  • HachikoHachiko Member Posts: 330
    This is how I would do it, should work with little math, no actors at all. Just the mouse and a when mouse is down behaviour.
    Let's say you'd like to make a grid of 20x20 square, each 32x32.
    That means that, if mouse is at 64x 0y, it's in the second square. Now, if you take the 64x, and divide it by the grid width (32), you got 2. What if mouse is at...50? 50/32 = 1,56, and that's not a valid square. Just ceil it :)
    So basically
    2 variables, tileWidth, tileHeight = 32, 32;
    if mouse is released (this way the check happens when touch is released, but you can set it up for when it's down too, just use constrain instead of change attribute to keep checking)
    change attribute game.squareCoordX = ceil(mouse.x/tileWidth)
    change attribute game.squareCoordY = ceil(mouse.y/tileHeight)
    And you got the square indexes. Now, to use them to put an actor right in the middle of a block, you just have to multiply back those values. if game.squareCoordX is 2, and game.squareCoordY is 1, you multiply them by tileWidth and tileHeight.
    newPositionY = game.squareCoordY(1)*tileHeight(32) = 32.
    newPositionX = game.squareCoordX(2)*tileWidth(32) = 64.
    note that that's not the middle of the square, you still have to do subtract to that, half of the width of the tile. 64-16 = 48. That's the x center of the square 2,1.
    then subtract half of the tile height to game.squareCoordY = 32-16 = 16
    The new coordinate that the object will have are 48 for the x, and 16 for the y. Exactly at square 2,1.
    This should work flawlessly, but I came up with it now, so I can't guarantee :P Obviously, you'll have to draw the grid lines. It just lets you go without having an actor for each square (bad) or lots of if statement (kinda sloppy).
  • SnowSnow Member Posts: 124
    @MotherHoose

    That wasn't sarcasm was it? :P It's just some simple if statements or "rules" to snap pieces to specific points. Any gs user should be able to follow it. Also the last one was supposed to be:

    if this._y >= 140 && this._y <= 260
    then (ceil((this._y/game.gridsize)*game.gridsize)) - game.gridsize/2

    not "+ game.gridsize/2"

    If you still want to index, like I said you can base your index off of dividing their position by grid width or screen width or from the center.
Sign In or Register to comment.