- Home /
Why isn't my word generator working?
I'm not a good coder; nevertheless, I was wondering if anyone could be so generous as to help me figure out why my code won't run. The script is a word generator intended to make a made-up language for an interactive comic book. I just can't seem to figure out how to make the script work. I hope someone can help me. It should be a relatively simple script to write, yet I find myself struggling tremendously. In the script, I went as far as to include notes to make it easier to understand. If the word generator works, I would gladly let others use it. (Please don't be daunted by it's length. It's long because I reused the switch statement for all of it's 8 parts)
#pragma strict
var wordCount : int = 10; //set this to desired amount of words (when the code works); use 10 for testing purposes.
function Start () {
//get number of words you want to make.
for (var x = 0; x< wordCount; x++){
wordGenerator(); //execute wordGenerator function to generate words.
}
}
Debug.Log(word);//print words on screen.
//wordGenerator function
function wordGenerator (){
//arrays
var commons : char ["a","i","u","e","o","ka","ki","ku","ke","ko","sa","shi","su","se","so","ta","chi","tsu","te","to","na","ni","nu","ne","no","ha","hi","fu","he","ho","ma","mi","mu","me","mo","ya","yu","yo","ra","ri","ru","re","ro","wa","n"];
var all : char ["a","i","u","e","o","ba","bi","bu","be","bo","da","di","du","de","do","fa","fi","fu","fe","fo","ga","gi","gu","ge","go","ha","hi","hu","he","ho","ja","ji","ju","je","jo","ka","ki","ku","ke","ko","la","li","lu","le","lo","ma","mi","mu","me","mo","na","ni","nu","ne","no","pa","pi","pu","pe","po","ra","ri","ru","re","ro","sa","si","su","se","so","ta","ti","tu","te","to","va","vi","vu","ve","vo","wa","wi","wu","we","wo","xa","xi","xu","xe","xo","ya","yi","yu","ye","yo","za","zi","zu","ze","zo"];
var addOns : char ["à","è","ì","ò","ù","á","é","í","ó","ú","â","ê","î","ô","û","ā","ī","ū","ē","ō","ã","ĩ","ũ","ẽ","õ","ă","ĭ","ŭ","ĕ","ŏ","ȃ","ȋ","ȗ","ȇ","ȏ","°"];
var endAddOn : char ["ll","s","ss","id","ia","ing","ung","eng","ong"];
//variables
var nothing : char = " "; //space varable.
var word : char; //the function will return this as a full word at the end of the function.
//part variables (not sure it should be a int or char).
var p1 : char;
var p2 : char;
var p3 : char;
var p4 : char;
var p5 : char;
var p6 : char;
var p7 : char;
var p8 : char;
/*
var t1 : int; //incase I need a var to get a part variable as a number and convert it using an integer to string statment. T stands for tempuary.
var t2 : int;
var t3 : int;
var t4 : int;
var t5 : int;
var t6 : int;
var t7 : int;
var t8 : int;
*/
var ranNum : int; //random number variable for the switch statments.
ranNum = Math.random(1,100);//gets a random number from 1 to 100 for the switch statment. The 1-100 will also act as my percentages(1%-100%) for the list and random letters in an arrays to select a syllable for a particular part.
//get p1; all parts (p2-p8)have the same code except for part1.
p1 = switch(ranNum){
case 1:
if (ranNum > 30){
p1 = Math.random()*(commons.length-1); //get a random number(or char) number from the commons array.
}
break;
case 2:
if (ranNum >40){
p1 = Math.random()*(all.length-1);//get a random number(or char) from the all array.
}
break;
case 3:
if (ranNum >60){
p1 = Math.random()*(addOns.length-1);//get a random number(or char) from the addOns array.
}
break;
}
ranNum = Math.random(1,100);
// get P2
p2 = switch(ranNum){
case 1:
if (ranNum > 30){
p2 = Math.random()*(commons.length-1);
}
break;
case 2:
if (ranNum >40){
p2 = Math.random()*(all.length-1);
}
break;
case 3:
if (ranNum >60){
p2 = Math.random()*(addOns.length-1);
}
break;
case 4:
if (ranNum >70){
p2 = Math.random()*(endAddOn.length-1); //get a random number(or char) from the endAddOn array.
}
break;
case 5:
if (ranNum >90){
p2 = nothing; //insert a space (after a digit gets a space all other parts get a space.
}
break;
}
ranNum = Math.random(1,100);
//get p3
p3 = switch(ranNum){
case 1:
if (ranNum > 30){
p3 = Math.random()*(commons.length-1);
}
break;
case 2:
if (ranNum >40){
p3 = Math.random()*(all.length-1);
}
break;
case 3:
if (ranNum >60){
p3 = Math.random()*(addOns.length-1);
}
break;
case 4:
if (ranNum >70){
p3 = Math.random()*(endAddOn.length-1);
}
break;
case 5:
if (ranNum >90){
p3 = nothing;
}
break;
}
if (p2 = nothing){
p3 = nothing; //if part before this one gets a space (nothing variable), then it also gets a space; include all the other parts.I mentioned this before in part1's switch statment.
}
ranNum = Math.random(1,100);
//get p4
p4 = switch(ranNum){
case 1:
if (ranNum > 30){
p4 = Math.random()*(commons.length-1);
}
break;
case 2:
if (ranNum >40){
p4 = Math.random()*(all.length-1);
}
break;
case 3:
if (ranNum >60){
p4 = Math.random()*(addOns.length-1);
}
break;
case 4:
if (ranNum >70){
p4 = Math.random()*(endAddOn.length-1);
}
break;
case 5:
if (ranNum >90){
p4 = nothing;
}
break;
}
if (p3 = nothing){
p4 = nothing;
}
ranNum = Math.random(1,100);
//get P5
p5 = switch(ranNum){
case 1:
if (ranNum > 30){
p5 = Math.random()*(commons.length-1);
}
break;
case 2:
if (ranNum >40){
p5 = Math.random()*(all.length-1);
}
break;
case 3:
if (ranNum >60){
p5 = Math.random()*(addOns.length-1);
}
break;
case 4:
if (ranNum >70){
p5 = Math.random()*(endAddOn.length-1);
}
break;
case 5:
if (ranNum >90){
p5 = nothing;
}
break;
}
if (p4 = nothing){
p5 = nothing;
}
ranNum = Math.random(1,100);
//get p6
p6 = switch(ranNum){
case 1:
if (ranNum > 30){
p6 = Math.random()*(commons.length-1);
}
break;
case 2:
if (ranNum >40){
p6 = Math.random()*(all.length-1);
}
break;
case 3:
if (ranNum >60){
p6 = Math.random()*(addOns.length-1);
}
break;
case 4:
if (ranNum >70){
p6 = Math.random()*(endAddOn.length-1);
}
break;
case 5:
if (ranNum >90){
p6 = nothing;
}
break;
}
if (p5 = nothing){
p6 = nothing;
}
ranNum = Math.random(1,100);
//get p7
p7 = switch(ranNum){
case 1:
if (ranNum > 30){
p7 = Math.random()*(commons.length-1);
}
break;
case 2:
if (ranNum >40){
p7 = Math.random()*(all.length-1);
}
break;
case 3:
if (ranNum >60){
p7 = Math.random()*(addOns.length-1);
}
break;
case 4:
if (ranNum >70){
p7 = Math.random()*(endAddOn.length-1);
}
break;
case 5:
if (ranNum >90){
p7 = nothing;
}
break;
}
if (p6 = nothing){
p7 = nothing;
}
ranNum = Math.random(1,100);
//get p8
p8 = switch(ranNum){
case 1:
if (ranNum > 30){
p8 = Math.random()*(commons.length-1);
}
break;
case 2:
if (ranNum >40){
p8 = Math.random()*(all.length-1);
}
break;
case 3:
if (ranNum >60){
p8 = Math.random()*(addOns.length-1);
}
break;
case 4:
if (ranNum >70){
p8 = Math.random()*(endAddOn.length-1);
}
break;
case 5:
if (ranNum >90){
p8 = nothing;
}
break;
}
if (p7 = nothing){
p8 = nothing;
}
//combine all parts to form a word and return it so that it can go back to it's original module to be printed to the screen.
word = p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
return word;
}
You have a lot of duplicate code in there, you could clean it up easily if you used some functions.
When you say: "It isn't working", that's completely useless information! What isn't working? Do you get any compiler errors? (you should) What have you done to fix it? Why do you think it's not working the way you want it to work? What is it currently doing that it shouldn't do?
All of these are questions you should consider answering when asking questions. Otherwise we're just guessing...
do you $$anonymous$$d if I switch this to C# from Javascript?
Also, do you really want all words to be made from 8 parts? That will give you very long words and you will miss all the nice short ones or is this what you try to do with the space? If one is space, next will be space too, so they wont be executed?
And why are you putting endAddons into the word?
Can you summarize how you want to construct your words in simple english without trying to explain the technical parts?
@Happy$$anonymous$$oo, you shouldn't edit other peoples questions except for formatting or other, non-destructive corrections. Usually the problem that person has is in the code, so converting it won't just confuse the OP but will also hide the initial issue :)
Oh I didn't meant to touch his question... just convert his code... as an answer...
But seeing his other question are also Javascript, that would probably confuse more than help
Answer by HappyMoo · Jan 07, 2014 at 08:27 AM
@Kasigawa, I created the randomPick function to allow you to express your word generation ideas in a more declarative way and then I created a generateWord function that I think is in the spirit of what you wanted to do, but may deviate from what you actually wrote. It should be way easier now to customize this to what you actually want exactly
#pragma strict
private var wordCount : int = 100;
private var commons : String[] = ["a","i","u","e","o","ka","ki","ku","ke","ko","sa","shi","su","se","so","ta","chi","tsu","te","to","na","ni","nu","ne","no","ha","hi","fu","he","ho","ma","mi","mu","me","mo","ya","yu","yo","ra","ri","ru","re","ro","wa","n"];
private var all : String[] = ["a","i","u","e","o","ba","bi","bu","be","bo","da","di","du","de","do","fa","fi","fu","fe","fo","ga","gi","gu","ge","go","ha","hi","hu","he","ho","ja","ji","ju","je","jo","ka","ki","ku","ke","ko","la","li","lu","le","lo","ma","mi","mu","me","mo","na","ni","nu","ne","no","pa","pi","pu","pe","po","ra","ri","ru","re","ro","sa","si","su","se","so","ta","ti","tu","te","to","va","vi","vu","ve","vo","wa","wi","wu","we","wo","xa","xi","xu","xe","xo","ya","yi","yu","ye","yo","za","zi","zu","ze","zo"];
private var addOns : String[] = ["à","è","ì","ò","ù","á","é","í","ó","ú","â","ê","î","ô","û","ā","ī","ū","ē","ō","ã","ĩ","ũ","ẽ","õ","ă","ĭ","ŭ","ĕ","ŏ","ȃ","ȋ","ȗ","ȇ","ȏ","°"];
private var endAddOn : String[] = ["ll","s","ss","id","ia","ing","ung","eng","ong"];
private var words : Array = new Array();
function Start () {
for (var x = 0; x< wordCount; x++){
var word = generateWord();
print(word);
words.push(word);
}
}
function generateWord (){
var word:String = "";
for(var i:int = 0; i<8; i++) // up to 8 word parts
{
word += randomPick([
60, commons,
30, all,
10, addOns
]);
if (i>0 && Random.value < 0.2) break; // 20% chance to stop here in all but the first step
}
word += randomPick([ // 50% chance of adding endAddOn at the end
50, endAddOn,
50, ""
]);
return word;
}
// arguments come in pairs of probability and choice, like randomPick([40,"A", 10,"B"])
// Probabilities don't need to add up to 100% - their relative size is important.
// The example above picks "A" 80% of the time and "B" 20% of the time
// Another Example: randomPick([1,"Third", 2,"TwoThirds"])
// If the choice is an Array, it will pick one of the elements randomnly:
// randomPick([2, ["John","Luke"], 6,"Anna"]). This will pick Anna 3 times as often as one of the other Names,
// i.e. 25% (John or Luke), 75% Anna or completelly decoded: 12.5% John, 12.5% Luke, 75% Anna
function randomPick(args:Object[]) : Object
{
var pSum:float = 0;
for(var i:int = 0; i<args.length; i+=2) pSum += parseFloat(args[i].ToString());
var p:float = Random.Range(0f, pSum);
var runningP:float = 0f;
for(i = 0; i<args.length; i+=2)
{
runningP += parseFloat(args[i].ToString());
if (p<=runningP)
{
var ret:Object = args[i+1];
// Too many array types in UnitScript
if (typeof(ret)==typeof(Array))
return (ret as Array)[Random.Range(0, (ret as Array).length)];
if (typeof(ret)==typeof(Object[]) || typeof(ret)==typeof(String[]))
return (ret as Object[])[Random.Range(0, (ret as Object[]).Length)];
// else:
return ret;
}
}
return "";
}
TestRun:
udueẽminanta diserimomichihe setenohiô tare hukoyoid lineninu kanis majokuja harosoung mianafuia taloomaziranuyieng livoeyu kovutaluisorora nalaiewaterenid sukuvasugaàong sosakatsuwu masuid raùfukafaketoũ hinakiisunuia yukis yoorimaung furueng atahofurui rusutekueng eyotemimimumaka soyumetamuress sonueng youheshifoē haóketehakimamuung batirasikakuno pohaxopenokoȋno kuûyanopi memu zokahaĭpeid yabiss rose ȗhoaneke okoseli ûtopashieng re°kesemaing îkeke etesozira chiunimeniss ēayayuyesohake ekafashimanikefis jemuĕzumimawaki kiwaung iȋong tosagemeeng meĩ jihatsueng ganouchi uzetsudihuūsoyoss iba xoyujuro ûseid teronjeshino émoung teshipa gukesa ĕheuung tahofo raòid árajoll îkeyunuilixato dehukowehirilitoong meronarureung emukateô yuhumikuraweus totaifuwabióro hifiyatayame ngozexurahate chichiarakaia yaoòoung suȗtutemumezike kisarireyadiû hohaíriyaxuâia ranàrinnjaȇ sakayuyoeng imirekumamipitsuss meekuoèhageia yuesaia sefumakayosayoyaeng zewafuna wayasuhonmuōke erumito zechijill natakeá sali nabi tsuchi ruhiîùneuname sukaraong koãyaki jimehell jamavonehehisuri tiaia ifu mariid miutereing
@Happy$$anonymous$$oo This is perfect. A million thank yous! I never though the random pick could have been a function. I also see that I lack syntax grammar. Like how I used char ins$$anonymous$$d of String. I also see that the probability of chance became a big part of the code as well. It's definitely done completely different then what I have imagined. I'm going to have to dissect this code to fully understand what is happening. Again thank you.
@Benproductions I acknowledge on my post I should have stated my bugs clearly, but the bugs I got didn't make any sense (missing semicolons on statements with semicolons; invalid switch tokens; and other bugs I didn't understand). I was also uncertain on how to write the code. I'm not much a coder anymore, this stuff is really hard for me now. $$anonymous$$y apologies.
Thank you, doing this was fun. Please don't post comments as answers. "Answers" are supposed to answer the original question - have a look at the tutorial video linked in the right bar. I'm turning your answer into a comment.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Player invisible after reloading scene 1 Answer
Add force to GameObject 2 Answers
BCE0044: expecting }, found 'private'. PLEASE HELP!!!!! 2 Answers