Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: chessjaf on Thu 12/11/2020 14:41:00

Title: How to code a puzzle with many interactibles. Such as chess pieces on a board.
Post by: chessjaf on Thu 12/11/2020 14:41:00
(Hypothetical puzzle, similar to one I'm designing, but using chess pieces will make it easier to describe)

I want to have 6 chess pieces in my inventory.
I want a room that is a chess board viewed up close.
I want to be able to put all 6 chess pieces anywhere on the board, and have the ability to pick them back up again.

Does anyone have a recommendation on how to do this?

I believe I could make all 64 squares as separate objects, but this seems overly cumbersome.  I'm hoping someone will be able to quickly point me in the direction of a better option.

Thank you,

ChessJAF
Title: Re: How to code a puzzle with many interactibles. Such as chess pieces on a board.
Post by: Khris on Thu 12/11/2020 15:46:56
You don't need 64 objects, just six. Use one big hotspot for the chess board, and when it is clicked, use basic math to determine the coordinates, then place an object there.
For instance if you have an 8x8 board where each field is 20x20 pixels, and the top left corner is at 100, 10:
Code (ags) Select
  int x = ((mouse.x - 100) / 20) * 20 + 100;
  int y = ((mouse.y - 10) / 20) * 20 + 10 + (20 - 1); // add (20 - 1) to move to bottom coordinate

Now you have the bottom left coordinates of the field that was clicked.
Count up an object counter and use object[count].X = x;  and so on to position the object. You have to keep track of which of your object[0] to object[5] is currently on the board.

That's the gist at least :)
Title: Re: How to code a puzzle with many interactibles. Such as chess pieces on a board.
Post by: chessjaf on Thu 12/11/2020 19:05:57
Thank you, that is appreciated and exactly the sort of thing I was after :)