Problem using AreThingsOverlapping with other conditions

Started by Belligerent Gnome, Tue 23/11/2010 10:00:21

Previous topic - Next topic

Belligerent Gnome

Alright, so, here's the story.

I've been trying to invoke a reaction from one character when another does something while the two are colliding. I spent nearly an entire day trying to figure it out and eventually got it working before, but then my computer finally died and I lost it.  :-\  So I recently decided to start again, and it's generally been more organised than it's last incarnation, but then I hit that roadblock again. Here's the code I'm currently trying to get working:

if ((AreThingsOverlapping(CHAR, CHAR1)) &&
player.Loop == 4){
cChar1.LockView(4);
cChar1.Animate(0, 0, eOnce, eBlock, eForwards);
cChar1.UnlockView();
}

So I put this in the rep_exec portion of the room, but when I test it and trigger the animation during collision, there's no response from the other character. I've tried countless variations of the code, swapping the two conditions, changing the second one... I even dropped the AreThingsOverlapping in favor of other, similar things. But the only time it worked was when I dropped the player.Loop or equivalent code completely, leaving AreThingsColliding the only condition and making the entire point of the code obsolete. Like I said, I did it before, but only after spending several hours trying even after consuming two cups of coffee.

Help would be greatly appreciated.

Sslaxx

Well, what are the names of the characters? You're looking for two characters overlapping? The if statement looks rather wrong to me. If you're checking with two characters, cChar1 and cChar2:
Code: ags
if (AreThingsOverlapping (cChar1.ID, cChar2.ID) && player.Loop == 4)
{
  cChar1.LockView(4);
 cChar1.Animate(0, 0, eOnce, eBlock, eForwards);
 cChar1.UnlockView();
}

Note the .ID tag - AreThingsOverlapping works with the integer numbers representing things.

What version of AGS are you running? You should be able to use Character.IsCollidingWithChar(Character* otherChar).
Code: ags
if (cChar1.IsCollidingWithChar (cChar2) && player.Loop == 4)
{
  cChar1.LockView(4);
 cChar1.Animate(0, 0, eOnce, eBlock, eForwards);
 cChar1.UnlockView();
}
Stuart "Sslaxx" Moore.

Khris

Sslaxx: He didn't get parse errors or the like, i.e. his code does work fine in principle.

The problem is very probably this:

When you animate cChar using Loop 4, you're also using the eBlock parameter which in turn prevents the room's rep_exec from running.

Now there's repeatedly_execute_always() which continues to run during blocking stuff, but that in turn can't call blocking commands.

The way to solve this is to simply make all the animations non-blocking (using eNoBlock). Since you're probably aware of that possibility, I guess you purposely made it blocking to avoid issues, those can usually be avoided by other means though. A bit more info is needed.

Belligerent Gnome

Ah, yes! Now I remember using the repeatedly_execute_always function. It still isn't working, but I tested it with the block on once and it closed with an error when I tried to animate during the collision, which is a good sign if only for the fact that it means it's paying attention to the code. Now the problem is that, for some reason, it won't animate if the block isn't on. Is there a way to solve that? Thanks for your help, by the way.

Khris

Could you post the code you're using to animate cChar?

I suspect the code you have right now calls cChar.Animate every game loop which prevents the animation from ever showing.

You need to use something like this:

Code: ags
  if (!cChar.Animating) cChar.Animate(...);

Belligerent Gnome

Sure.

function on_key_press(eKeyCode keycode)
{
if (keycode == eKeyQ)
  {
    cChar.LockView(1);
    cChar.Animate(4, 0, eOnce, eBlock, eForwards);
    cChar.UnlockView();
    cChar.Loop = 0;
  }
}

Does on_key_press run every game loop? Sorry, I'm something of a n00b.  :P

Khris

Try this:

Code: ags
function on_key_press(eKeyCode keycode)
{
  if (keycode == eKeyQ)
  {
    if (!cChar.Animating) cChar.Animate(4, 0, eOnce, eNoBlock, eForwards);
  }
}


The problem now is that the loop isn't reset after the animation.
Is the character at one point going to have four different animation loops for what I guess is their attack animation?
Because in that case you could put this in repeatedly_execute:

Code: ags
  if (!cChar.Animating && cChar.Loop > 3) cChar.Loop -= 4;


This will reset the character's loop to the default walking in the range of 0-3 after the animation. In the meantime, it'll also reset the loop from 4 to 0.

Belligerent Gnome

Well, that gets it to animate without the blocking on, but there's still no reaction from cChar1.  :-\
Sorry for all the trouble.

Khris

Debug it then. Put a label on a GUI, then add this to the room's rep_exe:

Code: ags
  lblDebug.Text = String.Format("%d %d", AreThingsOverlapping(CHAR, CHAR1), player.Loop);

(AreThingsOverlapping() is boolean, printing it like an int should work though and produce 0/1).

Belligerent Gnome

The debugger seems to take notice, both numbers go up pretty high when cChar gets close.

Khris

With my code? It should show "1 4", not high numbers.

Belligerent Gnome

Well, the first number gets higher the closer cChar gets to cChar1 (up to 28), and the second number only goes up to four when the loop is running but seems to randomly jump to four when it isn't.

I'm going to try working at this from a different angle, I'll keep you posted.

Matti

Quote from: Belligerent Gnome on Wed 01/12/2010 16:33:56
Well, the first number gets higher the closer cChar gets to cChar1 (up to 28)

What?  ???

The number should be either 0 (=false) or 1 (=true). I wonder what's going on there..

Khris

AreThingsOverlapping might be defined as an int and return an "overlapping index".

Ryan Timothy B

The manual says this:
QuoteReturns 0 if they are not overlapping, or the overlapping amount if they are. This amount is an arbitrary scale, but 1 means they are just about touching, all the way up to higher numbers for more overlappingness.

I guess it just gives you the option to check if it's overlapping to your requirements and not just 1 pixel of each corner returning true. Makes sense to me.

Matti


Belligerent Gnome

Okay, using a few arrays I was able to get it working. Now, when I press "Q", it sets my array to 1. When the array is 1, it runs the animation, allowing me to replace the condition "cChar.Loop" with "whatever == 1".
I then altered:

"if (!cChar.Animating && cChar.Loop > 3) cChar.Loop -= 4;"
to
"if (!cChar.Animating && cChar.Loop > 3){
cChar.Loop -= 4;
whatever[player.ID] = 0;
}"

It's a bit more complex, and I still have no idea why AreThingsOverlapping won't co-operate with "cChar.Loop", but it's working. I'm sure that there's an easier way to do this, but when it comes to programming I have a tendency to do things in a very roundabout way. Oh, and it's an array because it will be commonly used by several characters. Thanks for your help.

SMF spam blocked by CleanTalk