Code diagnostics plz

Posted by Taizen Chisou on May 8, 2012, 5:35 p.m.

Honestly, I just put this together, so perhaps it was utterly foolish of me to expect this to work.

The idea is that the text object takes it's given string and separates it into smaller strings each with different parameters.

It takes a string like

"The idea is /inot /cto destroy the /bstrange monsters, /cbut rather to take them alive for further inspection by /ySeamus."

and turns it into

"The idea is not to destroy the strange monsters, but rather to take them alive for further inspection by Seamus."

For some reason, it doesn't do this.

All of the ministrings are blank and the formatting is not present.

length = string_length(str);

while(parseStringPos < length)
    {
    parseStringPos += 1;
    changed = false;
    
    if (string_char_at(str,parseStringPos) == "")
        {
        parseStringPos+=1;
        
        if ((string_char_at(str,parseStringPos) == "i") && (changed = false))
            {
            changed = true;
            currentString += 1;
            totalStrings += 1;
            stringFormatting[currentString] = 1;
            stringStartXinc[currentString] = stringStartXinc[currentString-1]+string_width(stringText[currentString-1]);
            stringStartYinc[currentString] = stringStartYinc[currentString-1];
            parseStringPos += 1;
            }
        if ((string_char_at(str,parseStringPos) == "b") && (changed = false))
            {
            changed = true;
            currentString += 1;
            totalStrings += 1;
            stringColor[currentString] = c_blue;
            stringStartXinc[currentString] = stringStartXinc[currentString-1]+string_width(stringText[currentString-1]);
            stringStartYinc[currentString] = stringStartYinc[currentString-1];
            parseStringPos += 1;
            }
         if ((string_char_at(str,parseStringPos) == "g") && (changed = false))
            {
            changed = true;
            currentString += 1;
            totalStrings += 1;
            stringColor[currentString] = c_lime;
            stringStartXinc[currentString] = stringStartXinc[currentString-1]+string_width(stringText[currentString-1]);
            stringStartYinc[currentString] = stringStartYinc[currentString-1];
            parseStringPos += 1;
            }
         if ((string_char_at(str,parseStringPos) == "r") && (changed = false))
            {
            changed = true;
            currentString += 1;
            totalStrings += 1;
            stringColor[currentString] = c_red;
            stringStartXinc[currentString] = stringStartXinc[currentString-1]+string_width(stringText[currentString-1]);
            stringStartYinc[currentString] = stringStartYinc[currentString-1];
            parseStringPos += 1;
            }
          if ((string_char_at(str,parseStringPos) == "y") && (changed = false))
            {
            changed = true;
            currentString += 1;
            totalStrings += 1;
            stringColor[currentString] = c_yellow;
            stringStartXinc[currentString] = stringStartXinc[currentString-1]+string_width(stringText[currentString-1]);
            stringStartYinc[currentString] = stringStartYinc[currentString-1];
            parseStringPos += 1;
            }
          if ((string_char_at(str,parseStringPos) == "s") && (changed = false))
            {
            changed = true;
            currentString += 1;
            totalStrings += 1;
            stringEffect[currentString] = 1;
            stringStartXinc[currentString] = stringStartXinc[currentString-1]+string_width(stringText[currentString-1]);
            stringStartYinc[currentString] = stringStartYinc[currentString-1];
            parseStringPos += 1;
            }
          if ((string_char_at(str,parseStringPos) == "c") && (changed = false))
            {
            changed = true;
            currentString += 1;
            totalStrings += 1;
            parseStringPos += 1;
            stringStartXinc[currentString] = stringStartXinc[currentString-1]+string_width(stringText[currentString-1]);
            stringStartYinc[currentString] = stringStartYinc[currentString-1];
            }
                
        }
    else if (string_char_at(str,parseStringPos) != "#")
        {
        stringText[currentString] += string_char_at(str,parseStringPos);
        }
    else
        {
        currentString += 1;
        totalStrings += 1;
        stringLines += 1;
        stringStartXinc[currentString] = 0;
        stringStartYinc[currentString] = stringLines * 12;
        stringColor[currentString] = stringColor[currentString-1];
        stringFormatting[currentString] = stringFormatting[currentString-1];
        stringEffect[currentString] = stringEffect[currentString-1];
        }
    }

Comments

Juju 12 years, 7 months ago

Jesus Christ on a bendy bus, I'll have a crack at this in the morning (7/8 hours).

Edit: Wait, is it meant to check for a forward slash in your first if-statement? You've got an empty string in there at the minute which makes no sense to me.

Charlie Carlo 12 years, 7 months ago

This is why I use pictures of text.

Juju 12 years, 7 months ago

That's a bad idea and you should feel bad (read: your solution wastes space and is entirely inflexible).

Rob 12 years, 7 months ago

Quote:
Where's Rob when you need him?

He's busy.

JuurianChi 12 years, 7 months ago

GoobyRob, plz!

Juju 12 years, 7 months ago

This should fix it. I've left in formatting persistence so you can stack colours with formatting with effects but I've left out the specific code for each tag, it won't be hard to add it in. Caveat Emptor: I haven't tested this, only eyeballed it.

length = string_length(str);

currentString = 0; //Initiate registers
stringLines = 0;
totalStrings = 0;

stringText[currentString] = ""; //Add first entry to the arrays
stringStartXinc[currentString] = 0;
stringStartYinc[currentString] = 0;
stringColor[currentString] = c_black;
stringFormatting[currentString] = 0;
stringEffect[currentString] = 0;

while( parseStringPos < length) { //Loop until EoS
    parseStringPos += 1; //Iterate like a motherfucker
    if (string_char_at(str,parseStringPos) == "/") { //Check for "begin tag" character
        parseStringPos += 1; //Bump to the next character
        switch (string_char_at(str,parseStringPos)) { //switch is my favourite function
            case "i": { //Is this the droid we are looking for?
                previousString = currentString //Saves on a little processing time
                currentString += 1; //Move register to the next row of the arrays
                totalStrings += 1;
                parseStringPos += 1;
                
                stringStartXinc[currentString] = stringStartXinc[previousString]+string_width(stringText[previousString]); //Space out along x axis
                tringStartYinc[currentString] = stringStartYinc[previousString]; //Align with previous y coord
                
                stringText[currentString] = ""; //Set stringText to empty
                stringFormatting[currentString] = 1; //Set new stringFormatting for current string
                stringColor[currentString] = stringColor[previousString]; //Copy stringColor from previous string
                stringEffect[currentString] = stringEffect[previousString]; //Copy stringEffect from previous string
                break
            }
            case "b": { //Yadda yadda
                break
            }
            default: {
                show_message("No recognised formatting tag found at "+string(parseStringPos)+"!") //Spit an error
                break
            }
        }
    } else if (string_char_at(str,parseStringPos) = "#") { //Look for GM's stupid newline character
        previousString = currentString
        currentString += 1; //Move register to the next row of the arrays
        totalStrings += 1;
        stringLines += 1; //Add an extra line to the visible text
        
        stringStartXinc[currentString] = 0; //Start again
        stringStartYinc[currentString] = stringLines * 12; //But space out along the y axis
        
        stringText[currentString] = ""; //Set stringText to empty
        stringColor[currentString] = stringColor[previousString]; //Copy stringColor from previous string
        stringFormatting[currentString] = stringFormatting[previousString]; //Copy stringFormatting from previous string
        stringEffect[currentString] = stringEffect[previousString]; //Copy stringEffect from previous string
        
    } else stringText[currentString] += string_char_at(str,parseStringPos); //Add text to the current string
}

Comment: It wouldn't be too hard to add wordwrapping functionality.

poultry 12 years, 7 months ago

This has been done before, and is on the GMC..I think Fox-NL made it.

Alert Games 12 years, 7 months ago

Also be sure to spot something that could be modularized right away. Its a bad idea to go all copy-pasta in your code. Make a script for things you know are consistent or could easily be repetitive to manipulate.

Charlie Carlo 12 years, 7 months ago

Quote:
That's a bad idea and you should feel bad (read: your solution wastes space and is entirely inflexible).

I take it that was directed at me? I wasn't making a suggestion, I was commenting on how this code stuff is way over my head.

In retrospect, both of these comments "waste space and (are) entirely inflexible." Yay, internet.

Castypher 12 years, 7 months ago

Yeah, I've got a system that parses a single character, but not two like you're doing. In other words, I'll point you toward Juju.