Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Arjunaz78 on Thu 15/12/2011 03:42:37

Title: [SOLVE] Custom GUIs Button (Arrow Direction)
Post by: Arjunaz78 on Thu 15/12/2011 03:42:37
I've create a game with no character in it. The game similar to/or like-Myst games that only can move using custom gui arrow key button on click (total 4 arrow button GUI's, including forward,backward,right and left direction).

The question is, can i use a different code to make a player move on all direction without put a long script to it, because it got messy & confusing to myself to edit it along with other and many different games room section,currently i've create total of 19 rooms that include some of arrow gui button on certain room and all of 4 arrow GUI's button on other certain room.

Below is a one of the function on that arrow GUI's button that i use it on GlobalScript.

Button4 is a left button direction.


function Button4_OnClick(GUIControl *control, MouseButton button)
{
   if (player.Room == 3) {
   
     player.ChangeRoom(5);
   }

   if (player.Room == 5) {
   
     player.ChangeRoom(4);
   }

   if (player.Room == 6) {
     
     player.ChangeRoom(7);
   }
   
   if (player.Room == 8) {
     
     player.ChangeRoom(11);
   }
   
   if (player.Room == 12) {
     
     player.ChangeRoom(7);
   }
}


That means i've to put full/bucket of code on whole function in it while i create a new room right?
That's why i want to know if i can use a different code style (maybe using ARRAY?? or Modules?? or custom Global Variables??) or just continue put the whole of code in it.

THX.

Title: Re: Custom GUIs Button (Arrow Direction)
Post by: Khris on Thu 15/12/2011 04:06:46
Put this at the top of the global script:

int room[4];  // 0: left, 1: forward, 2: right, 3: back

function SetupMovement(int left, int forward, int right, int back) {

 room[0] = left;
 bLeft.Visible = (left > 0);

 room[1] = forward;
 bForward.Visible = (forward > 0);

 room[2] = right;
 bRight.Visible = (right > 0);

 room[3] = back;
 bBack.Visible = (back > 0);
}


Call the button to move left "bLeft" and put this in its OnClick:

function bLeft_OnClick(GUIControl *control, MouseButton button)
{
 player.ChangeRoom(room[0]);
}


Same for the other buttons, just change the index of the room[] array.

To set up the room connections, the easiest way is to use the global before-fadein event.
Just add this at the end of the global script:
function on_event (EventType event, int data) {

 if (event == eEventEnterRoomBeforeFadein) {

   if (player.Room == 1) SetupMovement(2, 3, 4, 0);  // left, forward, right, back
   if (player.Room == 2) SetupMovement(0, 5, 1, 0);
   ...
 }


Edit: array size corrected
Title: Re: Custom GUIs Button (Arrow Direction)
Post by: Arjunaz78 on Thu 15/12/2011 04:44:20
Now..it works..thank you very much khris..  :)
Title: Re: [SOLVE] Custom GUIs Button (Arrow Direction)
Post by: Arjunaz78 on Tue 20/12/2011 07:58:53
Owh..one more problem trigger now..

I just follow & use the code as you told me before..but the wierd thing comes like picture that i link it below..

http://imgur.com/Qcs6m

Well..below is the code that i've been use..

at the top of the global script i've use:

int room[3];

function SetupMovement(int left, int forward, int right, int back) {

  room[0] = left;
  Button4.Visible = (left > 0);

  room[1] = forward;
  Button1.Visible = (forward > 0);

  room[2] = right;
  Button3.Visible = (right > 0);

  room[3] = back;
  Button2.Visible = (back > 0);
}


For the button On Click i've use:

function Button4_OnClick(GUIControl *control, MouseButton button)
{
player.ChangeRoom(room[0]);
}

function Button1_OnClick(GUIControl *control, MouseButton button)
{
player.ChangeRoom(room[1]);
}

function Button3_OnClick(GUIControl *control, MouseButton button)
{
player.ChangeRoom(room[2]);
}

function Button2_OnClick(GUIControl *control, MouseButton button)
{
player.ChangeRoom(room[3]);
}


..and finally at the end of the Global Script i've use:

function on_event (EventType event, int data) {

  if (event == eEventEnterRoomBeforeFadein) {

    if (player.Room == 2) SetupMovement(0, 0, 0, 3);  // left, forward, right, back
    if (player.Room == 3) SetupMovement(0, 2, 5, 4);
    }
}


What the real error that cause it? did i miss something?
Title: Re: [SOLVE] Custom GUIs Button (Arrow Direction)
Post by: Gilbert on Tue 20/12/2011 08:18:45
When you declare int room[3];, the array only has 3 entries, i.e. with indices 0, 1 and 2.

If you want to have it taking values from 0 to 3 (i.e. there are 4 entries) you have to declare int room[4];.
Title: Re: [SOLVE] Custom GUIs Button (Arrow Direction)
Post by: Arjunaz78 on Tue 20/12/2011 09:14:46
Ok..now i got it..thanks mate..  :)
Title: Re: [SOLVE] Custom GUIs Button (Arrow Direction)
Post by: Khris on Tue 20/12/2011 09:48:40
Yep, my bad.