Search Unity

  1. How can our website serve you better? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

Grid Framework [scripting and editor plugins]

Discussion in 'Assets and Asset Store' started by hiphish, Jul 24, 2012.

  1. abDev

    abDev

    Joined:
    Feb 10, 2014
    Posts:
    5
    Greetings,
    first of all congratulations for the great tool. I have already implemented some solutions saving considerable time ... I'm stuck on a problem and I'm not sure if a solution is possible or not. Would it be possible to implement the snake game on an uneven surface (terrain like) using a grid as a movement controller? I do not need the exact solution, just to know if you think it is possible and, if you like, the idea behind it. I had thought to project vectors on plane, but does not seem functional (maybe I just failed to implement that type of solution).
    Thank you
     
  2. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    Hi,
    It should be possible if you only care about the X- and Y coordinate, something along the lines of:
    Code (csharp):
    1. GFGrid grid;
    2. Vector3 position;
    3.  
    4. Vector3 gridPosition = grid.WorldToGrid(position);
    5. gridPosition.x = Mathf.Round(gridPosition.x);
    6. gridPosition.y = Mathf.Round(gridPosition.y);
    7. Vector3 roundedPosition = grid.GridToWorld(gridPosition);
    8.  
    9. position.x = roundedPosition.x;
    10. position.y = roundedPosition.y;
    Basically do your gameplay rules in grid space and then only apply two coordinates to the world model. If you only need the direction vectors and you have a rectangular grid you can do it even easier:
    Code (csharp):
    1. GFRectGrid grid;
    2. Vector3 right = grid.right;
    3. Vector3 forward = grid.forward;
    I hope that's what you were looking for.

    PS: If you want you can show me the games you have made with Grid Framework to be be featured on the Showcase page. You can send me a PM if you don't want to use the thread.
     
  3. olszewskim

    olszewskim

    Joined:
    Oct 26, 2015
    Posts:
    11
    Hello again me :)
    I have a question. It is possible to show grid in build (WebGL)? I see option Hide On Play but i have it unchecked.



    But when I click play grid disappear:


    Any solution for this? Can I show this grid without editor (in build)?
     

    Attached Files:

  4. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    Hello again :)
    Did you add the GFGridRenderCamera script to your camera? You can find it in the menu bar under Component -> Grid Framework -> Camera. See also the user manual chapter Rendering and drawing a Grid.

    To give you some context, Unity has essentially two ways to render things: using meshes and shaders, which is the usual way, or the GL class for direct access to the rendering system. I have chosen the latter because it is easier to make changes on the fly (otherwise you would have to build a new mesh after the slightest change), but the downside is that it won't be automatically handled by cameras. I was considering the mesh approach, but then I wouldn't be able to see the grid in the editor. Plus, Vectrosity is much better at it, to achieve such good results I would have to basically re-implement Vectrosity and that means I would have to also charge for Grid Framework as much as it costs now plus what Vectrosity costs. That's a waste of your money and my time when you could just as well buy the two products separately instead. This is why version 2.0 will retain the same rendering system.
     
    olszewskim likes this.
  5. olszewskim

    olszewskim

    Joined:
    Oct 26, 2015
    Posts:
    11
    Oh I forgot about this script. Now it's work correctly. Thanks for help and fast response!
     
  6. Silly_Rollo

    Silly_Rollo

    Joined:
    Dec 21, 2012
    Posts:
    499
    If you are working on a major rewrite the folder structure is a bit of a mess. Put absolutely everything in a folder named "Grid Framework" and don't throw files all over the project. This is the standard and there's not many good reasons to deviate from it.

    Especially the docs shouldn't be in a random folder and I think you should bundle those in a .unitypackage that is optional to unzip anyway. A website link is preferable for anyone with a net connection rather than cluttering up a project with offline access.
     
  7. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    Everything has to be in a sub-directory of Assets/Plugins, otherwise other scripts in that directory won't work, so that has to stay. The Editor scripts can be moved in there, that will be be changed. The main Assets/Grid Framework directory is meant to be a "throw it away if you don't want it" kind of thing, it is not needed to run. I put it outside the Plugins folder for that reason, but I might consider moving it back in.

    The documentation directory is a thorn in my side as well, but it has to be that way: the documentation is generated by Doxygen and uses JavaScript files. Unity decided to market their UnityScript as JavaScript as well because JS used to be all the rage back in the day, so they chose "js" as the file extension. If I place the documentation anywhere else Unity will try to compile the actual JavaScript files and throw a million errors. Only the WebPlayerTemplate directory will be ignored by the compiler.

    If you want you can throw the local documentation away and the Help menu item will take you to an online documentation instead. I won't stop shipping an offline documentation, I cannot expect clients to be online all the time; even Unity ships with offline documentation for that reason. A ZIP or Unity package would be an option, but how are people supposed to find it? I can't expect them to go on a wild chase through the directories to find the help files.
     
  8. Silly_Rollo

    Silly_Rollo

    Joined:
    Dec 21, 2012
    Posts:
    499
    Have you glanced at what other Asset Store packages do? People stick to a folder and I've never seen another package that even dumps the whole docs by default. Separate Doxygen docs are just an annoyance for anyone with an proper IDE anyway and it's not a wild hunt for zips if *everything* is in one directory.

    You also may not be aware you can put the Editor scripts in any /Editor folder and it doesn't need to be in the root /Editor. It works just like Resources that anywhere in /Asset works if it is named to what Unity expects.

    It only needs to be in the Plugins directory for some communication with US and you are better off doing what A* Pathfinding Project does (which is about a gold standard for Unity Assets as you can get) and make US/JS access optional and move files when people need it (which isn't often). You could also do what Vectorisity does and actually compile a plugin and give the source as an optional package. Either way its a bit odd to stick non compiled scripts into /Plugins.
     
  9. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    I have used the A* Pathfinding plugin and because it was not in a subdirectory of Plugins it didn't work for some scripts.

    I like what the source release of Vectrosity does:
    Code (csharp):
    1. |-Assets
    2.   |-Plugins
    3.     |-Vectrosity
    4.       |-Editor
    5.       |-...
    It's all contained within one directory and it works with all scripts. Would that be an acceptable compromise? Placing everything in one directory and telling people to move files around means they have to re-do it after every update and it was the way how Grid Framework was originally published and people did not like that either. You can never make everyone happy.
     
  10. Silly_Rollo

    Silly_Rollo

    Joined:
    Dec 21, 2012
    Posts:
    499
    That's fine though needing to be in plugins is *only* necessary for certain issues with Unityscript and c# is completely indifferent to script locations. That's why A* requires you to change some options if you insist on using something besides c# but with c# it runs perfectly ootb.
     
  11. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    That's precisely why I did it. I used get to a lot of questions whether UnityScript was supported before writing the FAQ.

    Unless your C# script is in the Plugins directory, which is needed if you want to use it with UnityScript.
     
  12. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    Namespaces for everything
     
    Tinjaw likes this.
  13. ProtoPoly

    ProtoPoly

    Joined:
    Jan 5, 2014
    Posts:
    32
    I am going to be forced to abandon use of this library because I cannot figure out how to use it as an amateur programmer.
     
  14. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    What is the problem? Maybe I can help you get started, what are you trying to accomplish? Just in case you missed it (some people do), the user manual and API documentation are under Help -> Grid Framework Documentation in the Unity Editor menu. There are also little example games in Assets/Grid Framework/Examples for practical demonstration. They are also listed on the website, maybe those explanations work better for you.
     
  15. Onyxius

    Onyxius

    Joined:
    Aug 26, 2013
    Posts:
    8
    Hello,
    I was curious, i saw in one of the previous notes that adding a rotate would auto snap to grid as well. I created a 2x1 (scale) block and moving the block around snaps to grid just fine, but as soon as I rotate the block using a script, it snaps to the line instead of the center of the block. Could it be the script i'm using? This is what I am using for the rotate script. Also i'm on the latest version of unity 5.3.2f1. I was just thinking, is it because its rotating on the center of the block? If so how do i change where the center rotation is? I'm ultimately going to be creating a prototype battleship game.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RotateShip : MonoBehaviour
    5. {
    6.  
    7.     public float rotationAmount;
    8.     public float rotationSpeed;
    9.     public Vector3 destEuler = new Vector3(0, 0, 0);
    10.     private Vector3 currEuler = new Vector3(0, 0, 0);
    11.  
    12.     // Use this for initialization
    13.     void Start()
    14.     {
    15.  
    16.         rotationAmount = 90.0f;
    17.         rotationSpeed = 99.0f;
    18.         transform.eulerAngles = destEuler;
    19.     }
    20.  
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.  
    26.         if (Input.GetKeyDown(KeyCode.RightArrow))
    27.         {
    28.             destEuler.y += rotationAmount;
    29.         }
    30.         else if(Input.GetKeyDown(KeyCode.LeftArrow))
    31.         {
    32.             destEuler.y -= rotationAmount;
    33.         }
    34.         currEuler = Vector3.Lerp(currEuler, destEuler, Time.deltaTime * rotationSpeed);
    35.         transform.eulerAngles = currEuler;
    36.  
    37.  
    38.     }
    39. }
    40.  
    41.  
    42. I also imported a ship and resized it.  Whenever I move the ship, it snaps to the grid line not the grid box.
    43.  
    44.  
     
    Last edited: Feb 2, 2016
  16. pixelmonk

    pixelmonk

    Joined:
    May 25, 2011
    Posts:
    8
    Hello.
    Just purchased Grid Framework 1.9 and I do not have the menu item "Component -> Grid Framework -> Toggle Playmaker actions". I'm using Playmaker 1.7.8.4 and Unity 5.2.1. Earlier threads with instructions to modify scripts no longer seem valid (lines in the GF scripts I have do not contain the same code). What am I missing?
     
  17. pixelmonk

    pixelmonk

    Joined:
    May 25, 2011
    Posts:
    8
    Okay, I just looked through the GF scripts and found "GFPlayMakerPresenceCheck", but the entire script was commented out. So I uncommented it, but now I get these errors:
    Assets/Editor/Grid Framework/GFPlayMakerPresenceCheck.cs(15,37): error CS0117: `GFMenuItems' does not contain a definition for `ReplaceInFile'
    Assets/Editor/Grid Framework/GFPlayMakerPresenceCheck.cs(17,37): error CS0117: `GFMenuItems' does not contain a definition for `ReplaceInFile'

    Please help. How do I get Grid Framework Playmaker actions working?
     
  18. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    Hi, you should check the user manual: Help -> Grid Framework documentation. The manual is in HTML, click on the chapter Supporting other Unity plugins (either main page or side bar), there you will find the current instructions. It looks like I forgot to update the chapter for Playmaker with the new instructions.
    The symbol for Playmaker is GRID_FRAMEWORK_PLAYMAKER. Sorry for that oversight. There should be no need for you to edit any scripts.
     
  19. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    Hello,

    The snapping code is really not the best, it will get a good reworking in version 2.0 for certain. Here is how it works: there are three separate coordinates, each one is regarded independently, thus we can limit ourselves to one dimension. Take a look at the following drawing:
    Code (csharp):
    1.  x        x-x-x      x-x-x-x-x    
    2. |-|-|    |-|-|-|-|  |-|-|-|-|-|
    3. 0 1 2    0 1 2 3 4  0 1 2 3 4 5
    Here we have aligned a "block" of odd width by placing its centre between two lines. This rule applies to all odd numbers. But what about even numbers? The centre must be placed on a line:
    Code (csharp):
    1. x-x      x-x-x-x    x-x-x-x-x-x
    2. |-|-|    |-|-|-|-|  |-|-|-|-|-|
    3. 0 1 2    0 1 2 3 4  0 1 2 3 4 5
    It's hard to explain in an ASCII drawing because the lines have the same width as the gaps between them, but hopefully you get the idea.

    So what is happening? You object has size 2x1, so the alignment method uses the second rule for the X-coordinate and the first rule for the Y-coordinate. This looks fine as long as your block has either no rotation or 180° rotation, but it's wrong for 90° and 270°.

    How can we solve it? We would somehow need to change to scale of the block from 2x1 to 1x2. Of course that's impossible without messing your object up, but we can use another method for snapping: AlignVector3(position, scale):
    Code (csharp):
    1. Transform tr;  // Transform of your block
    2. bool horizontal = true;  // 0° or 180°
    3. GFRectGrid grid;  // the grid
    4.  
    5. Vector3 scale;
    6. if (horizontal) {
    7.     scale = new Vector3(2, 1, 1);
    8. } else {
    9.     scale = new Vector3(1, 2, 1);
    10. }
    11.  
    12. tr.position = grid.AlignVector3(transform.position, scale);
    13.  
    I can see another problem with your script, I don't know if you have fixed it otherwise, but when you rotate an object the pivot point is the centre of the object, so even without snapping your ship will end up on a grid line. I don't know how your game works, but my guess is that you want the ship to turn in place, right? Then you should move the pivot point to either the middle of the first or last square. Get the rotation working first without snapping, the use the snapping code above.
     
  20. pixelmonk

    pixelmonk

    Joined:
    May 25, 2011
    Posts:
    8
    Thanks for replying, but I tried to follow your instructions and it is still not working. Please see the screenshot. I'm not a coder (that's why I'm using Playmaker), so I'm sure I'm doing something wrong. Can you please offer more idiot-proof instructions? In your manual for enabling support for Playmaker, you have this code:
    #define SOME_SYMBOL
    // ... some time later
    #if SOME_SYMBOL
    // this code will be compiled
    # else
    // this code won't be compiled
    #endif

    Please don't assume I'm going to know what to put on the line that says "// this code will be compiled".

    Thanks for your help. GridFrameworkPlaymakerProblem.jpg
     
  21. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    I'll try to give some context. Some languages have a thing called a preprocessor, a mechanism that can control how your code is compiled. This is what the #if whatever directives do: if the whatever symbol is defined all the code between the #if and the corresponding #endif is compiled, otherwise it is skipped. The code might be complete garbage, if it is skipped the compiler will pretend it doesn't exist.

    So how do we define a symbol? I left it open because there are multiple ways in Unity and people might have different needs, see their manual for all possibilities.
    http://docs.unity3d.com/Manual/PlatformDependentCompilation.html

    The easiest way in my opinion is to define the symbols globally (i.e. for every build), it's explained at the very end of the page. Here is a TL;DR: create a file called smcs.rsp in you Assets folder. That's a regular plain text file with the extension rsp, not rsp.txt or rsp.cs. If you create the file through Unity be aware that Unity hides the file extension, so double-check in your file browser. I can see in your screenshot that your actual file extension is rsp.cs. The contents of the file are
    Code (csharp):
    1. -define:GRID_FRAMEWORK_PLAYMAKER
    That's with a leading dash and without spaces.

    Now all you need to do is re-run the compiler so it compiles the Playmaker actions script without skipping its content. Just create some new script and hit the Play button, that should get Unity to re-compile everything. You can then delete your script again.
     
  22. Azmar

    Azmar

    Joined:
    Feb 23, 2015
    Posts:
    246
    Hello, I am interested in your asset but I have a few questions. I would like to create a 3d game where it is like 10x10x5 (width, length, height). Here I will create 3d objects for example like a stool ( take up 1 block), a table (2x1) and carpet (4x4) on top of the floor. I would like to also set it where you can't build on top of the stool, but you can build on top of a carpet for example ( during runtime, like add a stool on top of a carpet). I would also like to have painting etc on the walls on like the upper height spots too, etc.

    Also would like to know which spots of the grid are occupied so I can have like people walk around etc.

    Am I able to accomplish this with this asset? Oh and most importantly needs to work with mobile (android), does it run nicely on the android devices?
     
  23. ageana

    ageana

    Joined:
    Nov 6, 2013
    Posts:
    23
    Has anyone used Grid Framework with a Scroll View (Unity UI) to create an endless grid?
     
  24. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    It works well on mobile devices, people have been using it without issues for years.

    Now for your other question: there is no data stored in the grid, you have to do it yourself like this:
    Code (csharp):
    1. class MyGridDataType;  // your type
    2. GFGrid grid;  // the grid you want to use
    3.  
    4. // amount to data
    5. int width = Mathf.FloorToInt(grid.renderTo.x) - Mathf.CeilToInt(grid.renderFrom.x);
    6. int height = Mathf.FloorToInt(grid.renderTo.y) - Mathf.CeilToInt(grid.renderFrom.y);
    7. int depth = Mathf.FloorToInt(grid.renderTo.z) - Mathf.CeilToInt(grid.renderFrom.z);
    8.  
    9. // An array for your data
    10. MyGridDataType[width, height, depth] = new MyGridDataType[width, height, depth]();
    Now you can store any data you want in that array. The array coordinates of a datum correspond to the grid coordinates if the grid renders from (0,0,0) (in local coordinates, not world coordinates). Otherwise use this to convert between grid- and array coordinates:
    Code (csharp):
    1. // Array to grid is similar, except we add instead of subtracting
    2. void GridToArray(ref int x, ref int y, ref int z) {
    3.     x -= Mathf.CeilToInt(grid.renderFrom.x);
    4.     y -= Mathf.CeilToInt(grid.renderFrom.y);
    5.     z -= Mathf.CeilToInt(grid.renderFrom.z);
    6. }
    In a very simple example MyGridDataType could be a simple bool: if the cell is occupied set it to false, otherwise to true. How do you mark cells as occupied? I would use a function that is part of your object (chair, carpet). The carpet function would be simply nothing, while the chair would mark all the cells it occupies from floor to ceiling as occupied:
    Code (csharp):
    1. // for the carpet
    2. void OccupyCells(MyGridDataType data) {
    3.     return;  // doing nothing
    4. }
    5.  
    6. // for the chair
    7. void OccupyCells(MyGridDataType data) {
    8.     int lowerX = GetChairLowerXCoordinate();  // some simple math, see included examples
    9.     int lowerY = GetChairLowerYCoordinate();
    10.     int upperX = GetChairUpperXCoordinate();
    11.     int upperY = GetChairUpperYCoordinate();
    12.     int height = data.GetLength(2);  // taken from the array itself
    13.  
    14.     for (int i = lowerX; i <= upperX; ++i) {
    15.         for (int j = lowerY; j <= upperY; ++j) {
    16.             for (int k = 0; k < height; ++k) {
    17.                 data[i,j,k] = OccupyCells.Occupied();  // whatever you chose
    18.             }
    19.         }
    20.     }
    21. }
    22.  
    In reality you will want more than just a simple bool, like a mask that makes the block occupied for some entities like people or furniture, but unoccupied for others, like hanging lights. To prevent objects from being placed you can either look at my runtime snapping example or just create an invisible collider, which ever you think looks better.
     
  25. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    You mean this?
    http://docs.unity3d.com/ScriptReference/GUI.BeginScrollView.html

    Grid Framework is world-space only, GUI space is an entirely different domain. People have been using Grid Framework to make their own GUIs, but that has to be done in world-space by positioning objects and cameras in the scene. If I make a GUI-space solution in the future that will be a separate project.
     
  26. ageana

    ageana

    Joined:
    Nov 6, 2013
    Posts:
    23
    More like this http://docs.unity3d.com/ScriptReference/UI.ScrollRect.html. I am trying to modify the "Seeming Endless Grid" example to make it work by dragging the grid/camera and not by using the keyboard.
     
  27. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    Do you actually want to use the GUI system or do you just want to drag the scene? From what I understand the UI classes are only for creating GUIs where you specify what UI elements to draw and those get rendered to the screen, separate from world space. Communicating back to other scene objects looks very cumbersome. If you just want to move the scene camera you can use Input.GetMouseButton and Input.mousePosition.
    http://docs.unity3d.com/ScriptReference/Input.GetMouseButton.html
    http://docs.unity3d.com/ScriptReference/Input-mousePosition.html
     
  28. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    Keeping the renderers updated and efficient
     
  29. CraigGraff

    CraigGraff

    Joined:
    May 7, 2013
    Posts:
    43
    There are a number of coordinate systems associated with GFHexGrid. Is one of them appropriate for tracking coordinates of a circle style hex grid?
     
  30. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    I'd say the cubic coordinate system because it uses three axes with 120° between them. Rhombic coordinates are biased towards one direction.
     
  31. tal1m0n

    tal1m0n

    Joined:
    Jun 9, 2013
    Posts:
    5
    Hey, the grid system looks great. Apologies for the noob question.

    I'm using playmaker, simplying trying to get tile by tile movement;
    First get position of game object,
    Convert to grid space somehow based on it's location?
    Use Get.Forward and Get.Up that somehow correlates to the physical object....

    Could you possibly give a quick explanation on how to translate positions between world space and grid
    for pm users?
     
  32. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    Hi,

    It depends on what you want. If you only want the forward/right/up vector you can use Get.Forward/Up/Right, they are in world-space. So if you have a board game and you want to move a unit five units right you run:
    Code (csharp):
    1. transform.position = transform.position + 5 * grid.Right;
    Except with Playmaker actions instead. These three vectors are just the usual Transform.Right/Up/Forward vectors that Unity provides scaled by the grid's spacing.

    If you want more control you will have to convert to grid coordinates, do your game logic and then convert back to world coordinates to move your object. Let's assume you have a board game with a complex set of rules expressed in grid-space. Then you would do the following:
    Code (csharp):
    1. Vector3 gridPosition = grid.WorldToGrid(transform.position);
    2. // Here is assume you have a function that takes in a position
    3. // and returns a list of coordinates
    4. List<Vector3> steps = GameRules(gridPosition);
    5. foreach (point in steps) {
    6.     point = grid.GridToWorld(point);
    7. }
    I don't know if you can do generic lists in Playmaker, but that's the basic idea: convert to grid-space, simulate, convert back to world-space and then move.

    A third idea is to forfeit the conversion from world to grid and instead run your entire logic just in grid space. At the end of every "simulation" of your game rules you convert the result to world-space to set the position of user-visible models. This is know as the Model-View-Controller (MVC) pattern.
    https://en.wikipedia.org/wiki/Model–view–controller
     
  33. ageana

    ageana

    Joined:
    Nov 6, 2013
    Posts:
    23
    It would be great to have a dedicated forum for Grid Framework, it's very hard to search all the pages in this topic.
     
  34. ageana

    ageana

    Joined:
    Nov 6, 2013
    Posts:
    23
    I am trying to modify "Seeming Endless Grid" example to create a simple scrolling map, my question is how can I add GameObjects(sprites) to the tiles? I looked in the other examples and all the game objects are already placed inside the grid.

    What would be the best method to dynamically 'populate' the grid with graphical tiles (like in the image bellow)? And how do I add interaction with them? How to create the connection between the grid and the graphical tiles?

    Thank you!

     
  35. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    Hi,

    How are you keeping track of your game? For example you could store the game's state in an array:
    Code (csharp):
    1. Tile[widh, height] gameTiles;
    where Tile is your class for tiles. Then to instantiate a tile in the word you would run this:
    Code (csharp):
    1. int x, y;  // position of the tile in the model-grid
    2. GFHexgrid grid; // your grid in the world
    3.  
    4. Vector3 worldPosition = grid.HerringUpToWorld(new Vector3(x, y, 1));  // position of the tile in the world
    5. // Place the tile in the world and in the array
    6. gameTiles[x, y] = Tile.InstantiateAt(worldPosition);
    7. // where Tile.InstantiateAt is some method of your tile class
    There is no "connection" between the grid and the tile object in the world, the grid is a coordinate system. All "connections" are implicit. In the above example the array position of the tile give you the model-coordinates. You can also infer the model coordinates from the world position of the tile:
    Code (csharp):
    1. Vector3 worldPosition;  // coordinates of the tile in wold-space
    2. Vector3 gridPosition = grid.WorldToHerringUp(worldPosition);  // Herring-coordinates
    3. int x = Mathf.RoundToInt(gridPosition.x);
    4. int y = Mathf.RoundToInt(gridPosition.y);
    Or you could store those coordinates explicitly inside your tile, but then you have two extra variables you need to keep updated when moving tiles around if that is a concern for your game.

    Using the array has the advantage that you can write all your game logic in discrete grid-based logic. If you want to make a tactical game for example (think Fire Emblem) and you want to move a unit across some tiles you can write the logic in your discrete model and only after the path has been set you concern yourself with world-space to move the player sprite smoothly along the path. This is known as the Model-View-Controller pattern.
    https://en.wikipedia.org/wiki/Model–view–controller


    A forum raises legal problems: I would have to either let anyone post anything or moderate it fully. If I let people post anything I cannot be held legally responsible for the content (unless it is downright illegal), but then the forum would basically turn into 4chan, which is not a place you go to if you are looking for serious information. If I decide to moderate it even slightly I am fully responsible for all the content, so I would have to moderate it full-time. I am quite frankly not making enough money from Grid Framework to hire full-time moderators and I don't know anyone who would be willing to moderate on a purely voluntary basis.

    It's easier if you just ask me a question and if enough people are asking the same thing I can put it in the FAQ. I try to answer every question within 24 hours, but there might be delays due to time zones.[/QUOTE]
     
  36. hexaust_

    hexaust_

    Joined:
    Mar 7, 2015
    Posts:
    23
    Thanks for your replies, I will give it a try and come back with the results. Btw, does the "Grid Debugger" or the "Hex Conversion Debugger" work? All that I get is 0. I tried adding them on the Lights Off example and Seeming Endless Grid.



    Also, how can I get the size of the grid, I tried grid.size but it's always 1? I am using as a base the "Seeming Endless Grid" example, and what I am trying to do now is to render a Tile (gameObject) for each cell, but how do I know how many cells are currently displayed? And how/when do I update the new cells as the grid size modifies?

    Thank you very much!
     
    Last edited: Feb 19, 2016
  37. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,109
    Hi, I'm new to the Grid Framework and maybe I just missed the obvious, but I cannot for the life of me find the most simple and useful function anywhere in the examples or documentation:

    Find which grid/element was clicked on by the user. The Lights Off example gets close, and I would have expected that in place of the isAdjacent() method there is something like an isHere() method, but it doesn't exist, none of the GFGrid class functions return a game object, and I can't believe something so simple takes more than one line of code. What am I missing?
     
  38. hexaust_

    hexaust_

    Joined:
    Mar 7, 2015
    Posts:
    23
    OK, I tried this for populating the endless grid, and it almost works (only if relative size is checked...)

    Code (CSharp):
    1.  
    2. for (var i = (int)(grid.renderFrom.x); i <= (int)(grid.renderTo.x); i++) {
    3.   for (var j = (int)grid.renderFrom.y; j <= (int)grid.renderTo.y; j++) {
    4.     var worldPosition = grid.NearestFaceW(grid.GridToWorld(new Vector3(i, j)));
    5.     var go = Instantiate(tilePrefab, new Vector3(worldPosition.x, worldPosition.y, 2), Quaternion.identity);
    6.     go.name = "[" + i + " : " + j + "]";
    7.    }
    8. }
    9.  
    If I uncheck Relative Size it creates extra game objects.

    Relative Size ON:


    Relative Size OFF:


    Any ideas on how to fix this? And maybe how can I instantiate new game objects when I move the camera and delete the old ones that are out of sight?

    Thanks!
     
  39. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    Just a quick note, I have seen you two guys post, but I'm in a really tight spot right now. I'll try to answer tomorrow morning. Sorry for the delay.
     
  40. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    Hi,
    clicking is part of Unity's Input class. How do you want clicking to work? Using a collider and the OnMouseDown callback? Something else? There is no general way of handling input, that's why it is not built in. To find a GameObject at a certain position you would have to query the entire scene for that position and account for floating-point inaccuracy. It's easier to do it the other way around and use OnMouseDown: you need a collider on your GameObject, then you can get the grid position of the GameObject from its coordinates
    Code (csharp):
    1. GFGrid grid;  // your object must be associated with a grid
    2. void OnMouseDown() {
    3.     Vector3 gridPosition = grid.WorldToGrid(transform.position);
    4. }
    All my examples work like that as well. The Lights-Out example uses a C# feature called events and delegates to notify all blocks in the scene, but the blocks have to subscribe individually to the event.
     
    Tom163 likes this.
  41. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    Here is how relative size works: when you want a grid rendered five units wide, should those five units be in world- or grid coordinates? If relative size is on it's in grid coordinates. Since you use grid coordinates to instantiate your tiles their position will always be in grid-coordinates and only match the rendered portion of the grid when you have relative size on.

    Your previous question ties into this: when Grid Framework was originally released there was no renderFrom and renderTo, there was only size. It worked like the scale of a Transform component and set the size in all directions. If you want the size of the grid you have to subtract renderFrom from renderTo:
    Code (csharp):
    1. Vector3 size = grid.renderTo - renderFrom;
    You should use relative size so the two coordinate spaces match up. You can convert from grid-size to world-size by multiplying the width of the grid-rectangle by side and the height by height:
    Code (csharp):
    1. // size from the snippet above
    2. float width = size.x * grid.side;
    3. float height = size.y * grid.height;
    The side is 3/2 the radius of the grid and the height is √3 time the radius. See the user manual for an illustration of these dimensions.

    The size property is only for backwards compatibility and shouldn't be used. It will be dropped in version 2.0 in favour of something more like renderFrom and renderTo (except they won't be part of the grid but a separate renderer component). I'm also dropping the relativeSize flag, rendering range will always be relative to the grid.
     
    hexaust_ likes this.
  42. hexaust_

    hexaust_

    Joined:
    Mar 7, 2015
    Posts:
    23
    Thank you for your answer hiphish! Another question, why does the GridChangedEvent fire twice?
     
  43. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    When exactly does it fire twice? I know that changing setting in the inspector can trigger the event even when no value changes due to how Unity works.
     
  44. hexaust_

    hexaust_

    Joined:
    Mar 7, 2015
    Posts:
    23
    I have added in InfinityCamera.cs from the Seemingly Endless Grid example the following lines:


    And when I run it and move the camera the event gets fired twice.


    I am using Unity 5.3.2f1 with Grid Framework 1.9.0
     
  45. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    I got it, the reason is the ResizeGrid method, more specifically these two lines:
    Code (csharp):
    1. _myGrid.renderFrom += rangeShift; // now add the shift to both values
    2. _myGrid.renderTo   += rangeShift;
    We are changing two properties of the grid, which means the grid is changed twice in the same frame, so the event it triggered twice.

    On another note, the proper way to subscribe to an event is to add the name of the method, not instantiate a new delegate:
    Code (csharp):
    1. // wrong
    2. _myGrid.GridChangedEvent += new GFGrid.GridChangedDelegate(OnGridChange);
    3. // correct
    4. _myGrid.GridChangedEvent += OnGridChange;
    I don't know if there is a technical difference, but the latter is the canonical way of doing it.
     
  46. hexaust_

    hexaust_

    Joined:
    Mar 7, 2015
    Posts:
    23
    I think there is a problem when using Relative Size ON on the Seeming Endless Grid example. If I hold down the left arrow key for example, after a couple of rendering steps the camera and the grid "desynchronizes". When Relative Size is OFF everything works right.

     
  47. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    Yes, that's to be expected. You are adding the edges of the camera rectangle in world-coordinates to the rendering range of the grid. They will only match if the grid's size is in world-size. That's why I gave you this snippet above:
    Code (csharp):
    1. Vector3 size = grid.renderTo - renderFrom;
    2. float width = size.x * grid.side;
    3. float height = size.y * grid.height;
    What this means is that if the right edge of the camera rectangle is e.g. at five units you don't set the grid's renderTo to five, but five divided by the grid's side.
     
    hexaust_ likes this.
  48. ITM-Worms

    ITM-Worms

    Joined:
    Jun 27, 2013
    Posts:
    20
    i use follow code to set tiles objekt Stackprefab to my grid.
    Code (CSharp):
    1. StackprefabClone [i] = Instantiate (Stackprefab,myGrid.GridToWorld(myGrid.NearestBoxG(myGrid.GridToWorld(newPosition))), transform.rotation) as GameObject;
    i give GridToWorld a newpostion as example new vector3(1,14,0) and the Grid place my object to the row 13 or 15.
    where is my mistake ?
     
  49. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    607
    Hi,
    Have you tried a minimal example?
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Derp : MonoBehaviour {
    5.     public GFRectGrid _grid;
    6.     public Transform  _t;
    7.     public Vector3 newPosition = new Vector3(1, 14, 0);
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         _t.position = _grid.GridToWorld(_grid.NearestBoxG(_grid.GridToWorld(newPosition)));
    12.     }
    13. }
    This works fine for me. If the minimal example works your problem must be somewhere else.
     
  50. ITM-Worms

    ITM-Worms

    Joined:
    Jun 27, 2013
    Posts:
    20
    thank you for the fast respons.

    i have test your code , and get the same result with my grid.
    attached a screen of my scene . I have make 4 objects with your script give dem grid position.y from 0 to 4 as you see in screen there are gaps but should no gaps.

     
unityunity