An easier way to do an "object selection system".

Started by Cubefiend, Sun 05/01/2014 05:41:51

Previous topic - Next topic

Cubefiend

I have this whole system of unconventional object selection, you don't need to use a mouse to play the game- instead you use numbers 1-9 to select up to 9 objects or characters in a room.
Then you use 0 to turn on the four commands, once you have done that, you can look, interact, or talk. additionally, R is used to fully observe a room. Currently, the code must be pasted into each room. I am aiming for a decent sized game, so copy-pasting feels like a lot of work for me. If there is any simpler way to do the system or any fixes, it'd be appreciated. Below is the code.
Code: ags

function room_RepExec()
{


    //within the first if statements each number when pressed makes a cursor highlight the object
    if(IsKeyPressed(eKey1))
    {
    
      
      oHeader.Visible = true;
      oHeader.SetPosition(uno.X, uno.Y);
      ///uno
      
      }
      
     if(IsKeyPressed(eKey2))
    {
       oHeader.Visible = true;
      oHeader.SetPosition(dos.X, dos.Y);
     // dos
    }
     if(IsKeyPressed(eKey3))
    {
       oHeader.Visible = true;
      oHeader.SetPosition(tres.X, tres.Y);
     // tres
    }
    if(IsKeyPressed(eKey4))
    {
       oHeader.Visible = true;
      oHeader.SetPosition(quartro.X, quartro.Y);
     // quartro
    }
    if(IsKeyPressed(eKey5))
    {
       oHeader.Visible = true;
      oHeader.SetPosition(cinco.X, cinco.Y);
     // cinco
    }
    if(IsKeyPressed(eKey6))
    {
       oHeader.Visible = true;
      oHeader.SetPosition(ses.X, ses.Y);
      
     // ses
    }
    if(IsKeyPressed(eKey7))
    {
       oHeader.Visible = true;
      oHeader.SetPosition(ceate.X, ceate.Y);
    //  ceate
    }
    if(IsKeyPressed(eKey8))
    {
       oHeader.Visible = true;
      oHeader.SetPosition(ocho.X, ocho.Y);
    //  ocho
    }
    if(IsKeyPressed(eKey9))
    {
       oHeader.Visible = true;
      oHeader.SetPosition(nueve.X, nueve.Y);
    //  nueve
    }
      //wtihin this loop is the command loop to look, interact, talk, or obvserve
  if(gGui2.Visible)
      {
        if(IsKeyPressed(eKeyR))
        {
          cCharacter.Say("A short, detailed description of the room.");
        }
       if(oHeader.X == uno.X)
       //repeat for every object in the room
       {
        if(IsKeyPressed(eKeyQ))
        {
          cCharacter.Say("I am looking");
          gGui1.Visible = true;
          gGui2.Visible = false;
        }
        if(IsKeyPressed(eKeyW))
        {
          cCharacter.Say("I am interacting");
           gGui1.Visible = true;
          gGui2.Visible = false;
        }
        if(IsKeyPressed(eKeyE))
        {
          cCharacter.Say("I am talking");
           gGui1.Visible = true;
          gGui2.Visible = false;
        }
       }
}
}
lauren merchant's magical adventure!

Khris

First of all, you can use on_key_press() in room scripts. Just add it to the script, no need to link it anywhere.

But, for this you need global code. In general, whenever you find yourself copy-pasting the same code in multiple places, you can be 99% sure that you're doing it wrong.
The objects of the current room can be accessed in a global context using the object[] array.
Here's a snippet:
Code: ags
  // inside global on_key_press

  if (k >= eKey1 && k <= eKey9) {
    int i = k - 48; // eKey1 == 49, eKey9 == 57
    if (i < Room.ObjectCount) {
      gHeader.SetPosition(object[i].X, object[i].Y);  
      gHeader.Visible = true;
    }
  }

Note that I'm using a GUI (gHeader), which will not only appear above everything else, but is also a global thing you only have to create once.

Also, you can center the GUI on the object:
Code: ags
  int x = (Game.SpriteWidth[object[i].Graphic]  - gHeader.Width) / 2;
  int y = (Game.SpriteHeight[object[i].Graphic] - gHeader.Height) / 2;


(I have substracted 48, meaning that pressing 1 will move the GUI to object[1]. The first object has an index of 0 though. You might want to subtract 49 instead.)

Edit: fixed missing brackets in second code snippet.

Cubefiend

Ah, that seems to do the trick, but the GUI centering didn't function right. Anyway, I have the system all set up now and I just need to refine some effects and such. Thank you.
lauren merchant's magical adventure!

Khris

Yeah, the centering code was missing the opening brackets.

SMF spam blocked by CleanTalk