I probably don't have time to code this myself, but here's how I would go about setting up the code part:
Make a custom int which will return a random number.
Make more custom ints which will return the math answer.
Make custom tokens to hold the random number ints and the answer int.
So, you'd have something like:
//min is the smallest random number returned. max is the largest random number returned.
int MyRandom(int min, int max)
{
//random() returns a random number starting at 0. Thus, random(10) will return a number
//between 0 and 9. We add 1 to the mix to give accurate results.
int nResult = random(max-min+1)+min;
return nResult;
}
//Add 2 numbers
int nAdd(int n1, int n2)
{
int nResult = n1 + n2;
return nResult;
}
//Subtract 2 numbers
int nSubtract(int n1, int n2)
{
int nResult = n1 - n2;
return nResult;
}
Then, do something like this in the code:
//The first random number to be used in the conversation
int nFirst = MyRandom(1, 10);
//The second random number to be used in the conversation
int nSecond = MyRandom(1, 10);
//The correct answer, composed of the two random numbers, fed in to one of the custom math ints.
int nAnswer = nAdd(nFirst, nSecond);
Then, declare them each as tokens. You can call the tokens directly in the conversation.
You probably also want to declare a few more random choices to use as the other options with something like:
//Create a random number answer for each of the other conversation options
//using the same technique as above.
int nWrong1 = MyRandom(1, 10);
//We'll use this as a simple counter to make sure that the loop ends in a situation where it may get stuck.
//Since we are not giving it a value, the engine will assume it has a value of 0.
int i;
//If the wrong answer that we randomly generated happens to match the correct answer, we need to change it.
// i<10 means that we'll have 10 tries to find an answer that doesn't match the correct answer.
//The only way that we'd ever get 10 random answers that all matched the correct answer
//is if the random number input only allowed a single return value.
//IE: MyRandom(10, 10), or extremely bad luck....
While (nWrong == nAnswer && i < 10)
{
//Generate a new Wrong answer
nWrong = MyRandom(1, 10);
//Add 1 to our counter every time we generate a new Wrong answer to make sure that we'll exit on the 10th try.
// using ++ after an int will add 1 to whatever the int's current value is.
i++;
}
That should prevent more than a single answer being correct.
I haven't done enough with conversations to know how to declare tokens in code, or even how to call them in a conversation.
Someone else will need to chime in for that.
EDIT
Added comments to explain my code better so that begining programmers can understand it a bit easier.
Modifié par wyldhunt1, 10 janvier 2012 - 10:01 .