Wintermute Engine Forum

Wintermute Engine => Technical forum => Topic started by: The Man on April 26, 2008, 04:58:54 PM

Title: The right sequence will open the door...
Post by: The Man on April 26, 2008, 04:58:54 PM
I'll try to explain myself the best I can...
I have this first person scene in which you can see a bookcase.In this bookcase there are four interactive books,that must be pressed in a certain sequence in order to open a door.If a book is pressed,it plays an animation.After You pressed all four books,the program should check if they have been pressed in the right sequence,and if not,the player must be able to start again from the beginning.
What's the best way to do this?I think I have to use arrays in some way,but I dont' know exactly how to...
Title: Re: The right sequence will open the door...
Post by: Mnemonic on April 28, 2008, 01:27:12 PM
Here's how I would do that... Use a string variable. Name the books like "1", "2", "3" etc. Everytime you click the book, add its name to the string variable and check if the length of the string is equal to the number of books. If it is, check if the string equals to the correct book combination. If it is, you're all set. If not, empty the string variable and start over.
Title: Re: The right sequence will open the door...
Post by: The Man on April 28, 2008, 05:00:00 PM
I understand,but what's the command to check the lenght of a string?
Title: Re: The right sequence will open the door...
Post by: Net on April 28, 2008, 07:19:02 PM
Code: [Select]
var test;
test = "Some text";

Game.Msg(test.Length); //this produce number 9 (strings SOME TEXT length)
Title: Re: The right sequence will open the door...
Post by: The Man on April 29, 2008, 05:46:59 PM
So I have to write something like:

if(a.length=4 && a=1342)
{
/do something/
}

Where "a" is my string variable,"4" is the number of books and "1342" is the right combination.
Is it correct?If not,how do I write this?
Title: Re: The right sequence will open the door...
Post by: Net on April 29, 2008, 07:11:39 PM
Code: [Select]
if(a.length=4 && a=1342) <-- there is a mistake
{
/do something/
}

if you use = you will set the variable
to check you must use ==

so your code must look like

Code: [Select]
if(a.length==4 && a==1342)
{
/do something/
}

and ofcourse you must add the numbers to your string variable this way

Code: [Select]
a = a + "NUMBER";

else you will sum the numbers and then your code

Code: [Select]
/do something/
will never be done
Title: Re: The right sequence will open the door...
Post by: The Man on April 29, 2008, 08:24:42 PM
Argh...I don't know,it doesn't work...Can you please check if there are mistakes?
I wrote this code into scene_init.script:

Code: [Select]
global a;
a = new String()256;
a="";

if(a=="1212")
{
Game.Msg("You did it!");
}

Then in the first book script I wrote:

Code: [Select]
global a;
on "LeftClick"
{
 a=a + "1";
}

And finally in the second book script:

Code: [Select]
global a;
on "LeftClick"
{
 a=a + "2";
}

So when I click the books in the sequence "first,second,first,second" The game should write "You did it!",but nothing happens instead...
Title: Re: The right sequence will open the door...
Post by: Net on April 29, 2008, 08:44:02 PM
Code: [Select]
a = new String()256; <-- here

the right way is

Code: [Select]
a = new String(256);
Title: Re: The right sequence will open the door...
Post by: The Man on April 29, 2008, 08:50:16 PM
Sorry,I wrote it in the wrong way here in the forum...in my script I wrote it in the right way,but still doesn't work...
I'm becoming crazy looking at these scripts...
Title: Re: The right sequence will open the door...
Post by: Net on April 29, 2008, 09:43:39 PM
Nevermind.
Is there any error?
Title: Re: The right sequence will open the door...
Post by: rkitting on April 30, 2008, 08:21:30 AM
What errors are you getting in your wme.log file?
Title: Re: The right sequence will open the door...
Post by: The Man on April 30, 2008, 08:54:52 AM
This is another strange thing...I'm not getting any error...it looks like everything works fine,but the code isn't executed...
How would you program this kind of code?Am I the only one who has got this kind of problem?
Title: Re: The right sequence will open the door...
Post by: Net on April 30, 2008, 09:35:45 AM
scene_init script is executed ONLY when you enter the scene and then it ends.

Try in scene_init script write this:

Code: [Select]
while(true)
{
  if(a=="1212")
  {
    Game.Msg("You did it!");
  }
  Sleep(200);
}
Title: Re: The right sequence will open the door...
Post by: The Man on April 30, 2008, 10:16:21 AM
Ok,that worked,but that was a try....Now I tried to adapt the code to my needs and wrote this:

Code: [Select]
global a;
a = new String(256);
a="";


while(true)
{
 if(a.Length==4 && a=="1212")
    {
  Game.Msg("You did it!");
    }

Sleep(200);
}

while(true)
{
  if(a.Length==4 && a!="1212")
    {
Game.Msg("You failed!");
    }
Sleep(200);
}

I thought this would have worked,instead the right combination works and the game writes "You did it!" but the wrong combinations doesn't do anything...I dont' know,it looks like the program doesn't read "a.Length"...maybe I'm using it in the wrong way... Also,I should add some command in the second "while" branch,that reset my "a" variable,but I dont' know exactly How's this command like...
Title: Re: The right sequence will open the door...
Post by: Net on April 30, 2008, 10:53:26 AM
You don't have to double use while(true).

This should work as you expect

Code: [Select]
while(true)
{
  if (a.Length == 4)
  {
    if (a == "1212")
    {
      Game.Msg("You did it!");
    }
    else
    {
      Game.Msg("You failed!");
    }
  }
  Sleep(200);
}

I rewrote your code, because if you use

Code: [Select]
while(true)
{
  if(a.Length==4 && a=="1212")
  {
    Game.Msg("You did it!");
  }
  else
  {
    Game.Msg("You failed!");
  }
  Sleep(200);
}

the game will write You failed! every time the loop is executed. The first (I think working code) will check, if variable a has length 4 and then check its value and write You did it! or You failed!. If variable a doesn't have expected length, the inner code will not be executed.

I hope this will help and I'm sorry for my English.
Title: Re: The right sequence will open the door...
Post by: sychron on April 30, 2008, 12:57:12 PM
I think both code snippets are identic, for WME processes Logical Operations from left to right. If a.length<4 fails, the statement fails, otherwise the second part is checked.
Title: Re: The right sequence will open the door...
Post by: The Man on April 30, 2008, 01:24:00 PM
It finally works...thanks everybody...Net,if it wasn't for you,I would be stucked...
What can I say?
Wintermute community Rocks!
Title: Re: The right sequence will open the door...
Post by: Net on April 30, 2008, 06:42:49 PM
I think both code snippets are identic, for WME processes Logical Operations from left to right. If a.length<4 fails, the statement fails, otherwise the second part is checked.

Ok, but The Man wanted to write You did it! or You failed! JUST when the a variable has length equal to 4. Thats what I wrote, the game will write You failed! until the a has length 4 and the right content.