Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: GoToHellDave on Thu 06/09/2012 07:29:49

Title: Acquiring and storing a character's position
Post by: GoToHellDave on Thu 06/09/2012 07:29:49
Hi Guys.

I was wondering if anybody knows of a way to acquire and store a characters position. What I'm attempting to do is have the protagonist combine two inventory items, be transported to a room, say a few lines and then be transported back to the exact room and position where he was before he combined the items. Any help would be greatly appreciated.

Ben.
Title: Re: Acquiring and storing a character's position
Post by: ddq on Thu 06/09/2012 07:53:34
Variables are perfect for this task! Global ones especially so. Make two global variables of type int. Call them something like temp_x and temp_y. Before you change rooms, set temp_x = player.X and temp_y = player.Y and when you call the ChangeRoom function to go back to the original room, pass the temp variables as additional parameters. There's other ways of doing this, but that's the simplest and most useful way. Check out the manual if you have any trouble.
Title: Re: Acquiring and storing a character's position
Post by: Crimson Wizard on Thu 06/09/2012 09:14:28
Okay, I know this is nitpicking (or probably it isn't?) and not related to the question directly, but calling global variables "temp_x" and "temp_y" is a big step on a path to madness. It is strongly recommended to give descriptive names to global (and not only) variables so that you always remember what they are used for. Like "character_temp_x", for example.
Title: Re: Acquiring and storing a character's position
Post by: GoToHellDave on Thu 06/09/2012 10:03:18
Thanks for the replies. I'll give it a go and let you know how it went.
Title: Re: Acquiring and storing a character's position
Post by: Khris on Thu 06/09/2012 10:07:21
Also, for characters, x and y are not capitalized. It's player.x and player.y. But it'll show up in the auto-complete window anyway.
Title: Re: Acquiring and storing a character's position
Post by: GoToHellDave on Thu 20/09/2012 12:39:52
Apologies for the late reply. I'm sure you guys don't need me to tell you that when you're making a game you tend to get sidetracked  (laugh)

Anyway. I'll start off by admitting that I'm not a particularly amazing programmer. In our little two-man team I'm effectively just the least-crappy programmer, so I may seem like a bit of a moron with the following questions.

I've attempted to implement what you guys have suggested, but I'm getting an error message because when I try to use the variables I've created in the 'changeRoom' function it gets confused because they aren't numerical symbols.

In the global script I have written the following code:

Quoteint character_temp_x = 0;
int character_temp_y = 0;
int character_temp_Room = 0;

function iFF7_UseInv()
{
  if (cDave.ActiveInventory == iPen)
  {
    character_temp_x = player.x;
    character_temp_y = player.y;
    character_temp_Room = player.Room;
    cDave.AddInventory(iFFPass);
    cDave.LoseInventory(iFF7);
    cDave.LoseInventory(iPen);
    cDave.ChangeRoom(6, 0, 0);
  }
}

As you can see, what I've done there is create three integers, one for the x position, the y position and the room number. Then I've programmed it to check if the character is combining 'iPen' with 'iFF7' and if he is, set the variables to their relevant numbers and transport the player to room 6.

In the code for room 6, I have done the following:

Quotefunction room_AfterFadeIn()
{
  cDave.Say("Will the bouncer actually fall for this and let me into the club?");
  cDave.Say("If not then perhaps I can find some other use for it");
  cDave.ChangeRoom(character_temp_Room, character_temp_x, character_temp_y);
}

As you can see here, the character enters the room, says a few lines and then is transported back to his previous position. I'm fairly certain that I shouldn't be putting the names of int variables in the brackets of the ChangeRoom function. If one of you could point me in the right direction then I'd be extremely grateful.

Ben.
Title: Re: Acquiring and storing a character's position
Post by: Crimson Wizard on Thu 20/09/2012 13:03:43
When you make variables that you want to use in different script (like you created vars in GlobalScript and want to use them in room script) then you must export them.
In GlobalScript.asc add this:
Code (ags) Select

export character_temp_x, character_temp_y, character_temp_Room;

In GlobalScript.ash add this:
Code (ags) Select

import int character_temp_x;
import int character_temp_y;
import int character_temp_Room;


Now the room scripts can "see" those variables and use them.
Title: Re: Acquiring and storing a character's position
Post by: Khris on Thu 20/09/2012 13:06:28
There's also the relatively new Global variables pane in the project tree where you can create those without all the import export business.
Title: Re: Acquiring and storing a character's position
Post by: GoToHellDave on Thu 20/09/2012 13:11:23
Once again, thanks for the swift replies. I'll give it a try and let you know how it goes.
Title: Re: Acquiring and storing a character's position
Post by: GoToHellDave on Thu 27/09/2012 14:36:49
I've just got around to attempting to implement your suggestions.

Everything seems to work well until it comes time to transport the player back to the original room. The game then crashes with an error message reading:

QuoteError: Load-room: unable to load the room file 'room214.crm'

At current my code looks like this:

    In the global script I have
   
Quoteint character_temp_x = 0;
int character_temp_y = 0;
int character_temp_Room = 0;

function iFF7_UseInv()
{
  if (cDave.ActiveInventory == iPen)
  {
    character_temp_x = player.x;
    character_temp_y = player.y;
    character_temp_Room = player.Room;
    cDave.AddInventory(iFFPass);
    cDave.LoseInventory(iFF7);
    cDave.LoseInventory(iPen);
    cDave.ChangeRoom(6, 0, 0);
  }
}

export character_temp_x, character_temp_y, character_temp_Room;

I had originally put 'export character_temp_x, character_temp_y, character_temp_Room' inside the function and had hoped that it was the cause of the issue. However, sticking it outside the function as is seen above has not rectified the issue.

    ...anyway, in the room 6 script I have
   
Quoteimport int character_temp_x;
import int character_temp_y;
import int character_temp_Room;

function hHotspot1_AnyClick()
{
  cDave.Say("I wonder if the bouncer will fall for this");
  cDave.Say("If not then maybe I can find some other use for it");
  cDave.ChangeRoom(character_temp_x, character_temp_y, character_temp_Room);
}

As always, your help is very much appreciated. Hopefully you can show me what schoolboy error I've made this time.

Ben.
Title: Re: Acquiring and storing a character's position
Post by: Crimson Wizard on Thu 27/09/2012 14:58:08
You are putting parameters in wrong order.
It should be
Code (ags) Select

cDave.ChangeRoom(character_temp_Room, character_temp_x, character_temp_y);


I.e: room index, x, y
Title: Re: Acquiring and storing a character's position
Post by: GoToHellDave on Mon 01/10/2012 12:42:17
Wow. I feel like a total idiot.

Thanks a lot guys, it works perfectly now.