Collisions as a conditional parameter

Non-platform specific questions.

Collisions as a conditional parameter

Postby bamby1983 » Mon Jan 21, 2013 2:36 pm

Is it possible to use a collision as a conditional parameter in an if-statement? For example:

Code: Select all
if (<Actor A is colliding with Actor B>) {
    // Do this
                                                        }
else {
    // Do that
      }
bamby1983
 
Posts: 112
Joined: Tue Jul 31, 2012 11:36 pm
Score: 8 Give a positive score

Re: Collisions as a conditional parameter

Postby skydereign » Mon Jan 21, 2013 6:54 pm

Technically yes. There is a function called getAllActorsInCollision, which can be used to do that.
Code: Select all
 int n;
Actor *actors;

actors = getAllActorsInCollision("Event Actor", &n);


if(actors)
{
    int i;
    for(i = 0; i < n; i++)
    {
        if(strcmp(actors[i].name, "B")==0) // colliding with an actor B
        {
            // do this
        }
        else
        {
            // do that
        }
    }
}

Notice though you have to loop through all actors colliding with the actor. Also, you don't have to specify "Event Actor" in the getAllActorsInCollision. If I had followed your use case, I would have put "A" instead (but usually that isn't a good idea). When you can, you should be clone specific, instead of actor specific.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Collisions as a conditional parameter

Postby bamby1983 » Mon Jan 21, 2013 9:30 pm

Thanks for the code, sky. I'm a little confused about certain bits, though.

What do the following pieces of code do?

Code: Select all
Actor *actors;


Code: Select all
if(actors)
bamby1983
 
Posts: 112
Joined: Tue Jul 31, 2012 11:36 pm
Score: 8 Give a positive score

Re: Collisions as a conditional parameter

Postby skydereign » Mon Jan 21, 2013 11:13 pm

Code: Select all
 int n; // used to store the number of actors colliding with event actor
Actor *actors; // declares an Actor* pointer (used to point to an array of actors event actor is colliding with)

actors = getAllActorsInCollision("Event Actor", &n); // the &n passes the address of the variable n, so the function can change the value
// actors now points to an array of n actors, where n is equal to the number of actors colliding with the actor
// if no actors are colliding, actors is set to null
if(actors) // this makes sure actors is not null (there are actors colliding and getAlActorsInCollision succeeded)
{
    int i;
    for(i = 0; i < n; i++)
    {
        if(strcmp(actors[i].name, "B")==0) // colliding with an actor B
        {
            // do this
        }
        else
        {
            // do that
        }
    }
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Collisions as a conditional parameter

Postby bamby1983 » Mon Jan 21, 2013 11:23 pm

Thank you, you are awesome! :D
bamby1983
 
Posts: 112
Joined: Tue Jul 31, 2012 11:36 pm
Score: 8 Give a positive score

Re: Collisions as a conditional parameter

Postby bamby1983 » Thu Mar 21, 2013 2:28 am

I tried using the approach in this thread but it doesn't seem to work. Here's the code I wrote for the function that checks whether 2 actors collide with each other. The function is designed so that it can accept the actor names dynamically. I removed the if statement safeguard against zero collisions as my background ensures that at least 1 actor always collide with the object.

Code: Select all
int checkCollision(char actor_1[30], char actor_2[30]) {
    int n; // Used to store the number of actors colliding with event actor
    int i;
    Actor *actors; // Declares an Actor* pointer (used to point to an array of actors event actor is colliding with)
 
    actors=getAllActorsInCollision(actor_1, &n); // The &n passes the address of the variable n, so the function can change the value
    // Actors now points to an array of n actors, where n is equal to the number of actors colliding with the actor
    // if no actors are colliding, actors is set to null
    for(i=0;i<n;i++) {
        if(strcmp(actors[i].name, actor_2)==0) // Check whether actor_1 collides with actor_2
            return 1;
                     }
       return 0;
                               }



Here is how I call this function when trying to check whether two objects collide with each other:

Code: Select all
if (checkCollision("Hover_Table_Right", "Key")) {
            // Some code here
                                              }
        else {
            // Additional code here
             }


The code in the else statement always gets executed instead of that in the if statement, regardless of whether or not the two objects collide. Could someone please let me know whether I've missed something and have gone wrong somewhere?
bamby1983
 
Posts: 112
Joined: Tue Jul 31, 2012 11:36 pm
Score: 8 Give a positive score

Re: Collisions as a conditional parameter

Postby skydereign » Thu Mar 21, 2013 4:34 am

That's because getAllActorsInCollision requires a clonename, not an actor name. So it needs to be more specific than just "Hover_Table_Right". I'll fix this though, as you would think it would work like other gE functions and default to the lowest indexed. For now though, you'll have to use "Hover_Table_Right.0" instead.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Collisions as a conditional parameter

Postby bamby1983 » Thu Mar 21, 2013 2:16 pm

Thank you, Sky! This fixed the problem! :)

I also learnt that using "Event Actor" in place of the clone name when the same clone calls the function yields successful results as well.
bamby1983
 
Posts: 112
Joined: Tue Jul 31, 2012 11:36 pm
Score: 8 Give a positive score

Re: Collisions as a conditional parameter

Postby skydereign » Thu Mar 21, 2013 6:32 pm

Right, Event Actor is normally what you would use. There is also "Collide Actor" for the actor you are colliding with, "Parent Actor" for the event actor's parent, and "Creator Actor" for the actor that created the event actor. Though some may not be implemented quite correctly for some functions in 1.4.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Collisions as a conditional parameter

Postby bamby1983 » Fri Mar 22, 2013 2:58 am

Thanks, that's useful to know.
bamby1983
 
Posts: 112
Joined: Tue Jul 31, 2012 11:36 pm
Score: 8 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron