Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Egmundo Huevoz on Fri 22/12/2017 12:07:56

Title: Walkstraight using mouse coordinates isn't working as I think it should
Post by: Egmundo Huevoz on Fri 22/12/2017 12:07:56
Hi! I made a new mouse mode with which I want my character to shoot other characters. My idea was to summon a character (cBullet) and check whether or not it was colliding with the command AreThingsOverlapping. It worked as I intended, except for one thing. On the "on_mouse_click" part of the global script, I put this:
Code (ags) Select
   
if (mouse.Mode==eModeAttack){
    cBullet.ChangeRoom(cNacho.Room, cNacho.x, cNacho.y-50); //it spawns where it should
    cBullet.WalkStraight(mouse.x, mouse.y);} //Here's the problem, apparently

Although I THINK it should walk straight from cNacho to my cursor, it doesn't. It walks like 300 pixels above or below the cursor, like the cursor was in a totally different place. Any ideas?
Title: Re: Walkstraight using mouse coordinates isn't working as I think it should
Post by: Egmundo Huevoz on Fri 22/12/2017 13:18:16
Quote from: Egmundo Huevoz on Fri 22/12/2017 12:07:56
Hi! I made a new mouse mode with which I want my character to shoot other characters. My idea was to summon a character (cBullet) and check whether or not it was colliding with the command AreThingsOverlapping. It worked as I intended, except for one thing. On the "on_mouse_click" part of the global script, I put this:
Code (ags) Select
   
if (mouse.Mode==eModeAttack){
    cBullet.ChangeRoom(cNacho.Room, cNacho.x, cNacho.y-50); //it spawns where it should
    cBullet.WalkStraight(mouse.x, mouse.y);} //Here's the problem, apparently

Although I THINK it should walk straight from cNacho to my cursor, it doesn't. It walks like 300 pixels above or below the cursor, like the cursor was in a totally different place. Any ideas?

Edit: also, if I were to click on a character, how would I make it so the bullet goes to THAT character? I know it has something to do with "Character*" but I can't get it to work :(
Title: Re: Walkstraight using mouse coordinates isn't working as I think it should
Post by: Khris on Fri 22/12/2017 15:25:26
Please don't quote the entire previous post; there's a reply button at the bottom of the thread.

If we're talking about a scrolling room, keep in mind that mouse.x and mouse.y are screen coordinates, while Walk() commands expect room coordinates.
  cBullet.WalkStraight(GetViewportX() + mouse.x, GetViewportY() + mouse.y);

Clicking arbitrary characters can be handled using Character.GetAtScreenXY(mouse.x, mouse.y) in on_mouse_click
Title: Re: Walkstraight using mouse coordinates isn't working as I think it should
Post by: Crimson Wizard on Fri 22/12/2017 15:38:03
Perhaps it is better to use Move function with eAnywhere argument instead? WalkStraight does not have WalkWhere parameter for some reason, which means the bullet will only move across walkable areas, which is unreasonable IMO.
Title: Re: Walkstraight using mouse coordinates isn't working as I think it should
Post by: Egmundo Huevoz on Sun 24/12/2017 07:50:34
Thank you, Khris, it worked. Yeah, I quoted the entire post by mistake, I thought I was editing. It was late into the night and I didn't notice (roll)
CW, you're right, I did it like you said. As I was just in a test room filled with a walkable area, I didn't notice the potential problem.

Edit:
So far I got this, and it works:

Code (ags) Select
   
if (mouse.Mode==eModeAttack){
    Character* target = Character.GetAtScreenXY(mouse.x, mouse.y);
    cBullet.ChangeRoom(player.Room, player.x, player.y);
    cBullet.Move(GetViewportX() + mouse.x, GetViewportY() + mouse.y, eNoBlock, eAnywhere
    if (target!=null){
      target.Say("You killed me!");
      target.ChangeRoom(0);}
}


But then I decided I might want that to happen in other scripts, so I tried making a function called "shooting" at the top of the global script, looking like this:
Code (ags) Select

function Shooting(Character* target){
  target = Character.GetAtScreenXY(mouse.x, mouse.y);
  cBullet.ChangeRoom(player.Room, player.x, player.y);
  cBullet.Move(GetViewportX() + mouse.x, GetViewportY() + mouse.y, eNoBlock, eAnywhere);
  if (target!=null){
    target.Say("You killed me!");
    target.ChangeRoom(0);}
}

No errors until then, but then, under the "on_mouse_click", I try to call it:
Code (ags) Select

if (mouse.Mode==eModeAttack){
    Shooting(Character* target);
}


And it says this:
GlobalScript.asc(602): Error (line 602): static non-property access: internal error

I know I'm doing something wrong, but can't figure out what :P
Title: Re: Walkstraight using mouse coordinates isn't working as I think it should
Post by: Cassiebsg on Sun 24/12/2017 08:30:36

Shooting(cTarget);

Shooting(cBadguy);

etc... ;)
Title: Re: Walkstraight using mouse coordinates isn't working as I think it should
Post by: Egmundo Huevoz on Sun 24/12/2017 10:49:22
Quote from: Cassiebsg on Sun 24/12/2017 08:30:36

Shooting(cTarget);

Shooting(cBadguy);

etc... ;)
But if I had 120 characters, I would have to do that 120 times... I managed to make it so the game knows what character is under the mouse. The problem is I can't make it into a function.
Title: Re: Walkstraight using mouse coordinates isn't working as I think it should
Post by: Khris on Sun 24/12/2017 10:55:35
If you just want to move the code out of on_mouse_click, you don't need a Character parameter at all.
function Shooting() {
  // ...
}

  if (mouse.Mode == eModeAttack) {
    Shooting();
  }



Using an extender function works like this:
Code (ags) Select
function Shoot(this Character*) {
  int up = 30; // bullet is 30 pixels above characters' feet
  cBullet.ChangeRoom(player.Room, player.x, player.y - up);
  cBullet.Move(this.x, this.y - up, eNoBlock, eAnywhere);
}


In on_mouse_click()
  if (mouse.Mode = eModeAttack) {
    Character* c = Character.GetAtScreenXY(mouse.x, mouse.y);
    if (c != null) c.Shoot();
  }
Title: Re: Walkstraight using mouse coordinates isn't working as I think it should
Post by: Egmundo Huevoz on Sun 24/12/2017 17:53:51
Yeah Khris, your way is much neater, lol. But I still wanted to know the reason for the error, and now I too, thanks to you, too (laugh) Thank you!
BTW, and I don't mean to insist, just that I still don't know how notifications work on the forums. Did you get my last message about the shop system?

P.s: I don't know much of anything, but let me know if I can help you out somehow. You're helping me too much, and I'm starting to feel bad about it :-[ I can translate from English to Spanish, for example. Or help with a latin accent for voice acting :-D