distance question

Game Editor comments and discussion.

Re: distance question

Postby foleyjo » Thu Jun 30, 2011 10:22 am

I see a few of things that would concern me (though it may be valid what you are doing

1 - In your double for loop you will be testing each actor against itself. Also each time you are setting distance back to 1000

2- You are finding the closest enemy but it looks like it's not being used.

3 looking at your code it appears to shoot for every enemy regardless of distance. Your code says
Loop 1 For each enemy create these variables and give the integers a value of 1000,
Loop 2 For each enemy search for which is the closest and store its clone number in point Then end the loop 2
Stop moving
Shoot
Then end loop1


Now it looks like you want to search all the enemys but only shoot at the closest as long as the closest is less than 1000! So maybe try this

Code: Select all
int n;
int enemies = ActorCount("enemy");
Actor *closest;    //This actor will be the closest clone
Actor *check;     //This actor will be the current clone that you are checking
int dist = 1000;

for(n=0; n<enemies;n++)     //For each enemy
{
  check = getclone2("enemy",n)    //See below for info on get clone2. 
  if(distance(x,y,check->x,check->y)<dist)   //This will check to see if the enemy is in shooting range until an enemy is less than this range
  {                                                             //Then it will check to see if the enemy being checked is closer than the current closest
     dist=distance(x,y,check->x,check->y);   //distance will go to the closest enemy below 1000
     closest = check;                                   // this closest actor is not the one being checked. Not using this for anything but thought you might want it  for future reference
  }     
}

if (dist < 1000)          //Only need to shoot if the closest actors distance was less than 1000
{
  xvelocity=0;           // stop moving
  CreateActor("projectile", "icon", "(none)", "(none)", 0, 0, false);  //shoot forward. If you want to shoot towards the enemy then change this to
//CreateActor("projectile", "icon", "(none)", "(none)",closest->x,closest->y, false);
}


You will also need to add the getclone2 to game-editor. II suggest you make a global script for this and then add the folloiwing code
I did a search for the user who made this but can't find it so can someone point out who credit should go to.

Code: Select all
Actor *getclone2 ( char *actorName, long cloneNumber )
{

  char resultString[256] = "";
  char cloneNumberAsString[10];
  Actor *resultActor;

  strcpy ( resultString, actorName );
  strncat ( resultString, ".", 1 );
  sprintf (cloneNumberAsString, "%i", cloneNumber);
  strncat  ( resultString, cloneNumberAsString, 10 );

  resultActor = getclone(resultString);

  return resultActor;

}


The only thing I can guarantee will work here is the getclone2 because that was written by someone who knows what they are doing where as I am just guessing. :lol:
It's good though because while helping you with this I'm learning and may use the same thing one day :D
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: distance question

Postby Nykos » Fri Jul 01, 2011 1:47 pm

well, i got to admit that, watching my code, and reading what you wrote, I don't understand why it works... But I'm sure it could be optimizable.
So i tried restarting it from the beggining, added getclone2 and tried your code and i got a lot of errors:

Error line 9: Expected ;
Error line 10: Expected ;
Error line 15: undeclared identifier dist
Warning line 15: Possible non relational operation
Error line 15: Undefined type for relational operation
Error line 21:Expected ;

and if I create A variable "dist", I only have Errors line 9, 10 and 21.
Nykos
 
Posts: 110
Joined: Sun Dec 19, 2010 11:11 pm
Score: 6 Give a positive score

Re: distance question

Postby Nykos » Fri Jul 01, 2011 2:09 pm

Hum :wink: there's something i forgot to say: the code i did is activated by a colision of an enemy with a filled region... In fact, I think it works in some ways, but not completely and is full of bugs. I'm posting my ged file, could you have a look at it please?
Attachments
nykos.rar
(62.26 KiB) Downloaded 82 times
Nykos
 
Posts: 110
Joined: Sun Dec 19, 2010 11:11 pm
Score: 6 Give a positive score

Re: distance question

Postby foleyjo » Wed Jul 06, 2011 11:18 am

Sorry for all the errors. I was at work when I was doing that so I wasn't concentrating too much on what I was typing :lol:

Line 9s error is simple

check = getclone2("enemy",n)

There should be a ; at the end

check = getclone2("enemy",n);

When I added this semi colon I got no errors with the code.
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: distance question

Postby foleyjo » Wed Jul 06, 2011 12:16 pm

OK I looked at the code and made some changes.

First I got rid of most of the variables and added the getclone2 script. You should try not to use global variables. Not sure why it's just something I was told.

Next I got rid of a lot of event scripts and commands. You may need some of these but I took them out because it will make it easier for you to see what is happening. You can always put them back if needed.

I also noticed in your projectile creation you were repeating a lot of the things you had already done.

I got rid of the timer and replaced it with a variable.
I changed the way the collision events work.

It seems to do what you had it doing already but it uses less code. I was also concerned with how your code would react when there are 2 or more enemies in range for shooting. I am now confident it will only shoot at the closest enemy within your limit box

Let me know if you have any questions.
Attachments
try.ged
(4.71 KiB) Downloaded 82 times
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: distance question

Postby Nykos » Wed Jul 06, 2011 9:48 pm

WOW, man I just looked at it, and it''s amazing. THANKS A LOT

I understand most of the code( even though i could never have been able to write it...) but there are some things that need a little explanations:

Code: Select all
Actor *closest;    //This actor will be the closest clone
Actor *check;     //This actor will be the current clone that you are checking
Actor * NewProjectile;  //So we can control the bullet later

This may be a very stupid question, but what does it mean? Does it declare new types of actors? Is it like a variable?



Code: Select all
NewProjectile->angle = direction(x, y, closest->x, closest->y);  //Give it an angle


I think I understand this one but i just NEED to be sure; are you telling the newly created actor what to do by doing this? Without him having create or draw events? I mean by using" -> " .

Hum, I think that's all :D . I'm just frustrated that i can't work on it tonight cause this code is brillant. Once again, thanks a lot man
Nykos
 
Posts: 110
Joined: Sun Dec 19, 2010 11:11 pm
Score: 6 Give a positive score

Re: distance question

Postby skydereign » Thu Jul 07, 2011 1:36 am

An Actor* is a pointer to an actor (which is a variable). So, you can essentially store an actor (for use) using an Actor*. You really need to use these when dealing with clones, as you can no longer use actorName.actorVar.

When using an Actor*, you use the -> to access its actor variables. So, it is setting NewProjectile's angle to the direction from (x,y) to the (x,y) position pointed to by closest.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: distance question

Postby Nykos » Thu Jul 07, 2011 8:52 am

ok that's what i thought. Thanks Skydereign :)
Nykos
 
Posts: 110
Joined: Sun Dec 19, 2010 11:11 pm
Score: 6 Give a positive score

Re: distance question

Postby foleyjo » Thu Jul 07, 2011 9:56 am

Well Skydereign explained things much better than I could.

I understand most of the code( even though i could never have been able to write it...)


It wasn't that long ago I was in the same position as you and to be honest there's a lot that I'm still learning. In no time at all you too will be giving help to people. If you feel you can help someone then do try because I have found things become a lot simpler when you have to explain them to someone else.
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: distance question

Postby Nykos » Tue Jul 12, 2011 8:17 pm

That's what I was thinking, about trying to help others( well , when I can :D ). thanks for the support Foleyjo.

I'm working on the game right now, and I was asking myself something. Is it possible to check multiple actors, other than enemy. I was thinking about dividing enemies in maybe two or three different actors. I tried to tweak a little the code but it did'nt work.
Nykos
 
Posts: 110
Joined: Sun Dec 19, 2010 11:11 pm
Score: 6 Give a positive score

Re: distance question

Postby jimmynewguy » Tue Jul 12, 2011 8:59 pm

Yes, adding multiple enemies to his code seems like it would be hard. Here is how I do my get the closest enemy.

I make an invisible actor called range that is the enemy creates when it is created, then after a timer is set off this code
Code: Select all
int n;
double mindist = 10000;
Actor *actors, *closest = NULL;
actors = getAllActorsInCollision("Event Actor", &n);//get all the actors colliding actors = the name of colliding actors n = the number of colliding actors


if(actors)//if there are actors
{
  int i;
  for(i = 0; i < n; i++)//loop number of actors
  {
    if(strcmp(actors[i].name, "enemy") == 0 ||//get the name of actors
    strcmp(actors[i].name, "enemy2") == 0)//again name of actors
 
    {
       double d = distance(parent.x, parent.y, actors[i].x, actors[i].y);//get distance
       if(d < mindist)//has to be smaller or out of range
       {
           closest = (actors + i);//closest  = the closest
           mindist = d;
       }
    }
  }
}
if(closest)
{
Actor * this = CreateActor("projectile", "icon", "(none)", "(none)", 0, 0, false);
this->angle = direction(x,y,closest->x,closest->y);
this->directional_velocity = 3;
}


This way you can add as many types of enemies as you want just add them like on lines 11 & 12.

Here's a demo of how it works, I used the graphics I found in one of these demos
Attachments
Closest Shoot.zip
(64.77 KiB) Downloaded 89 times
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: distance question

Postby foleyjo » Wed Jul 13, 2011 5:41 pm

Just realised there was a huge mistake in the original code I gave you :oops:

I forgot to put the check in for the closest actor it needs to be

Code: Select all
 if(check->InRange)   //This will check to see if the enemy is in shooting range
    {
      if ((distance(x,y,check->x,check->y) >-1)|| distance(x,y,check->x,check->y) <dist)   //check if it's closer than the actor the or bit checks to see if it's the first actor in //range
     {
        dist = distance(x,y,check->x,check->y);
        closest = check;   
      }                     // We now have the closest actor
    }
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: distance question

Postby Nykos » Sun Jul 17, 2011 5:45 pm

thanks to both of you, made it work. It's so cool :D
Nykos
 
Posts: 110
Joined: Sun Dec 19, 2010 11:11 pm
Score: 6 Give a positive score

Previous

Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest