GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2009-10-04 18:26:01

Pixelated_Pope
Member
Registered: 2009-07-30
Posts: 24

Only one instance behaving properly. [SOLVED! THANKS FLATLANDER!!]

So I have an enemy object.  The enemy object runs a script that controls its behavior.  The core idea is that it will do one of three things.

If it is far away from the target (the player) move towards it or hang out for a bit (this is randomized so that the enemy isn't just making a b-line for the player).
If it is in range, play the attack animation.

Now, the way I've set up animations for the monster is a timeline.  The timeline controls all animations and some variables. 

So, here's where things get weird.  I have two (or more, it doesn't matter) instances of the enemy object.  They will hop/move/idle, independently and correctly.  They can even target different players from one another.  However, only the first instance that was created will actually attack. 

Here's a bit of code that shows the enemy behavior.

Expandtimer -=1;
if ((timer < 0) && (busy == 0))
{
    //Decide on a target
    target = instance_nearest(x,y,players);

    //If in range of target, attack it.  If not, move towards it.
    if (distance_to_point(target.x, target.y) <10 && attacking == 0)
    {
        timeline_position = 21; //position 21 is where the attack animation begins 
        attacking = 1; //this variable is set back to 0 at the end of the attack animation in the timeline.
        busy = 1;    
        timer = 90;
        speed = 0;
        height = 0;    
    }
    else
    {
        behavior=floor(random(2));
        if behavior = 0 //idle
        {
            timeline_position = 3; //position 3 is where the idle animation begins.
            walkSpeed = 0
            speed = walkSpeed
            timer = 30
        }
        else //Hop
        {
            timeline_position = 5; //position 5 is where the hop animation begins.
            walkSpeed = 1;
            move_towards_point(target.x, target.y, walkSpeed)
            timer = 20; //number of frames for 1 jump
        } 
    }
}

So, the weird thing is I've set a watch on both instances of the object.  And attacking will get set to 1, but the timeline never finishes for that instance (I never even really see it start) so attacking stays at 1.  Sure, I can go set attacking back to 0 somewhere else, but the big problem is why is that timeline not taking off for the second instance of the object when it is working perfectly for the first instance.  And obviously the timelines can work independently of the instances because both enemies can hop towards the different players at the same time but not be perfectly in sync.

My buddy and I have been banging our heads against this problem for days, and am finally breaking down and bothering you fine people.

Thanks in advance.

Last edited by Pixelated_Pope (2009-10-06 18:19:56)

Offline

#2 2009-10-06 13:05:48

Pixelated_Pope
Member
Registered: 2009-07-30
Posts: 24

Re: Only one instance behaving properly. [SOLVED! THANKS FLATLANDER!!]

...Am I not giving enough information?  Or is this problem just too bizarre?  If it would help to get a hold of our .gm7 file (it's registered only, btw) I would be willing to do that... we are pretty desperate.  Our project is stuck until we can figure out what's going on.

Offline

#3 2009-10-06 14:43:20

Flatlander
Member
Registered: 2009-09-03
Posts: 9

Re: Only one instance behaving properly. [SOLVED! THANKS FLATLANDER!!]

Maybe 'attacking' has accidentally been set as global so when one instance is attacking another one can't
or
the line:

Expandif (distance_to_point(target.x, target.y) <10 && attacking == 0)

Needs some extra brackets i.e.:

Expandif ( (distance_to_point(target.x, target.y) <10) && (attacking == 0) )

But I doubt it!

Offline

#4 2009-10-06 14:45:18

Pixelated_Pope
Member
Registered: 2009-07-30
Posts: 24

Re: Only one instance behaving properly. [SOLVED! THANKS FLATLANDER!!]

Yeah, attacking is definitely being set independently, I've set watches on the local variables for both instances and Attacking is always being toggled independently.  As for the extra parentheses, yeah, that's not going to help, otherwise the first instance wouldn't function properly either.

Offline

#5 2009-10-06 16:50:47

Flatlander
Member
Registered: 2009-09-03
Posts: 9

Re: Only one instance behaving properly. [SOLVED! THANKS FLATLANDER!!]

You appear more experienced than me but I'm happy to look at your .gm7. (You've got me hooked now.)
r e s p o n s e <at-goes-here> t h e m i d d le h o u s e . c o . u k

Chris

Last edited by Flatlander (2009-10-06 16:51:56)

Offline

#6 2009-10-06 17:04:38

Pixelated_Pope
Member
Registered: 2009-07-30
Posts: 24

Re: Only one instance behaving properly. [SOLVED! THANKS FLATLANDER!!]

Sent.

Offline

#7 2009-10-06 18:23:04

Pixelated_Pope
Member
Registered: 2009-07-30
Posts: 24

Re: Only one instance behaving properly. [SOLVED! THANKS FLATLANDER!!]

Good thing I sent it to you, the problem would never have been found by the posted code.  We were declaring "busy" in the script specifically for the object rather than individual instances.

Expandexecute_string("busy = "+exeObj+".busy");

Totally unnecessary.   I commented out that line of code and everything works swimmingly!  Thanks again, Flat.  You just cured a gigantic headache.

Offline

Board footer

Powered by FluxBB