Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Tile Map, 2-D Grid movement, Arrays... How to make everything work?

Discussion in 'Scripting' started by Kaemalux, Sep 8, 2013.

  1. Kaemalux

    Kaemalux

    Joined:
    Aug 1, 2013
    Posts:
    45
    Hello! This is my first post in Unity Script Forum, and i'm almost a totally newbie user. :)

    I'll try to explain what i want to achieve, and if someone could gently help me it would be really appreciate. C# Please if possible, i'm focusing on learning that language as starting. ^^

    - The camera is orthographic, 2D, pointing on a tiled world (24x24 each tile: creatures, players, walls..)
    - The player can move only if it is his turn
    - The movement is grid-based
    - The movement shouldn't be allowed if the tile is occupied by enemy or wall

    First problem, i don't know how to set a proper array for my tile world. I mean, from zero. I understood (i hope so at least) that i store each tile data, but i dunno how to do it. Also, how to let the system know if the floor tile has a monster over it, and it isn't reachable at that moment?
    I would need really basic explanation, i tried to understand from forum posts or Unity answers, but they are already a step beyond my comprehension.
    And, the array is fixed, or while the player move i can include different tiles depending on position? I mean, if the range of action cannot be in any situation bigger than the camera, could i use the camera dimension for the array, and while the active scene "moves" even the tiles included in the array change?

    For movement, i use the C# - grid script i found in documentation, by Eric. It surely does it's job, but as you can understand i don't know how to check the tile (is it a wall, is it occupied by some npc?) so the player can move anywhere.

    If you have an alternative solution, even without Arrays, you are welcome, please just start from a basic explanation. :)

    As i'm learning, i don't need a complete script, just basis to start working it out by myself. The biggest problem is that i think i can understand parts one by one, but i don't know how to link it all together! In that case, i also need help on which-script/variable-is-linked-to-which-object in scene.

    Thanks for reading,
    Kind regards,
    Kae
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    good decision.

    if you don't mind to spend money and use a hex based map have a look into the asset store for turn based toolkit.

    also its announced that unity 4.3 will contain a new 2d "engine" but i don't know when it comes out or what it can do in detail.

    this is usually a 2d array.
    Code (csharp):
    1.  
    2. Tile[,] map = new Tile[24,24];
    3. //iterate through the array and assign a new tile to each position
    4. map[0,0].unit = player;
    5.  
    note: you could also use a one dimensional array and have a function recalculate the 2d position into an index

    simplest would be to use a class or struct to represent a tile
    Code (csharp):
    1.  
    2. public class Tile
    3. {
    4.    Unit unit; // check if there is a unit (blocked), unit is base class
    5.    TerrainType terrain; // fe an enum, could contain a wall or floor, water etc
    6.    public float walkingCost;    // set this very high for unwalkable/wall
    7. }
    8.  
    you must place the monsters/players somehow and copy a reference to the unit of a tile. so the "system" just needs to query wether unit is null or not.

    i don't understand that. you array/map is fixed size (24x24) or dynamically? it is prepopulated or filled procedurally on demand?

    don't know this. look in assetstore or google for a*.

    thats why you must query your data in the game logic and omit such tiles.

    this comes with experience and honestly is highly reduced by simply copying what someone posted. fe for your problem that player can walk everywhere you should dig into the code, write debug logs at certain positions (in code) to know what is happening. then you can identify the position where you need an additional check to prevent the player from moving there. if you want to link systems you must understand how they work. if you don't know that you can't link/change/adapt them properly. so this is always the hard work noone can take from you.

    this is highly dependend on everyones personal style (read: inexperience ;) ). and it is of virtually no help to read how others would do it. play around, tweak it, change it, try alternatives to learn the (dis)advantages and get experience and understanding. as long as it works there is no wrong way. there may be more elegant or efficient ways but there certainly is no "best".
     
  3. Kaemalux

    Kaemalux

    Joined:
    Aug 1, 2013
    Posts:
    45
    Thanks a lot exiguous for the exhaustive answer! I'll have a deep look at your advice, then i will likely be here to ask something else.
    Thanks again!
    If you have some other tip, or someone else has further explanations, i'm glad to read them!
    Regards,
    Kae

    Ps: Oh, about the line you didn't understand, i thought about a fixed tile size 24x24, but with different tile each time, like random generator things!
     
  4. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    this is not trivial as you must fe also make sure the map makes sense (ie is walkable). i would not try this from the beginning, get your things working and keep that in mind and make a comment or something on the positions you think could be altered/modified later to achieve this. you must have some way to fill/define your map and its contents. this could an editor window/custom inspector, an xml file or a seeded random function. select a tile and display its propreties to alter them.

    additional hint: i'm not sure how the tiles are handled with the tool you use. but i would avoid using a gameobject/prefab for each tile together with your tile class. instead the tile class should have a reference to the prefab and the prefab a reference to the tile class. when things are decoupled its much easier to implement something like lod where you only load parts of your map. also the more active gameobjects you have the slower unity/your game will run.
    an advanced topic (not for now) is completely removing the prefab from each tile. meshes can me merged together, the selected hex can be calculated from mouseposition etc.. but for the beginning and small maps you should stick with prefabs for each tile but disconnect them from the data to make such changes later easier.

    for the units i would have some unitmanager class having a reference to each unit (and somehow its tileposition, best if it can be queried from the "parent"tile diectly). then it has another list where all units are put inside in order you want to move them (fe initiative, speed). then you iterate over that list and remember the index of the unit which should do its turn. when the unit has finished its turn it calls a method/sends a message to notify the manager to increase the index. so a unit needs a reference to the manager (static manager class, singleton, static reference in unit, reference in unit).