Need Help ( Arrays )

Game Editor comments and discussion.

Need Help ( Arrays )

Postby trippola77 » Sun Jun 17, 2012 10:35 pm

Alright, I'm still kind of new to programming so I thought I'd try to do something simple. I'm thinking about making some sort of video game quiz and created some code to see how it would work. The problem is I have been trying to find a way to tell the player how many questions he has correctly answered out of 100 ( the code randomly pulls a question and if the player has already answered the question, I don't want it to add to the correctly answered score ) . I tried using arrays ( the variable is " questionC" and has 8 elements ( questionC[7]) to determine whether or not the player has answered the questions. I also used the Global code editor to give all the original values of questionC a value of 0 .
If any of you guys could look at my code ( I'll upload) and give me a few pointers, it'd be greatly appreciated ( the game is nothing special, just a display of what I want to accomplish with the code if I could get it to work)
Thanks!
-Tripp

The File is RANDOM_Q_GEN.GED
Attachments
TTT.zip
(946.03 KiB) Downloaded 37 times
trippola77
 
Posts: 16
Joined: Sat Feb 09, 2008 12:05 am
Score: 0 Give a positive score

Re: Need Help ( Arrays )

Postby skydereign » Sun Jun 17, 2012 11:12 pm

First thing to note, your script in global code looks something like this.
Code: Select all
int questionC[8];
questionC[0]=0;
questionC[1]=0;
// and so on

Code isn't executed in global code at startup. The only one that is processed is the declaration line (questionC[8]). So, if you want to initialize an array, you can use this format.
Code: Select all
int questionC[8] = {0, 0, 0, 0, 0, 0, 0, 0};

But, since you are initializing them to 0, all you need to write is this.
Code: Select all
int questionC[8] = {0};

This is because when initializing an array, it takes the values you pass it, and if you don't provide all the values (putting only one value instead of all 8) it initializes the rest of the array to 0.

Now from your code, you never actually set the array values you are checking to a non-zero value.
Code: Select all
if(questionC[0]==0)
{
    Q_count++;
}

Every time that script runs, Q_count will increase. That is because you never change quesetionC[0], so naturally it will keep being true. If you want it not to run again, you have to change questionC's value within the if statement.

Do you know about clones? You are using a lot of repetitive events, which could be cleaned up quite nicely with clones.
User avatar
skydereign
 
Posts: 3465
Joined: Mon Jul 28, 2008 8:29 am
Score: 571 Give a positive score

Re: Need Help ( Arrays )

Postby SuperSonic » Mon Jun 18, 2012 12:24 am

Beat me to it again Sky :)
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1444
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Need Help ( Arrays )

Postby trippola77 » Mon Jun 18, 2012 1:40 am

So if the player already answered the question would I include this in the mouse button down action?

Code: Select all
loadVars("questionC", "Question");



 if (questionC[0] == 0 )
{
    Q_count ++;
    questionC[0] ++;
}

else if (questionC[0] == 1)
{
    // Don't really know what to put here, but I concluded that maybe i would use else if to test if questionC[0] == 1
 

saveVars("Q_count", "Question");









Thanks!
trippola77
 
Posts: 16
Joined: Sat Feb 09, 2008 12:05 am
Score: 0 Give a positive score

Re: Need Help ( Arrays )

Postby skydereign » Mon Jun 18, 2012 1:42 am

Well, the int array questionC is being used to tell if the player has answered the question before.
Code: Select all
if(questionC[0]==0)
{
    // this code runs if the question hasn't been answered
    questionC[0]=1; // and now the question has been answered
}
else
{
    // this code runs if the question has already been answered
    // if you don't want any code here to trigger, then you can remove this else block
}
User avatar
skydereign
 
Posts: 3465
Joined: Mon Jul 28, 2008 8:29 am
Score: 571 Give a positive score

Re: Need Help ( Arrays )

Postby trippola77 » Mon Jun 18, 2012 2:06 am

Alright, that code works great, the only trouble I'm having now is once I click it, Q_count will go up like its supposed to , but if I exit out of the game and the reload it and end up with the same question, it will add another to Q_count even though its already been answered . . . Maybe I'm using the save function wrong?
( i attached an updated file )
Once again thanks for the help! means a lot!
Attachments
TTT.zip
(939.98 KiB) Downloaded 18 times
trippola77
 
Posts: 16
Joined: Sat Feb 09, 2008 12:05 am
Score: 0 Give a positive score

Re: Need Help ( Arrays )

Postby skydereign » Mon Jun 18, 2012 2:10 am

Currently you are only saving how many points the player has (Q_count). But you also need to save which questions the player has already answered.

Also you can't save the questionC array currently. You need to declare the questionC array with gE's gui variable creator (by clicking Variables when in script editor) instead of declaring it in global code. You have a different array already declared that way named array1 or similar. That variable can be saved by calling saveVars on the save group. But since questionC is not in a save group you can't actually save it.
User avatar
skydereign
 
Posts: 3465
Joined: Mon Jul 28, 2008 8:29 am
Score: 571 Give a positive score

Re: Need Help ( Arrays )

Postby trippola77 » Mon Jun 18, 2012 2:33 am

I got it working, Thanks a lot man! I gave you one of those plus things ( if thats important? ). Being new I'll probably be asking a lot of questions about this game I'm making so be on the lookout !

-Tripp
trippola77
 
Posts: 16
Joined: Sat Feb 09, 2008 12:05 am
Score: 0 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest

cron