PDA

View Full Version : while loop break?


wassup99
03-15-2008, 10:27 AM
Hi,

i'm a 2nd year computer science student with some knowledge in programming and have been tasked by a module to do something using Alice

i've worked on what i have for quite a few days now, basically i play a movie, then i move into a over-the-shoulder view of a game where i can move around with the arrow keys and what not, and the motive in this section is to complete quest which allows me to go to the next scene

now my knowledge of alice is very basic and sadly there aren't many examples i could follow from, so basically what i have done for my fps section is this -

world.main calls world.scene1start
scene1start consists of a
While(true)
Do Together
{
alot of while true loops, each one with a if statement indicating a situation that might happen during the game
e.g. i have a "i am walking" flag that will move me forward if the flag is set, and i have a "i am hit" flag that calls a damage() method that decrements my hp by 1, i have several "clicked" flags that calls the respective npcs talk() method, and within here i have a loop(infinity) that plays a background music etc. etc.
}

technically when the game progressed such that i want to transition to scene 2, i was hoping i can 'forcefully' end scene1start so that it'll return to the main method, and continue from there running scene2start, however i have found no way of doing this - if Alice had a break statement i could simply insert a
While(true)
If (scene1running)
Do Together
(All the while trues)
Else
break

all i need to do is set the scene1running flag to false for scene1running to break from the while true loop and return - alternatively if there was a return statement i could do the same thing but apparently this two functions are missing

i can't call scene2start directly within scene1start (or rather, i can but i don't want to) because there's just too many while true loops from scene1start and scene2start and doing this will cause them to overlap and i don't want that to happen

any ideas how to forcibly end my while(true) loop to return to main, or is my programming logic for alice off?

DickBaldwin
03-15-2008, 11:34 AM
Hi,

i'm a 2nd year computer science student with some knowledge in programming and have been tasked by a module to do something using Alice

i've worked on what i have for quite a few days now, basically i play a movie, then i move into a over-the-shoulder view of a game where i can move around with the arrow keys and what not, and the motive in this section is to complete quest which allows me to go to the next scene

now my knowledge of alice is very basic and sadly there aren't many examples i could follow from, so basically what i have done for my fps section is this -

world.main calls world.scene1start
scene1start consists of a
While(true)
Do Together
{
alot of while true loops, each one with a if statement indicating a situation that might happen during the game
e.g. i have a "i am walking" flag that will move me forward if the flag is set, and i have a "i am hit" flag that calls a damage() method that decrements my hp by 1, i have several "clicked" flags that calls the respective npcs talk() method, and within here i have a loop(infinity) that plays a background music etc. etc.
}

technically when the game progressed such that i want to transition to scene 2, i was hoping i can 'forcefully' end scene1start so that it'll return to the main method, and continue from there running scene2start, however i have found no way of doing this - if Alice had a break statement i could simply insert a
While(true)
If (scene1running)
Do Together
(All the while trues)
Else
break

all i need to do is set the scene1running flag to false for scene1running to break from the while true loop and return - alternatively if there was a return statement i could do the same thing but apparently this two functions are missing

i can't call scene2start directly within scene1start (or rather, i can but i don't want to) because there's just too many while true loops from scene1start and scene2start and doing this will cause them to overlap and i don't want that to happen

any ideas how to forcibly end my while(true) loop to return to main, or is my programming logic for alice off?

I don't have the time to dig into the details of exactly what you are doing. At the surface, however, it seems like simply running a while(true) loop is not very good logic. You might try creating a boolean variable and loop while that variable is true and stop looping when the value of the variable goes to false. Then all you have to do to terminate the loop is to set the value of the variable to false as a result of some condition.

Unlike Java, I don't believe that there is a break statement in Alice 2.0.

Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm

wassup99
03-15-2008, 12:16 PM
i realize while(true) is not good programming logic, however for example i try to do a scene where i can attack a troll, and if within proximity i damage the troll

under world events i have
While X is pressed,
knight.attack()

knight.attack() contains a RNG to choice, and if choice < 0.5 call swing1(), else call swing2() - within each swings() i set poses and timings and call a method troll.damage() which checks the proximity of my sword to the troll and determines whether or not damage is done - i have done this in several instances, not just for troll.damage, but the event handler throws me an error which i believe is cause of a world event calling too many submethods (or something similar)

from then on what i did was instead of calling troll.damage() directly, i set a "troll is hit" flag to true, and my scene method which is currently running, checks through busy waiting, all these flags constantly - whenever it sees the flag is on, it executes troll.damage() from the world method, not from my nested swing1() method, and this doesn't cause errors

why i believe i had to use while(true) loops to implement an active scene was because if i had simply done a while(condition) loop, for e.g. i run a method that sets the scene and had a

While(i am alive)
//perform actions

being the procedural language that it is, the loop will only be parsed once - once i am alive goes from true to false once, the loop is never returned to again, which is useful for some things, but not useful for events that can happen again and again at random times

if for e.g. i did

While(true)
{
While(i am alive)
//perform actions
}

no matter how many times i am alive is set and cleared and set and cleared, the actions are still performed (or not, respectively) - the only problem here is of course, escaping the while(true) loop altogether, which is the problem i have :(

wassup99
03-15-2008, 08:21 PM
you were right about the while(condition) ... it seems i misunderstood the workings of do together, i have changed all my while(true) loops to while(scene 1 running) and it works now, however i have another problem - is it possible to prematurely end a music file from playing, when a certain event happens?

DickBaldwin
03-15-2008, 08:49 PM
you were right about the while(condition) ... it seems i misunderstood the workings of do together, i have changed all my while(true) loops to while(scene 1 running) and it works now, however i have another problem - is it possible to prematurely end a music file from playing, when a certain event happens?

Although I'm not certain, I seem to remember seeing a recent posting, possibly by DrJim stating that it is not possible to stop a music file once it starts. It seems to me that the posting contained a workaround but I don't remember what it was. Then again, I may have dreamed that there was such a posting.

You might try doing a Google search for the following keywords and see what that turns up.

alice drjim music site:www.alice.org

Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm

wassup99
03-16-2008, 11:23 AM
solved the problem, thanks baldwin :)

DrJim
03-18-2008, 12:09 PM
If you didn't solve the audio problem, the solution was to just turn the volume to zero at the time you want. You can't actually stop a track, as far as I know.