Basic basic intro help please with 'Struct'

Started by madradubhcroga, Fri 18/05/2012 16:47:31

Previous topic - Next topic

madradubhcroga

Hi,

I need some InStructions!

I'm trying to make my first Struct -  to make a database of questions.

in Globalscript.asc I have

Code: ags


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


Khris

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:
Code: ags
// 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:
Code: ags
// this in the header, too

enum Category {
  eCatNature = 1,
  eCatPolitics = 2
};


Now you can write
Code: ags
  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.

Kweepa

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).

Code: AGS

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 :)
Still waiting for Purity of the Surf II

madradubhcroga





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

Code: ags


  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 "





monkey0506

#4
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).

Code: ags
// 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;

SMF spam blocked by CleanTalk