Author Topic: Basic basic intro help please with 'Struct'  (Read 250 times)  Share 


Khris

  • Evil Dark Emperor Death-Kill
    • Lifetime Achievement Award Winner
    •  
    • I can help with play testing
    •  
    • I can help with scripting
    •  
    • I can help with translating
    •  
Re: Basic basic intro help please with 'Struct'
« Reply #1 on: 18 May 2012, 17:24 »
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: Adventure Game Studio
  1. // header (GlobalScript.ash)
  2.  
  3. struct Question {
  4.   String q; //The question
  5.   String ans[4]; //The answeres, from ans[0] to ans[3]. You could make 4 seperate variables, I preffer arrays
  6.   int RightAns; //Number of the correct answere;
  7.   int category; //Say 1 = Nature, 2 = Poltics etc.
  8. };
  9.  
  10. // GlobalScript.asc, at the very top
  11. Question question[50];
  12.  
  13. // this inside game_start:
  14.  
  15.   question[0].q = "Cé hé seo?";
  16.   question[0].ans[0] = "Ronnie Drew";
  17.   question[0].ans[1] = "...";
  18.   question[0].ans[2] = "...";
  19.   question[0].ans[3] = "...";
  20.   question[0].RightAns = 0;
  21.   question[0].category = 1;

Note that you can setup an enum for the categories:
Code: Adventure Game Studio
  1. // this in the header, too
  2.  
  3. enum Category {
  4.   eCatNature = 1,
  5.   eCatPolitics = 2
  6. };

Now you can write
Code: Adventure Game Studio
  1.   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.
http://whathaveyoutried.com/

The other day on yahoo answers:
"Can you print colored images with black ink? If so tell me how please Thanx Kimberly"

Kweepa

  • Mutated Guano Deviser
    • Best Innovation Award Winner 2009, for his modules and plugins
    •  
  • Kweepa worked on a game that was nominated for an AGS Award!
Re: Basic basic intro help please with 'Struct'
« Reply #2 on: 18 May 2012, 17:27 »
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: Adventure Game Studio
  1. struct Question // 1. declared first
  2. {
  3.   String q;
  4.   String ans[4];
  5.   int rightAns;
  6.   int category;
  7. };
  8.  
  9. Question questions[50]; // 3. renamed this to avoid clash with the variable below
  10. Question question;
  11.  
  12. function game_start() // 4. put initialization into game_start function
  13. {
  14.   question.q = "Who is this?";
  15.   question.ans[0] = "Van Morrison";
  16.   question.ans[1] = "Shane MacGowan";
  17.   question.ans[2] = "Feargal Sharkey";
  18.   question.ans[3] = "Ronnie Drew"; // 2. last array index is 4-1
  19.   question.rightAns = 3;
  20.   question.category = 1;
  21. }
  22.  

Ah, Khris :)
Still waiting for Purity of the Surf II

Re: Basic basic intro help please with 'Struct'
« Reply #3 on: 18 May 2012, 18:41 »




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]

  question[0].category = eCatPolitics;
 
[/code]

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 "





monkey_05_06

  • AGS Project Admins
  • #1 Straight Basher
Re: Basic basic intro help please with 'Struct'
« Reply #4 on: 18 May 2012, 19:27 »
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: Adventure Game Studio
  1. // GlobalScript.ash
  2.  
  3. // beneath the definition of the Question struct
  4. import Question questions[50];
  5. import Question question;
  6.  
  7. // GlobalScript.asc
  8.  
  9. // beneath the definition of the instances (outside of any functions)
  10. export questions;
  11. export question;
« Last Edit: 18 May 2012, 19:29 by monkey_05_06 »
By and large I didn't accomplish what I set out to do, but I did accomplish a fair bit. So, there is that.