A* or Similar Pathfinding...

Game Editor comments and discussion.

A* or Similar Pathfinding...

Postby EvanBlack » Sun Oct 02, 2011 11:27 pm

Has anyone implemented Path Finding through a 2D Array into any games with GE yet? I am currently trying to learn A* and looking for demos with 2D Array Path finding by an AI.


DEMO DESCRIPTION:

Creates Nodes based on mapsize.
Space between node based on tilesize.
AI follows player point moving single node at a time.
Attachments
AIMovementDemo.rar
(234.62 KiB) Downloaded 130 times
Last edited by EvanBlack on Mon Oct 03, 2011 11:35 pm, edited 1 time in total.
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: A* or Similar Pathfinding...

Postby skydereign » Mon Oct 03, 2011 2:07 am

I just made a recursive function, that searches through the array. I've actually done several versions of it, but the main one returned a string with the directions to move. It is pretty straight forward, I recently made one using graphs (nodes depicted by actors), but it all uses pretty much the same thing as you find online, but if you need any help, feel free to ask.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: A* or Similar Pathfinding...

Postby EvanBlack » Mon Oct 03, 2011 3:48 am

Why is it that when I use:

Menu: Script-> Global Code
Button: Variables-> Add

To make a Global variable that I can't assign a value to it in my global code? But if I make it an Actor Variable I can have an actor assign a value to it but it doesn't seem to keep its value.
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: A* or Similar Pathfinding...

Postby skydereign » Mon Oct 03, 2011 4:06 am

That's because an actor variable needs an actor. So in global space, you can't set actor variables. But, if you are in the scope of a function, you can because functions are called by actors, and therefore are in the actor's scope. Since global variables exist everywhere, even not in an actor, you can edit them.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: A* or Similar Pathfinding...

Postby EvanBlack » Mon Oct 03, 2011 4:49 am

No you didn't understand what I said.

I CANNOT edit the GLOBAL Variable.

I tried to set my Global Variable to 2 from global script and it would not set. I had to instead use the global script to initialize the global variable.

Edit:

I found out that its not just Global Variables made through that add button, but I cannot set global variables from global script at all. I can though initialize global variables and set them at the same time in global script. Anyone know why this is?
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: A* or Similar Pathfinding...

Postby skydereign » Mon Oct 03, 2011 6:01 am

Yeah didn't read it carefully.
EvanBlack wrote:But if I make it an Actor Variable I can have an actor assign a value to it but it doesn't seem to keep its value.

This makes it sound like you were having actor variables in global script not work, but I guess you were doing that in a different scope (otherwise I don't think it makes sense).

Well if you are making global variables with the built in variables, and want to initialize it, you should instead make it a variable declared in script. The reason it doesn't let you do it with the other variables, is that space isn't reliable in terms of when it is run. It is better to make global variables through global script, and you should only use the built in variable creator for actor variables, or save group variables.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: A* or Similar Pathfinding...

Postby EvanBlack » Mon Oct 03, 2011 6:50 am

Ok thanks.

I'm having another problem now and I just don't know this one at all. I am trying to find the length of a 2D string array. I tried a couple ways but nothing worked and its not doing what I want it to do. It keeps returning -1. Here is the code to the Function:

Code: Select all
int GetAIID(char str[50])
{
    int i;
    int len = sizeof(AINames)/((sizeof(char))*20); //char AINames[2][20]
 
    if(!len){return -2;}
    else
    {
        for(i = 0; i < len; ++i)
        {
            if(AINames[i] == str)
            {
              return i;
            }
        }
    }
    return -1;
}
Last edited by EvanBlack on Mon Oct 03, 2011 7:14 am, edited 1 time in total.
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: A* or Similar Pathfinding...

Postby skydereign » Mon Oct 03, 2011 7:06 am

If you want to find the length of a string, you can use strlen. If it is a 2d array, then you just use strlen(array[0]), or similar. Do though make sure the strings are initialized to 0, so that there are null characters in the array (otherwise strlen won't work).
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: A* or Similar Pathfinding...

Postby EvanBlack » Mon Oct 03, 2011 10:39 am

What is going on here.... I can't see my tile when I create it through script. Do I need to have the tile already created?

Code: Select all
void CreateMap()
{
 int mh;
 int mw;
 int tX = 0;
 int tY = 0;
 int tNum = 0;
 char tName[10];
 
 for(mh = 0; mh < MAPSIZE[0]; ++mh)
 {
     tY = 100 * mh;
     for(mw = 0; mw < MAPSIZE[1]; ++mw)
     {
         tX = 100 * mw;
         sprintf(tName, "Tile%i", tNum);
         CreateActor(tName, tName, "no parent", "data/Tile1.png", tX, tY, false);
         strcpy(TilesNames[tNum], tName);
         TilesID[tNum] = tNum;
         TilesX[tNum]  = mw;
         TilesY[tNum]  = mh;
         tNum = tNum +1;
     }
 }
 
 
}
Last edited by EvanBlack on Mon Oct 03, 2011 10:52 am, edited 1 time in total.
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: A* or Similar Pathfinding...

Postby skydereign » Mon Oct 03, 2011 10:46 am

Chances are, that isn't what you want.
Code: Select all
CreateActor("actor_name", "animation_name", "parent_name", "path_name", x, y, true);


So, perhaps the animation name is the same as the actor's name. If that is the case, then the next two strings, need to be "(none)". Though, it looks like tName should be replaced with "Tile1.png". Of course, Tile1 would need to be an animation.

If you try to create an actor with an animation it doesn't have, it will be created, but either show the pacman, or be invisible (since it doesn't have an animation).
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: A* or Similar Pathfinding...

Postby EvanBlack » Mon Oct 03, 2011 10:58 am

Umm.. I don't think what I am trying do is right... Do I need to have the actor created in game editor first? Then does that mean I have to make 100 actors just to build my map? How come there isn't a script clone function?

I am trying to make a function build a map from scratch.

Ok I created Tile1 with the animation in Game Editor and set it to not create at start up, then it created 1 tile, but how do I build a map without having to create 100 actors manually?
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: A* or Similar Pathfinding...

Postby skydereign » Mon Oct 03, 2011 12:13 pm

Okay, that would explain it. Yes you need the actor to be created in the editor (but it doesn't have to exist in the game). Usually what people do is have a single tile actor, and clone it to create different tiles. Each clone can have a different animation, so while they are all tiles, you can set them to whatever you want. For instance, you can have one actor named tile, with 40 different animations (or frames in an animation). This way you only have to create one actor, but you do have to add several animations to the actor, and each clone can be unique.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: A* or Similar Pathfinding...

Postby EvanBlack » Mon Oct 03, 2011 1:20 pm

How do I access the clone in script? How can I create the clone? When I use this code GE crashes..

Code: Select all
sprintf(tName, "Tile.%i", tNum);
CreateActor(tName, "Tile1", "no parent", "no path", tX, tY, false);


UPDATED:

I got it. This code does what I wanted.
Code: Select all
sprintf(tName, "Tile.%i", tNum);
CreateActor(
getclone("tName")->name, "Tile1", "no parent", "no path", tX, tY, false);
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: A* or Similar Pathfinding...

Postby skydereign » Mon Oct 03, 2011 9:08 pm

Yeah, you don't want to do it that way. That is the same thing as doing this.
Code: Select all
CreateActor("Tile1", "Tile1", "no parent", "no path", tX, tY, false);


It is good however that you've learned how to use an Actor*. But, if you want to create clones, all you need to use is the above script. When you create a clone, it takes the clone with the highest cloneindex, and ups that by one. Actors have a name variable, clonename variable, and cloneindex variable. The name, is the name of the actor. The cloneindex is the indexing for the clone, a unique tag for each of them. The clonename combines the two as actor_name.cloneindex. So, what your code does is store a clonename into tName, so you can get a specific clone (which doesn't exist), and get the clones name (removing the cloneindex). Because of this, all you have done is add the cloneindex so you can remove it later.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: A* or Similar Pathfinding...

Postby EvanBlack » Mon Oct 03, 2011 9:19 pm

Im kind of confused by what you said. But the Actor* has a clone index from the start? So every Actor has an initial clone index of 0?

So when I use CreateActor it creates a clone? Can I access the clone index via Actor->cloneindex?

Im still confused. I want to auto generate a tile map from a single actor with many different tiles.

I changed my script to instead of placing tiles, it plots nodes based on Game Center. I still want to place the tiles at these nodes.

DEMO UPLOADED TO FIRST POST
Last edited by EvanBlack on Mon Oct 03, 2011 11:26 pm, edited 2 times in total.
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Next

Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest