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 Based Map Nav

Discussion in 'Assets and Asset Store' started by Leslie-Young, Jun 2, 2012.

  1. c-Row

    c-Row

    Joined:
    Nov 10, 2009
    Posts:
    847
    Well, it seems even men smarter than us don't have a perfect solution.

     
  2. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    That's gamedev for you :) Gotta make compromises to keep priorities balanced. No use you got a perfect simulation if players can't enjoy it in realtime.
     
  3. CrucibleLab

    CrucibleLab

    Joined:
    May 29, 2012
    Posts:
    19
    Leslie - I got another question for you (surprise) and this relates to the Raycast and Layer mask question I threw at you about a week ago:

    So, I'd expect to find all your standard units to have been already assigned to layer 21 and, thus, this would show up in the Inspector panel. But oddly, it seems that no layer value is set for any of the standard units (i.e., nothing selected in the Layer dropdown list in the Inspector panel) and the TagManager under the Inspector panel has no value assigned to 'User Layer 21' when it's supposed to have the value 'unit'.

    Maybe I am completely off the track here but that's basically what I gathered from watching the tutorial videos and keeping up with this thread. Please help me make some sense out of this. :-(
     
  4. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    Packages from the asset store can't set things like this when installed, or not as far as I know. This is why I stated in the ducomentation to set it,

    For more on layer, check the Unity docs.
    http://docs.unity3d.com/Documentation/Components/Layers.html
     
    Last edited: Nov 18, 2012
  5. MCHammond

    MCHammond

    Joined:
    May 15, 2009
    Posts:
    74
    Just bought this asset and its looking good, cuts down on my workload a huge amount.

    I just have a few Questions regarding the code:

    Is their a documentation of the API?

    If I wanted to move a unit to game object "node01" how would this look?
    MoveTo("node01", ref _moves).
    or
    TileNode Node01 = node01.GetComponent<TileNode>();
    MoveTo(Node01, ref _moves);


    Thanks I just need to get my head around the basics.
     
  6. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    The only documentation is the provided PDF files. The source is wel ldocumented and each fucntion should tell you what it does. You want to mainly look at TileNode and MapNav. NaviUnit will contain more info on the MopveTo function.

    MoveTo expects the component. The idea is that a player click on a tile object and you get the TileNode component from that object.
    If you want to find a node by name you could do.
    TileNode Node01 =GameObject.Find("node1").GetComponent<TileNode>();
    you could also do..
    TileNode Node01 = the_mapnav[1];
    or
    TileNode Node01 = the_mapnav.nodes[1];
    assuming "the_mapnav" is a reference to your MapNav gameobject's MpaNav component. (the mapnav map)

    Have a look at the scripts under sample to see how the classses and functions are used for the various sample scenes.
     
  7. bonesbrigade

    bonesbrigade

    Joined:
    Oct 29, 2012
    Posts:
    1
    Hi Leslie - first off thanks for the great tools!

    I have actually converted a lot of them to JS (as I am more comfortable with the code that way), but maintain the TMNController and NaviUnit as CS files.

    I have added in some features that allow me to place units manually and then have the game controller find and link them automatically (similar to SpawnUnit but using existing gameObjects).

    I also built a config file loader that allows me to modify things like movement speed and max moves from a text file, and then the game controller applies it to the relative objects (the config file is written in JSON... I am a web developer and that was my favorite format).

    But wow does your code save me time!! I love it - I was going to build one myself but saw yours and gave it a shot. It is a very great tool and I find the tools useful (I love the auto mask tool!)
     
  8. MCHammond

    MCHammond

    Joined:
    May 15, 2009
    Posts:
    74
    Sounds like what I am trying to implement :p
    I want to place the units in the editor manually and not randomly spawn them.
    Any hints on how you got the units to work if they are manually placed?

    Just starting out, this is what I have so far:
    All it does atm is finds the closest node to the unit and snaps the unit to it at the start of the game. "all the nodes have the tag "Node".
     
  9. MCHammond

    MCHammond

    Joined:
    May 15, 2009
    Posts:
    74
    Ok
    I got what I wanted working, I can now place units in the editor and they are usable in the game. I was a bit confused as to were everything was and I kept following things through different scripts and eventually I was able to add the right code in the right place.

    In case anyone is interested the code/setup is like this:

    Add the tag "Node" to all your nodes. "this is to speed up searches, according to the Unity API a tag search has less overhead".
    Add the tag "Enemy" to all the units you want to place "or what ever you want".

    Add this function to your GameController:
    Code (csharp):
    1. //find the nearest Node to the target Game Object.
    2.     private GameObject FindClosestNode(GameObject Target){
    3.         GameObject[] gos;
    4.         gos = GameObject.FindGameObjectsWithTag("Node");
    5.         GameObject closest = null;
    6.         float distance = Mathf.Infinity;
    7.         Vector3 position = Target.transform.position;
    8.         foreach (GameObject go in gos) {
    9.             Vector3 diff = go.transform.position - position;
    10.             float curDistance = diff.sqrMagnitude;
    11.             if (curDistance < distance) {
    12.                 closest = go;
    13.                 distance = curDistance;
    14.                 }
    15.             }
    16.        
    17.         return closest;
    18.     }
    Add this function to your GameController:
    Code (csharp):
    1.         ///Attaches all the objects with this Tag to the MapNav and preps them as units ready to use.
    2. private void AttachToNode(string Tag)
    3.     {
    4.         GameObject[] Targets = GameObject.FindGameObjectsWithTag(Tag);
    5.         foreach (GameObject Target in Targets){
    6.             GameObject TargetNode = FindClosestNode(Target);
    7.             Unit unit = (Unit) Unit.AttachUnit(Target, map, TargetNode);
    8.             unit.Init(OnUnitEvent);
    9.  
    10.             if (randomMovement) unit.ChooseRandomTileAndMove();
    11.         }
    12.            
    13.     }
    Add this line of code in the Update() of the GameController:
    Code (csharp):
    1. AttachToNode("Enemy");
    Add this Function to the NaviUnit script:
    Code (csharp):
    1.     /// <summary>Attachs the unit to the node and preps it ready for use.</summary>
    2.     public static NaviUnit AttachUnit(GameObject Target, MapNav mapnav, GameObject TargetNode)
    3.     {
    4.         TileNode TargetTile = TargetNode.GetComponent<TileNode>();
    5.         Target.transform.position = TargetTile.transform.position;
    6.         NaviUnit unit = Target.GetComponent<NaviUnit>();
    7.         unit.mapnav = mapnav;
    8.         unit._tr = Target.transform;
    9.         unit.LinkWith(TargetTile);
    10.         return unit;
    11.     }
     
    Last edited: Nov 22, 2012
  10. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    @bonesbrigade, glad you find it usefull and that the JS conversion did not give you too much trouble.

    @MCHammond, here's few tips
    MapNav allready carry a cache of all the nodes so you do not ahve to use gos = GameObject.FindGameObjectsWithTag("Node");
    In the samples the variable for the map is called "map" and is defined in TMNController which GameController ingerits from.
    So you could do this ...

    Code (csharp):
    1.  
    2.     private GameObject FindClosestNode(GameObject Target){
    3.         GameObject closest = null;
    4.         float distance = Mathf.Infinity;
    5.         Vector3 position = Target.transform.position;
    6.         foreach (GameObject go in map.nodesCache) {
    7.             Vector3 diff = go.transform.position - position;
    8.             float curDistance = diff.sqrMagnitude;
    9.             if (curDistance < distance) {
    10.                 closest = go;
    11.                 distance = curDistance;
    12.             }
    13.         }    
    14.         return closest;
    15.     }
    16.  

    You should not do AttachToNode("Enemy"); in Update, especially if it is just a one time setup of something. You don't want code executing that has allready done its job. I'd rather do the attach thing after the unit was created and in the Start() if you placed them manually during design time (in the editor) to they get inited on startup.

    I'd also do a test in AttachToNode to see if the unit is not alrleady on a node. unit.node != null
    Well that is if your code where not expecting AttachToNode to always update a unit for example if you are not using the movement functions but moving the unit in another way and was depending on this fucntion to update the nnode it is over. In that case I would rather just call some code to find its node on the one unit after it was moved rather than calling this function in the update to check units that don't need checking.
     
  11. MCHammond

    MCHammond

    Joined:
    May 15, 2009
    Posts:
    74
    Thanks Leslie Young, awesome feedback :p

    Its good to know about the node cache, I will use that when I can.

    The AttachToNode("Enemy") in the Update() is placed in the "state == State.Init" if statement the same as the SpawnRandomUnits(spawnCount);. I did not want to post any of your code "for obvious reasons".

    After looking into implementing a simple AI, I am going to use Tag's on everything like "Enemy", "Player", "Door" and "Grenade" so I will change the FindClosestNode(GameObject) to something like FindClosestTag(GameObject, Tag) that way I can use the same function every time I want to find closest tagged thing like an enemy or player.

    Pseudocode for my planned enemy AI implementation "Zombies":
    Anyways thanks for your help, don't feel obligated to reply to my ramblings.

    Is there somewhere users of this asset can share and discuss?
     
    Last edited: Nov 22, 2012
  12. MCHammond

    MCHammond

    Joined:
    May 15, 2009
    Posts:
    74
    To anyone reading this thread thinking of getting this asset, This is very true. The source has all you need in terms of documentation to get started.
    Just make sure your script editor is not set to hide or truncate comments.
     
  13. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    You can share and discuss here. I don't mind if you post snippets from my scripts here.
     
    Last edited: Nov 26, 2012
  14. topagae

    topagae

    Joined:
    Oct 30, 2012
    Posts:
    10
    So when I try to go through the first YouTube tutorial, I notice the window is different (I imagine due to a newer version), and when I try to follow the instructions I get the following console error:

    NullReferenceException: Object reference not set to an instance of an object
    MapNav.CreateTileNodes (UnityEngine.GameObject nodeFab, .MapNav map, TilesLayout layout, Single tileSpacing, Single tileSize, TileType initialMask, Int32 xCount, Int32 yCount) (at Assets/Tile Based Map and Nav/Scripts/TMN/MapNav.cs:289)
    MapNavCreateWindow.CreateMapNav () (at Assets/Tile Based Map and Nav/Editor/MapNavCreateWindow.cs:76)
    MapNavCreateWindow.OnGUI () (at Assets/Tile Based Map and Nav/Editor/MapNavCreateWindow.cs:57)
    System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)

    And nothing appears on the scene, though I do get only one node (Should get a 10x10)? Very new to your plugin and unity so I'm a bit lost on this part.

    Edit: Found the answer while reading through these forums. Should really update tutorials and questions when you update stuff.

    Edit: What's missing: In the following video: http://www.youtube.com/watch?v=ZjAU0AbA0aQ
    Some things are missing, such as the fact that the UI looks nothing like how it is in the video, which is confusing, and I had to search these forums to find out we needed to select a prefab for our tiles or they show up blank. Also, out of the box "Show" functionality doesn't seem to work because as it is now.
     
    Last edited: Nov 28, 2012
  15. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    Please provide a link to which video you refer to so that I can tell whether it is out fo date or not.
    All videos linked in the first post are valid and relating to version 2+ of Map&Nav.

    Can you also indicate what the problem was and what part of the forum you are refering to, else I won't know what you mean by udpating the tutorial... like what part is missing.
     
  16. csshelton70

    csshelton70

    Joined:
    Nov 26, 2012
    Posts:
    16
    First off, let me just say how amazing this tool is...we'll worth the cost!

    @MCHammond - dude, you just saved me some work. Thanks for posting your code.

    Now for my question...

    I want to be able to indicate to each of the players the "spawning" or "deployment" area at the beginning of the game. Any thoughts on how to only show the first 2-3 rows AND color them a different color at runtime - I can probably figure out how to limit them to just those nodes during the deployment phase.
     
  17. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    I'm guessing the two players are on opposite sides of the map?
    To show and ake the 1st two rows blue and to show and make the last two rows red you oculd do something like this. (note, not tested)

    Code (csharp):
    1.  
    2. // this will assume you are using one of the node prefabs from the samples, or one that works similar to them
    3. // show the first two rows and make 'em blue
    4. for (int i = 0; i < (mapnav.nodesXCount * 2); i++)
    5. {
    6.     TileNode node = mapnav.nodes[i];
    7.     node.Show(true); // show it, also allow clicking on it since the collider is activated (see code for Show())
    8.     node.renderer.material.color = Color.blue; // make it blue
    9. }
    10.  
    11. // show the last two rows and make 'em red
    12. for (int i = 0; i < (mapnav.nodesXCount * 2); i++)
    13. {
    14.     TileNode node = mapnav.nodes[mapnav.nodes.length - 1 - i];
    15.     node.Show(true); // show it
    16.     node.renderer.material.color = Color.red; // make it red
    17. }
    18.  
     
  18. topagae

    topagae

    Joined:
    Oct 30, 2012
    Posts:
    10
    So I'm adding in logic that requires me to know pretty well how the tiles are linked up. I need to know this because I'm writing logic that takes into account where each neighbor is to a certain node (To the left, upper right, etc). So I'm trying to logic out which directions are which in the TileNode array in the LinkNodes function in the MapNav.cs script file. Starting at around Line 323. Looking at this code it seems to go like this: (Psuedo coding it up!)

    For each node

    Make link to next node (Node to the right of current node?)
    Make link to previous node (Node to to the left of current node?)

    if(There are nodes in the previous row)
    Make a link to the node in the previous row, but same column

    if(someRandom flag that switches on and off)
    Make a link to the node in the previous row and NEXT column
    else
    Make a link to the node in the previous row and PREVIOUS column


    So my questions are these:
    What's the random flag for? I imagine the answer has something to do with the next question and that's: How are the tiles laid out in your code? Hexes don't really have straight up and down rows and columns, so presuming I picture like below:

    http://screencast.com/t/K3fBkqK1

    How am I to figure out which number in the tile node array goes to which direction the neighbor is in? I marked the numbers that I think represent where those links are in the nodeLinks array.
     
    Last edited: Nov 28, 2012
  19. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    What is the random flag you refer to, the variable named "atoffs"? If so, that is how I determine if a node onm the next/prev row is 1/2 column "ahead" or "behind" in the hex layout relative to the center one being linked with - refer to your linked pic.

    You will not be able to tell how a node lay relative to its neighbour just from looking into TileNode.nodeLinks array. Though there might be a pattern like TileNode.nodeLinks [0] being always the previous node from and [1] always the next, etc, I will not promisse this and support this behaviour.

    From my code (line323 onward) you can see that [2] and [4] is used for the row below and [3] and [5] for the next row. What is the "next" and what is the "prev" column swops though and you can't rely on [2] always being the previous row with a 1/2 column offset to back (to the left) as it might [4].

    What you can do though is to check the position of the one node relative to the other. If [2]'s X position is smaller than the middle's node's then it must be behind (to the left).

    Hope that explains it, please do ask if it is not clear.
     
  20. topagae

    topagae

    Joined:
    Oct 30, 2012
    Posts:
    10
    Makes sense, since you gotta thread em. My current solution I got in my head is to get the left node, then I can pretty easily use the X's and Y's of all the others compared to those two to get the rest. Probably XD.

    Edit:

    Here's my new psuedocode:

    Assign left and right (Since I can see in your code it's always static)

    Check link 2, if it's X is lower then target node, it's lower right, else it's lower left. I can then assign the other to link 4.

    Do the same above, for link 3 and 5.

    Then I got all my directions for a node!

    Here's a picture if anyone else wants to attempt this modification for use in their game:

    http://imgur.com/8K7uO
     
    Last edited: Nov 28, 2012
  21. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    You might find it usefull to just go and modify my linking code. Just test the X value there and then and make sure that [2] for example will always be used to save the left node and [4] for the right node - then you don't have to do this X test while the game is running.
     
  22. topagae

    topagae

    Joined:
    Oct 30, 2012
    Posts:
    10
    That would indeed be a lot easier, that was my intention but for any person's purposes I was hoping to illustrate a simple way to do this in a game.

    Edit: IF anyone wants the code I made this work in this asset. All my nodes now reliably connect to their neighbors in a way you can easily use. These properties are useful for say, traversing a straight line of nodes. Or searching certain nodes around other nodes.
     
    Last edited: Dec 1, 2012
  23. dsfds

    dsfds

    Joined:
    Aug 19, 2012
    Posts:
    16
    what is the correct way to remove a unit that spawn from the engine?
     
  24. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    Call the_unit.node.units.Remove(the_unit); so that it is unlinked with the node, else the movement code might think it is still there. You can destroy the unit's game object any way you like after that..
     
    Last edited: Dec 4, 2012
  25. daniel92

    daniel92

    Joined:
    Nov 28, 2012
    Posts:
    1
    I'm making a small game for a class project and when we replaced your units with our own modeled pieces, we are not able to select our pieces. We can still select your units, however. All of the scripts are the same and have been added to our pieces. If you could provide any help, it would be nice.
     
  26. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    The important things to check ...
    * Layer = Unit
    * The new unit had the Unit component (or any script/component that you inherit from NaviUnit if you are not using the sample Unit script)
    * There is a big enough collider on the same object that the Unit component is on.
    * If you used the sample Unit component/script then you probably need to add the SampleWeapon script too - well, just check if yo uare getting errors in the console when this is not on the new unit object.
     
  27. jingato

    jingato

    Joined:
    Jun 23, 2010
    Posts:
    299
    I Leslie,

    I've been enjoying your Tilemap quite a a bit. It's save a lot of development time. I have one issue though. I know that you have the ability to turn off the connection between two nodes, but I is the anyway to disconnect them for only one direction? I need to have a path that is a one way path, but I can't figure out how I can do that. Is this possible?

    Thanks you
     
  28. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    How to find on what side of a node another sits has been discussed from here onward (note, this discussion is about hex tiles). That is the best solution I have for it and you will need that to determine which nodes to turn off in a certain direction. Map&Nav was not really made to know the directional links between nodes.
     
  29. c-Row

    c-Row

    Joined:
    Nov 10, 2009
    Posts:
    847
    Is there a roadmap for future versions/features? I reckon there have been some interesting ideas and additions on the last few pages.
     
  30. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    There are no plans to add further features at this time.
     
  31. Alucard384

    Alucard384

    Joined:
    Nov 21, 2010
    Posts:
    8
    Am I missing something with the Auto Height Setup? It creates everything fine but when I go to turn the colliders on in code at runtime it throws a null reference when it hits one of the nodes that were removed due to a wall. It seems to delete the node from the scene but it is still being counted within the map.nodes property. I'm using your UniRPG along with Map&Nav if that helps at all.
     
  32. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    Can you please post the error msg so that I can see on what line/where in the code this happens.
     
  33. Alucard384

    Alucard384

    Joined:
    Nov 21, 2010
    Posts:
    8
    Basically since the auto height setup with node deletion messes up the nodes id's it throws the null reference due to the node missing in the list.

    For me, it goes through the first 32 and since 33 through 38 got deleted due to it being a wall it wont skip to 39 and sends the error.
     
  34. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    Ah, so the error is in your own code. Thought it might be somethng in one of my scripts that I can look at, that is why I asked for the error message.

    The arrays of the nodes, like MapNav.nodesCache and MapNav.nodes will always be size, nodesXCount * nodesYCount.
    If node5 is deleted in the scene it will be set as NULL in the array. This is by design; node6 won't be shifted down to fill the "null" space and the array won't be shrunk to indicate that there is one less node. The node might be gone, but it left a space that is still being tracked.You need to test for null if you know some nodes will be missing in the scene.
     
    Last edited: Dec 9, 2012
  35. Alucard384

    Alucard384

    Joined:
    Nov 21, 2010
    Posts:
    8
    Ah, that'll be useful to know. Thanks.
     
  36. dsfds

    dsfds

    Joined:
    Aug 19, 2012
    Posts:
    16
    I also grab your other tools, UniRPG and DungeonEd. Is there any way to combine all them together with only 1 map tile and extend all the functionality.
    I looked at your code the Map tile part. They are basically the same engine. Could you give me some overview of how to combine them with the min effort?
     
  37. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    UniRPG and DungeonEd is not compatible and you need to use the one or the other. I suggest using UniRPG and creating the in-game editor based on UniRPG's runtime scripts if you need something like that (which is what DungeonEd was - it only provided a runtime editor).

    Map&Nav don't need to be "combined" as such. You simply use it with UniRPG created maps like you would use it with any other kind of map. So, you would paint your map with UniRPG Tile Ed and then create a Map&Nav grid/map object which would serve as a system to handle navigation from node to node and other things where you might need nodes.
     
  38. dsfds

    dsfds

    Joined:
    Aug 19, 2012
    Posts:
    16
    But in this case you will have double of tiles to work with? UniRPG only for paint your map. Map Nav for game logic stuff.
     
  39. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    You will have the painted UniRPG Tile Ed tiles for art and Map&Nav nodes for logic; like you might have had the Unity Terrain for art and Map&Nav nodes for logic, or a flat plane for art and map&nav nodes for logic. Do not confuse the nodes in a map&nav for tiles or art. The only art conponent they might have are the markers for indicating where a unit can move but they do not need any art component to function and what is presented in the sample scenes are just there as samples of what can be done.
     
    Last edited: Dec 14, 2012
  40. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    If you where thinking of getting this package, now might be the time!

    Map Nav is on special (50% off) for only $17.50
     
  41. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    I've decided to give a little gift to the community to say thanks for the support in buying my Tile Ed and Map&Nav packages through the year.

    You can download the Battlemass client side scripts here or grab the file attached to the post. It is all the scripts used on the client side and includes simple AI, multiplayer, hot-seat, and even a very old implementation of map&nav (note, the package I sell now is way better), and more. There should be something usefull in there for someone. It is given as is and I will not be able to provide support on this.
     

    Attached Files:

    Last edited: Dec 23, 2012
  42. syphon7

    syphon7

    Joined:
    Dec 22, 2012
    Posts:
    3
    Thanks for releasing this source code! I just got your plugin the other day and its already saved me hours and hours of development time.
     
  43. syphon7

    syphon7

    Joined:
    Dec 22, 2012
    Posts:
    3
    Hello,

    I've got a bit of a problem when I try and link nodes between MapNavs.

    Code (csharp):
    1. curTile=(TileNode)curMap.nodesCache.GetValue(x+150);
    2. linkTile = (TileNode)topMap.nodesCache.GetValue(x);
    3. curTile.linkOnOffSwitch.SetLinkStateWith(linkTile, true);

    I feel like this should work, but I get a cast error "Cannot cast from source type to destination type"

    Any thoughts as to what might be going wrong?

    Thanks!
     
  44. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    You are casting the "curMap to TileNode. What you want to do should be done like this to indicate you want to cast the nodesCache, (notice position of brackets)
    Code (csharp):
    1. (TileNode)( curMap.nodesCache ).GetValue(x+150);
    Anyway, you should not be casting since you are looking for a component on a GameObject; nodesCache is an array of GameObjects and GameObject do not inherit from TileNode.

    GetComponent is how you get the component.

    If this is runtime code then you can make use of the cache of nodes that MapNav create in its Start() function,
    Code (csharp):
    1.  
    2.    // you can only do this after the MapNav Start() function got a chance to run.
    3.     curTile=curMap.nodes[x+150];
    4.     linkTile = topMap.nodes[x];
    5.     curTile.linkOnOffSwitch.SetLinkStateWith(linkTile, true);
    6.  
    if you are doing this in editor scripts (or even runtime) you can use this modification to your code ...
    Code (csharp):
    1.  
    2.     // 1st get the GameObjct at [x+150] and then do GetComponent to get the TileNode component from it.
    3.     curTile=curMap.nodesCache[x+150].GetComponent<TileNode>();
    4.     linkTile = topMap.nodesCache[x].GetComponent<TileNode>();
    5.     curTile.linkOnOffSwitch.SetLinkStateWith(linkTile, true);
    6.  
     
    Last edited: Dec 26, 2012
  45. syphon7

    syphon7

    Joined:
    Dec 22, 2012
    Posts:
    3
    Thanks, that helped a lot. I still have one problem however:

    When I run this code the linkOnOffswitch equals null on all of the tiles.

    Code (csharp):
    1. curTile.linkOnOffSwitch.SetLinkStateWith(linkTile, true);
     
  46. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    Sorry, I did not pick up on that.

    linkOnOffswitch is only available after the Start() fucntion of the TileNode got a chance to run and also only if the TNELinksOnOffSwitch component is present on the Node object. In your case you do not have the TNELinksOnOffSwitch component on the Node yet since you are setting up links with nodes that are from different maps. TileNode.SetLinkState() (TileNode.cs line 299) won't work cause it expects the nodes being checked to allready be linked and that you are just trying to manipulate the on/off state .. in your case they are not linked yet.

    You can open up Editor/TileNodeEditor.cs and check CreateLinkBetweenSelected() line 71 to see how I create the links when you click nodes to be linked with each other (the thing I talked about in a previous post, about the inspector).

    Anyway, to get to the point. You do not use TNELinksOnOffSwitch but TNEForcedLink. TNELinksOnOffSwitch is used to set the state of existing links to on or off without breaking the link but you want a new link that does not exist and which must exist over two maps.

    Code (csharp):
    1.  
    2. GameObject g1 = curMap.nodesCache[x+150]; // node 1's gameobject (current)
    3. GameObject g2 = topMap.nodesCache[x];     // node 2's gameobject (linktile)
    4.  
    5. // check if TNEForcedLink is present, else add
    6. TNEForcedLink ls = g1.GetComponent<TNEForcedLink>();
    7. if (ls == null) ls = g1.AddComponent<TNEForcedLink>();
    8.  
    9. // now link with forced link
    10. ls.LinkWith(g2.GetComponent<TileNode>());
    11.  
     
    Last edited: Dec 27, 2012
  47. johny

    johny

    Joined:
    Aug 31, 2011
    Posts:
    133
    This may sound like a really stupid question but I cant for the life of me figure it out. On the Unit script is there an easy way to check if it is the unit selected? Something like

    if( selected == true){
    do this;
    }

    Thanks!
     
  48. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    Check out the GameController.cs in the samples to see how I handle and track unit selection.The variable, selectedUnit, defined at line 40 is what you want to look at. Do a search for it in that script to see how it use used throughout.
     
  49. kingawesome

    kingawesome

    Joined:
    Nov 22, 2012
    Posts:
    1
    Just bought the package and am loving it so far. I was messing around with trying to get some basics up and running for a turn based game, and this launched me forward much quicker :D

    Quick, and probably dumb question though. When I use the map/nav creator is it possible to have it create the actual ground as well? Lets say I have my prefabs already (a grassy cube, a water cube etc...) and want to create a 10x10 map of all grass with map/nav nodes on top.

    Is the process to first manually lay out my actual "game board", then create the map/nav nodes, then use auto-height setup tool to get everything lined up properly?

    Just want to make sure I'm not missing a step or anything.
     
  50. E1iTe

    E1iTe

    Joined:
    Aug 7, 2012
    Posts:
    47
    Hi again Leslie, quick question... there used to be a way to completely delete a link between nodes... I'm wondering if i'm missing something but I'm trying to delete a link between two nodes and then have a second MapNav and link two nodes between them. When I try to just unlink the original and then add a link to the new node it creates the link but it also makes the new link "unlinked" because I see a red line. Any fix for this?

    Thanks again!