Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Uhfgood on Thu 26/07/2012 06:47:35

Title: Getting pc to walk to an object and THEN interact with it?
Post by: Uhfgood on Thu 26/07/2012 06:47:35
I set up a hotspot, and was trying to get it to walk there before interacting with it.  There was a problem because if I was using the walk cursor, it wouldn't interact, and the interact cursor wouldn't walk.  So I solved it by writing code in the global script that basically made the pc walk, and then process click, and it worked fine.

However when I cut the code from that hotspot and tried to put it into an object's code, it will interact directly without walking over there, and if I comment out the line ProcessClick he will walk to the position but not interact with it at all.

So basically I want it to walk to the object I clicked on, and then do the interactions.  Below is some code.

please note this is in GlobalScript.asc:
Code (AGS) Select
function on_mouse_click(MouseButton button) // called when a mouse button is clicked. button is either LEFT or RIGHT
{
if (IsGamePaused() == 1) // Game is paused, so do nothing (ie. don't allow mouse click)
{
}
else if (button == eMouseLeft)
{
if( gPieMain01.Visible == false )
{
gPieMain01.SetPosition( mouse.x - (gPieMain01.Width / 2),  mouse.y - (gPieMain01.Height / 2) );

if( gPieMain01.X < -44 ) gPieMain01.X = -44;
if( gPieMain01.Y < 60 - 44 ) gPieMain01.Y = 60 - 44;

if( gPieMain01.X + gPieMain01.Width - 44 > 640 ) gPieMain01.X = (640 - gPieMain01.Width) + 44;
if( gPieMain01.Y + gPieMain01.Height - 44 > 420 ) gPieMain01.Y = (420 - gPieMain01.Height) + 44;

gPieTextOverlay.X = gPieMain01.X - 64;
gPieTextOverlay.Y = gPieMain01.Y - 64;

//if( (gPieMain01.Y + gPieMain01.Height) > 420 ) gPieMain01.Y = 420 - gPieMain01.Height;

gPieMain01.Visible = true;
gPieTextOverlay.Visible = true;

startx = gPieMain01.X + (gPieMain01.Width / 2);
starty = gPieMain01.Y + (gPieMain01.Height / 2);
}
else
{
pieOption = subOption;
if( mouse.x > (gPieMain01.X + 54) && mouse.x < ( (gPieMain01.X + gPieMain01.Width) - 54) )
{
if( mouse.y > (gPieMain01.Y + 54) && mouse.y < ( (gPieMain01.Y + gPieMain01.Height) - 54) )
{
pieOption = 0;
}
}

clear_screen();
gPieMain01.Visible = false;
gPieTextOverlay.Visible = false;
cEgo.Walk(mouse.x + GetViewportX(), mouse.y + GetViewportY());
ProcessClick(startx, starty, eModeInteract);
}
}

}


This next code is in the room script:
Code (AGS) Select
function hPainting_AnyClick()
{

}

function hCrystalBall_AnyClick()
{
}

function oPainting_AnyClick()
{
if(pieOption == 0)
{
}
else
if(pieOption == 11)  // look at
{
if( lookAtCount == 0)
{
lookAtCount++;
cEgo.Say("It's a painting of a top-down view of the room.");
}
else
if( lookAtCount == 1)
{
lookAtCount++;
cEgo.Say("This was taken...er..painted before a few additions,");
cEgo.Say(" like the rocking chair and the giant crystal ball.");
}
else
if( lookAtCount == 2)
{
lookAtCount++;
cEgo.Say("So it's actually a painting of a rug!");
cEgo.Say("I love that rug.");
cEgo.Say("Of course this was also before the rug was moved in front of the fireplace.");
}
else
{
int randLook = Random(2);

if(randLook == 0)
cEgo.Say("You don't need me to describe it for you!");
else
if(randLook == 1)
cEgo.Say("You've already seen the painting.");
else
if(randLook == 2)
cEgo.Say("It's the same painting you've looked at several times before.");
}
}
}


The hotspot is of course hPainting, the object being oPainting.

Any ideas?

Title: Re: Getting pc to walk to an object and THEN interact with it?
Post by: Khris on Thu 26/07/2012 08:21:28
You're using cEgo.Walk(x, y); which means that the blocking parameter uses the default, eNoBlock. In other words, the line is pretty much skipped/ignored since blocking code follows.
It should be cEgo.Walk(mouse.x + GetViewportX(), mouse.y + GetViewportY(), eBlock);

But, while it's clear why it doesn't work as intended, I'm wondering how this has worked for the hotspot. Did you set its WalkToPoint?
Title: Re: Getting pc to walk to an object and THEN interact with it?
Post by: Uhfgood on Thu 26/07/2012 23:16:18
Yeah I believe I did set a walkto point for the hotspot

I don't know if there's a walkto point for an object though.

Also using eblock means I can't do anything else while he's walking, when the player should be able to exit out of the current action at any time (unless this is impossible to do with ags)
Title: Re: Getting pc to walk to an object and THEN interact with it?
Post by: Uhfgood on Thu 26/07/2012 23:17:17
Also like I said, if I turn on eBlock, then it will walk but ignore the interaction.
Title: Re: Getting pc to walk to an object and THEN interact with it?
Post by: Khris on Fri 27/07/2012 00:48:11
No, objects don't have WalkTo points.

If the interaction is ignored, something else is wrong. I don't know what though, simply using eBlock shouldn't have any effect on the subsequent ProcessClick command. Weird.

Another thing you can try is my GotThere module (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=36383.msg477380#msg477380). In that case, remove the Walk line from on_mouse_click first though.
The walking to the hotspot/object/character is non-blocking, you need to state target coords and a direction and the interaction is only executed if the player reached the specified target. You also have to remove the walk to coords from the hotspots or at least turn off the relevant option in General settings.
Title: Re: Getting pc to walk to an object and THEN interact with it?
Post by: Uhfgood on Sat 28/07/2012 07:30:51
I'm unsure how to import an scm module.  Also where to stick the code for it?
Title: Re: Getting pc to walk to an object and THEN interact with it?
Post by: Khris on Sat 28/07/2012 09:27:25
You right click the scripts node and select Import, then the file.
How to use it is explained right in the post I linked, not sure what you mean by "where to stick the code".
Title: Re: Getting pc to walk to an object and THEN interact with it?
Post by: Uhfgood on Sat 28/07/2012 21:46:20
Where to call your code from it just says "function x..." --

But I'm assuming the call to "Got There" is going to be in the event handler for the object or hotspot in question?