Sign in to follow this  
mcaccountman

Unity Need help with pathfinding C#

Recommended Posts

Hey, im making pathfinding for c# and understand the algorithm but the code is confusing.  How would i be able to devide the amount of set space and make rectangles using graphics that are 50 , 50 pixels and fill the area if the area is a multiple of 50? Im not using XNA or Unity.

Share this post


Link to post
Share on other sites

It is hard to determine what you mean in the original post

 

Hey, im making pathfinding for c# and understand the algorithm but the code is confusing.  How would i be able to devide the amount of set space and make rectangles using graphics that are 50 , 50 pixels and fill the area if the area is a multiple of 50? Im not using XNA or Unity.

 

Do you mean you are doing a 2D game which is on a grid or that is 50*50 pixels in size? Or is this a 3D render and you want to overlay a grid on top that you will path find over?

 

If your game is 2D grid based they you should not worry about the render size of each tile at all, just the coordinates, as the render size of the tile is UI based and all the path finder should care about is the tile you are currently evaluating etc.

 

My Path finder code has no concept about the map/game world or whatever it is navigating over, instead it take the follow arguments, The bounding size of the map, so X,Y,W,H along with a Func<Vector, bool> which the path finder uses to decide if the vector location is a valid move. The caller supplies the function and this is what decouples the route finder from the map. If you can think about it this way it will help split what is A* code away from map and render based stuff which just don't matter :)

 

 

Also preferably a list of the rectangles name in listing them like "myTile1" "myTile2" "myTile3" ect.

 

If you have a grid why would you want to "name" of the tile instead of its coordinates? The only logical reason for the names would be you have the tiles in a map by name and you want to use that for lookup, this is unwise as the cost of string based lookups with a large grid could get costly for something like path finding.

Share this post


Link to post
Share on other sites

Generally the navigation mesh (3D) or navigation grid (2D) is created along with the art. 

 

Often it will encode additional information, such as the type of motion to use on the area (walk, swim, climb, crawl, slippery, etc) and sometimes different areas can be traversed at different times (can swim after getting flippers, can walk on quicksand after getting special boots, etc).

 

WozNZ basically covered the rest.

Share this post


Link to post
Share on other sites

Guys, All i need is a algorithm to make 50, 50 width and height rectangles that fit into a 1000 , 1000 pixel form. i want to name the tiles so that in different locations it could set a boolean for the tile to be unable to pass or not. It is a 2d game. No xna or Unity. all i want is to make multiple rectangles like new Rectangle blah (blah blah) exept without me having to do it a bunch of times.

Share this post


Link to post
Share on other sites

Please be more descriptive. Im trying to make it so there is 44 tiles (i did the math) each 50 by 50 pixels. I want them all named from 1 to 44 using possibly a  loop. Im not sure how loops (or any commands beside manually naming it like (new Rectange recty;)) can be used to name things but if you can find that out it will be extremely helpful.

Share this post


Link to post
Share on other sites

Not sure where you're getting 44 tiles from.... a 1000X1000 pixel area would hold 400 50X50 pixel rects

 

If you're asking to name the variable itself myTile1, myTile2 etc, like:

 

var myTile1 = new Rectangle(...);

 

Then no this is not possible in any reasonable way and is really a horrible idea. Instead, you should have something like a 2d array of Rectangles accessed by their grid location:

 

var width = worldSizeX / tileSizeX;

var height = worldSizeY / tileSizeY;

 

var grid = new Rectangle[width, height]();

 

And then use something like this to build the array:

 

for (var j = 0; j < height; j++)

{

  for (var i = 0; i < width; i++)

  {

    grid[i,j] = new Rectangle(i * tileSizeX, j * tileSizeY, tileSizeX, tileSizeY);

  }

}

Edited by jdean300

Share this post


Link to post
Share on other sites

Im not sure how loops (or any commands beside manually naming it like (new Rectange recty;)) can be used to name things but if you can find that out it will be extremely helpful.


I'm not sure why you're jumping in to pathfinding already without having the fundamentals down already. You MUST understand the basic principles of programming, or else you'll ask extremely weird questions like this.

You can't make a loop to generate multiple *variables* such as "Rectangle myRect1; Rectangle myRect2;" without using code generation, and you should NEVER use code generation for this kind of problem. You can't do ANYTHING to make new variables in C# at runtime unless you're compiling code on the fly, which you shouldn't do. You can only set the values of existing variables and use collections such as arrays, lists, etc.

In this case you need to use an array. Here is an example of a 2D array of Rectangles in a C# WinForms app:
 
Rectangle[,] grid = new Rectangle[20,20];

for (int y=0; y<20; ++y)
{
    for (int x=0; x<20; ++x)
    {
        grid[x,y] = new Rectangle(x * 50, y * 50, 50, 50);
    }
}
And like jdean300 said, where did you get 44 tiles? That isn't even a square number. (1000 / 50) per axis is 20, squared is 400. Edited by Nypyren

Share this post


Link to post
Share on other sites

For the OP, you are trying to connect the appearance of the tile to deep into your code.

 

public class Tile

{

    public string Name { get; set; }

    // some other properties in you tile here

}

 

Now you can store your map as follows

 

var Map = new Tile[20, 20];

 

And loop through the grid adding the tiles using whatever map builder you want.

 

NOTE [20, 20] is based in 1000*1000 pixels divided by your 50*50 tiles

 

Notice how there is no notion of the size of the tiles above, that is all decoupled and is the responsibility of the render engine which could use Tile.Name to determine the graphic representation for the tile.  This way the tiles could be 10*10 or 200*200 on screen or even ACSII text display, it does not matter. 

You have to decouple in this way, think what happens if your game is played on a 4K monitor, your 1000*1000 pixel map is now tiny. The pixel size of tiles should never impact the game engine logic or you will find yourself in a world of pain later down the line. The reality is that if you have grid based movement in your game you should be able to write the engine to control the game that is self contained and has no concept about how input is received or how the game is displayed, they just do not matter to the game logic

 

You need to step back and think about what you are trying to do and possibly take some time out and learn some more about code, abstraction and separation of interests before you revisit your game code.  

Edited by WozNZ

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628320
    • Total Posts
      2982056
  • Similar Content

    • By PING YI LIN
      Hi,
      I am an indie game developer from Taiwan.
      This is my game” Wo Yao Da”, which is on sale on Steam now.
      Wo Yao Da video
      Wo Yao Da is a third-person ARPG game, in which the player plays as a tribe warrior and a mysterious weirdo, adventuring in the world of gremlins.

      The tribe warrior is good at using knife, spear, bow and arquebus, and his prototype is the warriors from the Gaoshan Tribe (high mountain tribe) in Taiwan in the early 19th century.

      While the prototype of the mysterious weirdo is the founder of a “special” and new religion. He created a series of “Buddha dance.”
      STAR WARS 莊圓大師
      再掉槍啊
      【莊圓大師 : 對十方世界的復仇】1080p高畫質正式中文預告片,全球搶先上映!(HD)
      He does behave rather differently and in the game, the player attack just like whatever he does in real life, such as rolling on the ground over and over and over… oh yes, that Buddha Dance too, which is something you don’t want to miss.
      I did refer to objects in history for the costumes and buildings, but the story plot is a work of fiction. Please take it lightly and have fun.

      The gremlins in this game looks like this:

      Thank you.
      Steam Page: Wo Yao Da 我要大
    • By Hilster
      Hello 2D Artists,
      I've started making a 2D Puzzle Adventure game for mobile and I'm looking for someone who would want in on creating assets for the game. The core of the programming is pretty much complete, you can walk within the grid laid out and push boxes, when there is an object on top of a pressure pad it will activate the linked objects or if there is one object with multiple linked pressure pads it requires you to activate all points for the object to become active. 

      The level iteration for the game is quick and simple, a Photoshop file that is made of individual pixels that represents objects is put into the game and it creates the level out of those pixels with the assigned objects.
      The objects that need sprites created so far is the character, box, pressure pad, door, trap door, the walls, the stairs and the tiled background.
      I intend to add more objects so the amount I'd like to add will be extended.
      My motivations for posting here is to have something that looks nice to be able to display on my portfolio, so if you're looking for a working game that you can place your art into and improve the look of your portfolio then we're in business.
      Please reply with a few past examples of your art below and I'll be in touch!
    • By thefollower
      Hi
      I have set up my TcpClient to connect to my server and that works fine. But i am a bit confused how i read messages from the network stream with it continuously running via async, without ever closing the connection ?
      My TcpClient Manager class has:
       
      public async Task<bool> Connect(string address, int port) { try { await _tcpClient.ConnectAsync(address, port); IsConnected = true; return true; } catch(Exception e) { Debug.Log(e); return false; } } public async Task<int> Read(byte[] readBuffer) { if (!IsConnected) return -1; using (var networkStream = _tcpClient.GetStream()) { try { var bytesRead = await networkStream.ReadAsync(readBuffer, 0, readBuffer.Length); return bytesRead; } catch (Exception e) { Debug.Log(e); IsConnected = false; return -1; } } }  
      So i thought to just run a co-routine and call Read constantly to get the most recent message, but that doesn't make much sense to me since a co-routine would be blocked with the await. How is this actually done? The MS Docs don't have very good Async examples with the TcpClient class so i don't know fully get how to keep calling Read correctly.
    • By NUITGaming
      Landscaping back ground maid in Unreal 4.18
  • Popular Now