2019-02-10 05:16:24

Hello:
So recently, after deciding to reread the Bgt manual to familiarize myself with its basic concepts again, I have decided to start on a program. However, I'm running into a small but extremely annoying error. I keep getting an unexpected token error on the 25th line of this program, which is after I declair Void play_game();

#include "menue.bgt";
#include "speak.bgt"
#include "rotation.bgt"
void main()
{
show_game_window("Game_alpha");
speak("Hello");

string text="Welcome To this game. Please choose an option from the below menu";
string[] items={"Play game","Explore the grid","exit"};
r=new_menu(text, items, 0);
if(r==1) {
play_game();
}
else if(r==2) {
explore_grid();
}
else if(r==3) {
exit();
}
}
void play_game();
{
{
int answer = question("Question", "would you like to read the game  info and rules??");
if(answer==0)
{
alert("Let's try that again.");
}
if(answer==1)
{
alert("Loading...");
}
if(answer==2)
{
alert("All right then. Continuing to game...");
}}

Would someone mind telling me what I'm messing up here, please?

-
That Guy. Serving those people since that time. To contact, use that info.

2019-02-10 05:33:33

Hi,
Delete the ; in play_game(); that's not needed when you declare new functions. And remove that extra { after play_game.
Regards,
Amit

There once was a moviestar icon.
Who prefered to sleep with the light on.
They learnt how to code, devices sure glowed,
and lit the night using python.

2019-02-11 03:00:46

Sorry, I'm still getting the error. I removed the ; as you specified and furthermore removed the extra {, but I'm still getting the same error on the same line.

-
That Guy. Serving those people since that time. To contact, use that info.

2019-02-11 06:40:20

So, this is caused when braces do not match, or braces are not where they should be.  Keep in mind that BGT needs no brace if there is only one action to be performed based on a condition check.  Like so:
if(A > 0)
A *= B;
However, braces are needed if there is more than one action to be performed after a given check, like so:
if(A > 0)
{
A *= B;
B += C;
A = C;
}
In this case, you had braces in places you didn't need them, and so the compiler wasn't pleased.  I removed the unnecessary braces.  I also corrected another error that I saw.  Keep in mind that the alert function takes two parameters, so if you want to just give a message without a dialog name, use quotes with a comma, then the string of test you want to actually say, like so:
alert("", "This is a test.");
However, the compiler is not pleased with your line where you declare menu items in the string array.  It wants an identifier there instead of the string constant it sees.  I prefer to use the dynamic menu, and that is done differently.  The code with the braces and alert calls fixed is as follows:

void main()
{
show_game_window("Game_alpha");
speak("Hello");
string text="Welcome To this game. Please choose an option from the below menu";
string[] items="Play game","Explore the grid","exit";
r=new_menu(text, items, 0);
if(r==0)
play_game();
else if(r==1)
explore_grid();
else if(r==2)
exit();
}

void play_game()
{
int answer = question("Question", "would you like to read the game  info and rules??");
if(answer==0)
alert("Let's try that again.");
if(answer==1)
alert("", "Loading...");
if(answer==2)
alert("", "All right then. Continuing to game...");
}

void explore_grid()
{
//Code for this function goes here.
}

2019-02-13 21:52:57

Thanks, I appreciate the help. But, when I entered the code as you wrote it, I now get an unexpected token error on line 5

-
That Guy. Serving those people since that time. To contact, use that info.

2019-02-14 00:18:45

The only error that I see is the one about string constant when I try to compile:
On line: 6 (28)
Line: string[] items="Play game","Explore the grid","exit";
Error: Expected identifier
On line: 6 (28)
Line: string[] items="Play game","Explore the grid","exit";
Error: Instead found '<string constant>'

Which means you might have accidentally removed a brace somewhere or something, and that's why you're getting the token error.  The code that gives the string error is as follows:
void main()
{
show_game_window("Game_alpha");
speak("Hello");
string text="Welcome To this game. Please choose an option from the below menu";
string[] items="Play game","Explore the grid","exit";
r=new_menu(text, items, 0);
if(r==0)
play_game();
else if(r==1)
explore_grid();
else if(r==2)
exit();
}

void play_game()
{
int answer = question("Question", "would you like to read the game  info and rules??");
if(answer==0)
alert("", "Let's try that again.");
if(answer==1)
alert("", "Loading...");
if(answer==2)
alert("", "All right then. Continuing to game...");
}

void explore_grid()
{
//Code for this function goes here.
}

2019-02-14 02:18:44

Ahh. I may have forgotten to include the #include code in my original post, but that string is  for a third-party menu I'm using, because I don't like dynamic menu. When I use the old matcher script to check if i have any unclosed or overclosed braces, it comes up fine.

-
That Guy. Serving those people since that time. To contact, use that info.

2019-02-14 02:37:50

I can't help you with the menu issue, because I have never used it and don't have a copy of it.

2019-02-14 15:27:28

Well, I don't know why, but when I wrote out the whole file again, I didn't get any errors. Until I tried to do that question answer type thing.
void question()
{
int answer;
answer = question("Would you like to view the game instructions and rules?");
if(answer == 1)
{
bool open("readme.txt", "rb");
}
else if(answer == 2)
{
game();
}
else
{
alert("Please try again");
}
}

-
That Guy. Serving those people since that time. To contact, use that info.

2019-02-15 05:17:52

A couple issues

1. Your not using question correctly. For starters, the return type is double, not int. also, the signature is double question(string title, string text).
Meaning you'd want something like
double answer = question("rules and instructions", "would you like to view the game rules and instructions?");

2. I can only guess at the meaning of this line
bool open("readme.txt", "rb");
Looks like your trying to get the contents of readme.txt.
It's usually good practice to see if it exists first

if(!file_exists("readme.txt"))
{
alert("uhh, uhh...", "Hey there, stop messing with my files!");
exit();
}
//then on to actually reading
file f;
f.open("readme.txt", "rb");
string content=f.read();
f.close();
HTH

2019-02-15 18:12:34

I've developed a habit of writing both the opening brace and then putting the closing brace after that, then filling the space in between with code.
Since then, the number of these annoying errors has drastically reduced.

2019-02-15 22:18:58 (edited by JLove 2019-02-15 22:19:50)

Where it gets tricky is when you've got nested statements.  Get in the habit of counting braces, and make sure that you end up with 0 open when your function is completed.  You might encounter bugs in behavior if they're in the wrong places, but you will compile, and those bugs are easier to track down than the elusive and all-vague "unexpected end of file" message that the compiler gives.

2019-02-16 01:00:28

I've gotta agree with you on that one. The compiler sometimes can be pretty annoying when it comes to telling you where you messed up. But @10, I'll certainly give that all a try pretty soon here, and thank all of you a lot for all the help you've given me. I sure do appreciate it.

-
That Guy. Serving those people since that time. To contact, use that info.