Wintermute Engine Forum
Wintermute Engine => Technical forum => Topic started 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...
-
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.
-
I understand,but what's the command to check the lenght of a string?
-
var test;
test = "Some text";
Game.Msg(test.Length); //this produce number 9 (strings SOME TEXT length)
-
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?
-
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
if(a.length==4 && a==1342)
{
/do something/
}
and ofcourse you must add the numbers to your string variable this way
a = a + "NUMBER";
else you will sum the numbers and then your code
/do something/
will never be done
-
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:
global a;
a = new String()256;
a="";
if(a=="1212")
{
Game.Msg("You did it!");
}
Then in the first book script I wrote:
global a;
on "LeftClick"
{
a=a + "1";
}
And finally in the second book script:
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...
-
a = new String()256; <-- here
the right way is
a = new String(256);
-
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...
-
Nevermind.
Is there any error?
-
What errors are you getting in your wme.log file?
-
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?
-
scene_init script is executed ONLY when you enter the scene and then it ends.
Try in scene_init script write this:
while(true)
{
if(a=="1212")
{
Game.Msg("You did it!");
}
Sleep(200);
}
-
Ok,that worked,but that was a try....Now I tried to adapt the code to my needs and wrote this:
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...
-
You don't have to double use while(true).
This should work as you expect
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
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.
-
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.
-
It finally works...thanks everybody...Net,if it wasn't for you,I would be stucked...
What can I say?
Wintermute community Rocks!
-
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.