Acquiring and storing a character's position

Started by GoToHellDave, Thu 06/09/2012 07:29:49

Previous topic - Next topic

GoToHellDave

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.

ddq

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.

Crimson Wizard

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.

GoToHellDave

Thanks for the replies. I'll give it a go and let you know how it went.

Khris

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.

GoToHellDave

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.

Crimson Wizard

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

export character_temp_x, character_temp_y, character_temp_Room;

In GlobalScript.ash add this:
Code: ags

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.

Khris

There's also the relatively new Global variables pane in the project tree where you can create those without all the import export business.

GoToHellDave

Once again, thanks for the swift replies. I'll give it a try and let you know how it goes.

GoToHellDave

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.

Crimson Wizard

You are putting parameters in wrong order.
It should be
Code: ags

cDave.ChangeRoom(character_temp_Room, character_temp_x, character_temp_y);


I.e: room index, x, y

GoToHellDave

Wow. I feel like a total idiot.

Thanks a lot guys, it works perfectly now.

SMF spam blocked by CleanTalk