Hi,
I need some InStructions!
I'm trying to make my first Struct - to make a database of questions.
in Globalscript.asc I have
Question question[50];
struct Question {
String q; //The question
String ans[4]; //The answeres, from ans[0] to ans[3]. You could make 4 seperate variables, I preffer arrays
int RightAns; //Number of the correct answere;
int catagory; //Say 1 = Nature, 2 = Poltics etc.
};
Question question;
question. = ("Cé hé seo?");
question.ans[4] = ("Ronnie Drew");
question.RightAns = ("Ronnie Drew");
question.catagory= 1;
The error I get reads
GlobalScript.asc(61): Error (line 61): Parse error: unexpected 'Question'
line 61 is the first line of code above.
I don't really get the concept of struct yet- and probably won't until I've done a few.
???
Pointers appreciated.
AnMDC
First of all, the forum search should provide several threads that explain in detail how to do this. Please only open a thread after you have exhausted available resources.
Now, since this thread is here:
// header (GlobalScript.ash)
struct Question {
String q; //The question
String ans[4]; //The answeres, from ans[0] to ans[3]. You could make 4 seperate variables, I preffer arrays
int RightAns; //Number of the correct answere;
int category; //Say 1 = Nature, 2 = Poltics etc.
};
// GlobalScript.asc, at the very top
Question question[50];
// this inside game_start:
question[0].q = "Cé hé seo?";
question[0].ans[0] = "Ronnie Drew";
question[0].ans[1] = "...";
question[0].ans[2] = "...";
question[0].ans[3] = "...";
question[0].RightAns = 0;
question[0].category = 1;
Note that you can setup an enum for the categories:
// this in the header, too
enum Category {
eCatNature = 1,
eCatPolitics = 2
};
Now you can write
question[0].category = eCatPolitics;
You don't need to put ()s around Strings.
.RightAns is an int and supposed to hold the index of .ans[] that stores the correct answer, so needs a value from 0-3.
A few things:
1. You need to declare structs before using them.
2. Array indices go from 0...N-1
3. You can't have a variable called question and an array called question[50].
4. Initialization of structs (i.e., filling out the variable contents) has to go in a function (eg game_start).
struct Question // 1. declared first
{
String q;
String ans[4];
int rightAns;
int category;
};
Question questions[50]; // 3. renamed this to avoid clash with the variable below
Question question;
function game_start() // 4. put initialization into game_start function
{
question.q = "Who is this?";
question.ans[0] = "Van Morrison";
question.ans[1] = "Shane MacGowan";
question.ans[2] = "Feargal Sharkey";
question.ans[3] = "Ronnie Drew"; // 2. last array index is 4-1
question.rightAns = 3;
question.category = 1;
}
Ah, Khris :)
Thankyou both.
I belive you Khris, and I don't mean to waste anybody's time. The first 6 or 7 threads I searched seemed too advanced for me, (or delt with specific issues) and the manual entry is so terse that I don't know how to apply the information.
I think I have applied your codes correctly, because the game runs.
But I don't know how to call it.
I have an object in room 1 that on click opens a gGUI holding a picture of Mr. Drew, and space for the questions and answers.
What is the correct way to proceed?
ps
I tried putting
question[0].category = eCatPolitics;
in room1.asc
under
function oQ1_AnyClick()
but got
" Failed to save room room1.crm; details below
room1.asc(10): Error (line 10): Undefined token 'question' "
when I capitalized the Q in question, the message changed to:
" Failed to save room room1.crm; details below
room1.asc(10): Error (line 10): fixed size array cannot be used in this way "
If you're putting that code in the room script then you're going to need to import and export the instances of your struct from the Global Script (which is also just a search away).
// GlobalScript.ash
// beneath the definition of the Question struct
import Question questions[50];
import Question question;
// GlobalScript.asc
// beneath the definition of the instances (outside of any functions)
export questions;
export question;