Help wanted to get the M key to bring up the Map Room

Started by alphadalekben, Tue 15/07/2014 17:44:08

Previous topic - Next topic

alphadalekben

I have hit a dead end in coding my Map Screen in-game. I want to be able to bring up the Map Screen when the M key is pressed. But I keep getting error messages when I try and run the game. Here's what the code currently looks like;

Code: ags

function MapScreen;

if ((keycode == eKeyM) 
{
  cJack.ChangeRoom(1)
}


Would I also need to move said script away from the Global Script or is it ok where it is?

Finally, can somebody please demonstrate a way of stopping the script from working in the Map Screen itself?

Cassiebsg

You not specifying what errors you are getting, but I can see by what your code, that you have one ( too many... or a ) too less... also you missing a ; at the end of the changeroom line. and you also seem to be missing the {} that open and close the function...
Hope this helps.

Code: ags

function MapScreen;
{ 
  if (keycode == eKeyM)
    {
    cJack.ChangeRoom(1);
    }
}

As getting the script to stop am not sure what you mean?
I probably would suggest that you do a GUI for the map, if it's just an image to be displayed, and not an interactive room where you can move the player and have stuff happen.
There are those who believe that life here began out there...

Khris

This is still wrong; apart from the superfluous semi-colon, this function is never called.

There's no need for a custom function here; all you need to do is find the on_key_press function in GlobalScript.asc; it already contains code for several buttons.
Just add this to the end, before the function's final closing brace: }
Code: ags
  if (keycode == eKeyM) {
    if (player.Room != 1) player.ChangeRoom(1);
    // uncomment the following to make M also go back
    // else player.ChangeRoom(player.PreviousRoom);
  }

Edit: fixed error in code

alphadalekben

Thanks. I've added Khris' code and I get this error message.

GlobalScript.asc(213): Error (line 213): static non-property access: internal error

Hopefully it's just a silly mistake, but hey.

Also, would I be able to have the Tab button as a second Map key, so if you were playing on a laptop, you'd use the Tab key instead of the M key?

Khris

I had "eKeyCode" instead of "keycode".
To add a second key, just use
Code: ags
  if (keycode == eKeyM || keycode == eKeyTab)

alphadalekben

#5
The code is adapted. Another issue has arisen with a code i haven't touched.

GlobalScript.asc(221): Error (line 221): Nested functions not supported (you may have forgotten a closing brace)

Here's what the code looks like

[code/ags]
if (keycode == eKeyM || keycode == eKeyTab) {
    if (player.Room != 1) player.ChangeRoom(1);
    // uncomment the following to make M also go back
    // else player.ChangeRoom(player.PreviousRoom);
  }


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)
[code/ags]

Also, how do I make it so that, for example, if the player walks onto the palm tree object on the map, it takes him to the desert. Does that make any sense?

EDIT: I changed 'keycode' to 'eKeyCode'

Vincent

Quote
GlobalScript.asc(221): Error (line 221): Nested functions not supported (you may have forgotten a closing brace)

Code: ags

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)
  }
}

Khris

alphadalekben, you probably have pasted my code in the wrong place, which is why I specifically told you where to put it.

Your script is supposed to look like this:

Code: ags
function on_key_press(eKeyCode keycode)
{  // <- function's opening brace

  // lots of existing code here, indented

  // add my code here

}  // <- function's closing brace


The error you get typically occurs when AGS encounters the "function" keyword but the previous function has not been closed yet. My guess is you accidentally removed on_key_press's closing brace when pasting my code snippet.

As for a character's walking on something triggering an event: draw a region on the background, then use the region's "player walks onto" event.

To post code, use: [code=ags] ... [/code]

alphadalekben

All fixed now thanks. :grin: By the way, Khris, what should the 'Player walks onto' event look like if it is set to go to Room 2 (both in the bottom right Events window and in the Global or Room Script (depending on what's better to use for this)?

Hopefully I make sense there.

Khris

You add an event just like described in the tutorial:
-click the "player walks onto" cell, and you should get a [...] button in the empty cell next to it.
-click that button and AGS will create the function in the appropriate place (in this case: the room script), naming and linking it
-add code inside the function

You should see a function added called region1_walksOnto or similar, and that very name should now appear in the cell next to the event name.
This is how you can link a function to a game event.

You did do the manual's tutorial, right...?

alphadalekben

At what point does the tutorial tell you how to do regions? I can see hotspots but no reasons.

Slasher

There IS a Region events (and functions and properties) topic section in the manual if you type Region Events in the manual search.

Apply Regions as you do with Hotspots.

Add events to them as how Khris said.

alphadalekben

How do I get the region selector out of erasor mode and into a mode where i can select what region i want?

Khris

Exactly like with hotspots, there's a dropdown menu at the very top of the properties window where you can select the current region; region 0 acts as eraser.

alphadalekben

I've put in the script to get the room to change to the desired location. Now, I just need the character to spawn on the walkable area.

These are the character's X and Y coordinates when he spawns when the game starts up (in the desert area).

StartX: 238
StartY: 224

Khris

Code: ags
  player.ChangeRoom(2, 238, 224);


But this is not how these forums are supposed to be used. Your problems are extremely trivial, and the Beginner's Tech forum is not a replacement for trying to figure things out yourself using the tutorials and reference.
When I discovered AGS I spent weeks reading through the manual and forums. I'm not suggesting you absolutely have to do the same, but given that the answer to your problem is right in the manual entry for the command you are already using, maybe keeping the helpfile handy couldn't hurt.

alphadalekben

#16
Oh, right. Sorry for the misunderstanding. Thanks for all your help though :smiley:.

I'll be sure to keep your advice in mind.

alphadalekben

Another minor issue here. I'm trying to get it so that the character spawns over the area icon (cactus for the desert etc (the ones I've drawn the regions over and I haven't set anything in the 'While standing on function' so nothing should trigger there). The issue is that I keep being sent back to the starting room.

Here is the code currently;

Code: ags

if (keycode == eKeyM) {
    if (player.Room != 1) {
      if (player.Room ==2) player.ChangeRoom(1, 160, 120);
      if (player.Room ==3) player.ChangeRoom(1, 312, 173);
      if (player.Room ==4) player.ChangeRoom(1, 275, 119);
    }
    else player.ChangeRoom(player.PreviousRoom);


I assume that the error occurs from the 'else' statement, but how do I go about fixing it?

Khris

While you don't have "while standing on" events, you are still moving the player there and thus triggering the "walks onto" events of the regions.
Here's what you can do to fix this:

Code: ags
Region *enteredRegion;

function room_Load()
{
  Region *r = Region.GetAtRoomXY(player.x, player.y);
  if (r.ID) { // if actual region, since no region returns region #0
    enteredRegion = r;
    r.Enabled = false;
  }
}

function region15_WalksOnto()
{
  enteredRegion.Enabled = true;
}


For this to work, use region 15 to flood fill the entire room around the regions that trigger the room changes. When the character first arrives, the region they end up on is disabled, then turned back on as soon as they step off it.
Naturally, you need to link the room's "before fadein" and region 15's "walks onto event" just like always, simply pasting this will, again, not work.
The order doesn't matter, as long as the Region declaration in line 1 is somewhere above the two functions. If you are already using room_Load(), just add my code at the start to the existing function.

alphadalekben

Ah, cheers, Khris. I didn't think of that. Everything works well now, thank you very much. You know, with the amount you've done for me, I'll have to give you a credit in the finished game huh?

SMF spam blocked by CleanTalk