Problem with sequential puzzle

Started by Maverick, Tue 22/11/2005 20:16:57

Previous topic - Next topic

Maverick

I just started using AGS about 2 weeks ago and I have a problem with a sequential puzzle. The idea is to push blocks in a cetain sequence to open a door. I used the script that Strazer suggested in one of the old threads but it did not work. The blocks are animated when you interact with them but have to reset if the sequence is wrong. I created hotspots for each block and set up the code as follows:

In the room script

int door_sequence=0

I then used the interact interface to create "run script" for each of the hotspots as follows:

//sequence hotspot 1

if(door_sequence==0) {door_sequence++;
object[1].SetView(4);
object{1].Animate(1,3,eOnce,eBlock);}
else if (door_sequence==3);
{object[1].SetView;
object[1].Animate(1,3,eOnce,eBackwards);
object[2].SetView(4);
object[2].Animate(2,3,eOnce;eBackwards);
oject[3].SetView(4);
object[3].Animate(3,3,eOnce;eBackwards);
oject[4].SetView(4);
object[4].Animate(4,3,eOnce;eBackwards);}
else door_sequence=0;

I did the same for hotspot 2 and 3 except for the variable test that goes up by 1 everytime

//hotspot 4

if(door_sequence==3) {door_sequence++;
object[0].SetView(4);
object{0].Animate(0,3,eOnce,eBlock);}
else if(door_sequence!=3)
{object[1].SetView;
object[1].Animate(1,3,eOnce,eBackwards);
object[2].SetView(4);
object[2].Animate(2,3,eOnce;eBackwards);
oject[3].SetView(4);
object[3].Animate(3,3,eOnce;eBackwards);
oject[4].SetView(4);
object[4].Animate(4,3,eOnce;eBackwards);}
else door_sequence=0;


I don't know if I am even close or if the script even "lives' in the right place. Any help would be welcomed. I realise that the else if part won't work right because the counter will not reset if it contains the value 1 or 2 (think I can fix that)


Elliott Hird

I'm sorry, but I have do do this.

THIS HAS BEEN ASKED AT LEAST 3 TIMES IN THE PAST TWO DAYS! PLEASE, READ THE THREADS!

Thanks.

SSH

Maybe, Elliott, you could link to these threads, rather than just complain about them?
12

Maverick

Quote from: SSH on Tue 22/11/2005 21:31:41
Maybe, Elliott, you could link to these threads, rather than just complain about them?

Thanks!
I believe that it was inferred in my post that I did read all the threads FIRST. The latest solutions pertain to a GUI which I cannot use in my case hence I went with Strazer solution in another thread. Even if I use his code as is it does not work which means I'm probably putting the code in the wrong place. I added the rest of the code I used so someone can tell me whether Im on the right track or not.

In future I will include all the threads I consulted up to that point to avoid ending up in the dog box.

Wretched

This won't work as you don't seem to be checking if a block has already been pushed when you tell it to animate back. Also not sure if you have 4 or five blocks, so just change all 4's to 5's.

//Top of room_script

#define BlockNumber 4

int Sequence[BlockNumber];
int BlockState[BlockNumber];
int SequenceIndex;

//Init. Put this in first time player enters room
{
  //Your desired sequence
  Sequence[0]=3;
  Sequence[1]=2;
  Sequence[2]=0;
  Sequence[3]=1;
  //Sequence[4]=4;


  //All Boxes at start position
  BlockState[0]=0;
  BlockState[1]=0; 
  BlockState[2]=0;
  BlockState[3]=0;
  //BlockState[4]=0;

  SequenceIndex=0;
}

//Put this in room_script
function PushBlock(int Block)
{
   if (BlockState[Block]==1)
  {
     // Already pushed so do nothing
     return;
  }

  BlockState[Block]=1;
  //Animate Block moving forwards
  object[Block+1].SetView(4);
  object[Block+1].Animate(Block+1,3,eOnce,eBlock);

  if (Block==Sequence[SequenceIndex])
  {
     //Correct order
     SequenceIndex++;
     if (SequenceIndex==BlockNumber)
     {
        //Door opens
     }
  }
  else
  {
     //Wrong order so reset moved Boxes
     SequenceIndex=0;
     for (int b=0;b<BlockNumber;b++)
     {
         if (BlockState==1)
         {
           BlockState=0;
           //Animate Block moving back
           object[Block+1].SetView(4);
           object[Block+1].Animate(Block+1,3,eOnce,eBlock,eBackwards);
         }
     }
  }
}

Then in each hotspot script put

  PushBlock(Corresponding Block number);



It might be nicer to use none blocking when reseting the blocks so they all move back at the same time and add a check at the end of the script to ensure all blocks have finished animating, or just put a Wait(n) in with n sufficient to cover all blocks animations.



Maverick

#5
Thanks Wretched.
I tried to understand the code you sent me first so it took me a while to read up the topics in the manual before I used it (makes no sense to just copy it if I do not learn from it) in my game but unfortunately I'm getting all sorts of error messages when I want to save the room so I do not know whether it is going to work yet. Just for the record I'm only working with 4 blocks.

I'll use quotes from your code to better explain the problem areas:

//Init. Put this in first time player enters room

I take it that I use the room interactions button to create this under "first time player enters screen" then Run script


{
Ã,  Ã,  Ã, //Wrong order so reset moved Boxes
Ã,  Ã,  Ã, SequenceIndex=0;
Ã,  Ã,  Ã, for (int b=0;b<BlockNumber;b++)
Ã,  Ã,  Ã, 
I assume that this is all part of the text to explain what follows and that it does not form part of the actual script? If that is the case then int b is not defined anywhere hence error message " Undefined symbol 'b'. Where do I define b?

Thats all for now as I cannot test the script any further to see if anything else pops up.


???

Wretched

Hi, sorry I'm working in C++ at present, you need to do

int b=0;
while(b<BlockNumber)
{


.
.
.
.

  b++;
}


Maverick

#8
This may be a very stupid question but do I define it at the top of the room script where I declared the other variables or do I just substitute it where you had it in the first place (I have no scripting knowledge apart from what I have learned from the manual in the past two weeks). Is it possible to edit it into the code you posted yesterday?

Wretched

Ok, I'll be a bit more explicit;

This code goes at the very top of the room script file
Code: ags

// room script file

#define BlockNumber 4

int Sequence[BlockNumber];
int BlockState[BlockNumber];
int SequenceIndex;

function PushBlock(int Block)
{
Ã,  Ã, if (BlockState[Block]==1)
Ã,  {
Ã,  Ã,  Ã, // Already pushed so do nothing
Ã,  Ã,  Ã, return; 
Ã,  }

Ã,  BlockState[Block]=1;
Ã,  //Animate Block moving forwards
Ã,  object[Block+1].SetView(4);
Ã,  object[Block+1].Animate(Block+1,3,eOnce,eBlock);

Ã,  if (Block==Sequence[SequenceIndex])
Ã,  {
Ã,  Ã,  Ã, //Correct order 
Ã,  Ã,  Ã, SequenceIndex++;
Ã,  Ã,  Ã, if (SequenceIndex==BlockNumber)
Ã,  Ã,  Ã, {
Ã,  Ã,  Ã,  Ã,  //Door opens. Put your door opening code here
Ã,  Ã,  Ã, }
Ã,  }
Ã,  else
Ã,  {
Ã,  Ã,  Ã, //Wrong order so reset moved Boxes
Ã,  Ã,  Ã, SequenceIndex=0;
Ã,  Ã,  Ã, int b=0;
Ã,  Ã,  Ã, while (b<BlockNumber)
Ã,  Ã,  Ã, {
Ã,  Ã,  Ã,  Ã,  Ã, if (BlockState[b]==1)
Ã,  Ã,  Ã,  Ã,  Ã, {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã, BlockState[b]=0;
Ã,  Ã,  Ã,  Ã,  Ã,  Ã, //Animate Block moving back
Ã,  Ã,  Ã,  Ã,  Ã,  Ã, object[Block+1].SetView(4);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã, object[Block+1].Animate(Block+1,3,eOnce,eBlock,eBackwards);
Ã,  Ã,  Ã,  Ã,  Ã, }
Ã,  Ã,  Ã,  Ã,  Ã, b++;
Ã,  Ã,  Ã, }
Ã,  }
}


Put this in first time player enters room script
Code: ags

Ã,  //Your desired sequence
Ã,  Sequence[0]=3;
Ã,  Sequence[1]=2;
Ã,  Sequence[2]=0;
Ã,  Sequence[3]=1;

Ã,  //All Boxes at start position
Ã,  BlockState[0]=0;
Ã,  BlockState[1]=0;Ã,  
Ã,  BlockState[2]=0;
Ã,  BlockState[3]=0;

Ã,  SequenceIndex=0;


Then in interact hotspot 1 script put
Code: ags

Ã,  PushBlock(1);


Then in interact hotspot 2 script put
Code: ags

Ã,  PushBlock(2);


and so on for all blocks. Hope this is a little more clear.



Maverick

Thanks Wretched!
I tried the code as is and only added the door open script. The room saves which tells me that there should not be any capture errors but when I run a test absolutely nothing happens (blocks do not move, door does not move). Its like putting a character in an empty room . I don't get it

The views are all set up in view 4
Block1 is on loop 1
Block 2 on loop 2 etc
Door is on loop 0

Door script
object[0].SetView(4);
object[0].Animate(0,0,eOnce.NoBlock,eForwards);


Any ideas on what I missed
???

Wretched

If nothing is happening I'd start by putting some Display("X"); commands in the code so you can see if that bit of code is being run or not. Starting with the hotspot interactions.

I tried the code and there was a wee bug in the while loop, you need to change the three 'Block+1' bits to 'b+1'.

Here's my test project Block demo

Only difference is I used interact object instead of interact hotspot, don't know how you want to trigger the blocks.

Maverick

Great!

I have no idea why it does not work with hotspots but I changed it to interact with object and now its working although not exactly the way I wanted. The problem is that this type of puzzle can be solved very easily from memory in terms of how the blocks behave. To make this effective I'll have to add a lot more blocks. The idea is rather to have the blocks reset (if wrong) only once all the blocks have been moved to state 1. That way the player cannot pick up the sequence from just looking at the behaviour of the blocks but should rely on some clue elsewhere in the game (or try all the combinations).

Is it possible to set it up as explained?

I am very grateful for the time that went into this. This is also a great example of how a sequencial memory puzzle can be set up and should be added to the tutorials with explanations as to why variables etc were set up this way.Ã, 

You've been a great help.
:)

Wretched

Maybe the objects were covering the hotspots? then you would need to do
object[1].Clickable=false; for each block in first time player enter screen bit.
Anyway here is the altered version,
block 2

I've added a ResetBlock() function that you could call if you want to give the player a way to start again.

I tried to make the original match the behaviour of your example code. As I'm not working in AGS at present it's good to keep my hand in now and again.

Maverick

#14
Excellent! It is runs like clockwork now and the hotspots were over the objects so next time I'll remember that!!!
Apart from that it also opened up other alternatives in terms of adding more scope to how I can set up the final puzzle in my game.


Only problem is that I do not understand all the code you used but this is not the time nor the place to get into that. If you have the time drop me a PM to discuss this.

SMF spam blocked by CleanTalk