Error in code - text doesn't get appended? [SOLVED]

Started by NsMn, Sun 11/10/2009 10:11:04

Previous topic - Next topic

NsMn

Hey all,

I got another problem that I just don't have a clue how to fix it.
I have this little function:

Code: ags
function ListInventory(){
  String Text="Inventory:[";
  int for=1;
  if (InvOne.ItemCount == 0)Say("I don't have an item now.");
  else{
    Text.Append(InvOne.ItemAtIndex[0].Name);
  while (for < InvOne.ItemCount){
    Text.Append(String.Format(", %s", InvOne.ItemAtIndex[for].Name));
    for++;
  }
Say(Text.Copy());  //Say(); is a custom speech function
}
}


However, all the function returns is the "Inventory:" and an empty line.
Any of you see the error in this code?

By the way, I tested it with no items in the window, 1 item and 2 items, the only thing that worked was no item.

tzachs

I don't have the manual next to me, but if I remember correctly it should be
Text = Text.Append(...);

NsMn

#2
Oh, yeah... that's the problem... I actually always remember this, but not this time, obviously. Thanks tzachs!


NsMn

Well... I've got an even longer code and a even wackier result now. This:

Code:

Code: ags

function AddEmptyLines(this String*, Label*Notadd){
  int Amountoflines = GetTextHeight(this.Copy(), eFontFont3, lblDocBlack.Width) / FontHeight;
  int for;
  while (for<Amountoflines){
    if(Notadd != lblDocBlack)lblDocBlack.Text=lblDocBlack.Text.Append("[");
    if(Notadd != lblDocPurple)lblDocPurple.Text=lblDocPurple.Text.Append("[");
    if(Notadd != lblDocBlue)lblDocBlue.Text=lblDocBlue.Text.Append("[");
    if(Notadd != lblDocGreen)lblDocGreen.Text=lblDocGreen.Text.Append("[");
    for++;
  }
  }


function AddTextToDoc(String Text,  Label* lblDoc){
  if (GetTextHeight(lblDoc.Text, eFontFont3, lblDoc.Width) >= lblDoc.Height - FontHeight){
    lblDocBlack.Text="";
    lblDocPurple.Text="";
    lblDocGreen.Text="";
    lblDocBlue.Text="";
    }
  
  if (lblDoc.Text == ""){lblDoc.Text=lblDoc.Text.Append(String.Format("%s",Text)); Text.AddEmptyLines(lblDoc);}
  else {lblDoc.Text=lblDoc.Text.Append(String.Format("[%s",Text)); Text.AddEmptyLines(lblDoc);}
  
  if (GetTextHeight(lblDoc.Text, eFontFont3, lblDoc.Width) >= lblDoc.Height - FontHeight) {
    lblDocBlack.Text="";
    lblDocPurple.Text="";
    lblDocGreen.Text="";
    lblDocBlue.Text="";
    }
  if (lblDoc.Text == ""){lblDoc.Text=lblDoc.Text.Append(String.Format("%s",Text)); Text.AddEmptyLines(lblDoc);}
}


...Should add empty Lines to the other labels that use other colours. But, anyhow, it always adds too many or none, and I can't find the problem. Could be something with the GetTextHeight function, but that doesn't help fixing the problem either. Sigh.

Gilbert

I haven't read thoroughly your codes yet, but as far as I remember, local variables within functions aren't always necessarily initialised as 0 (maybe it's fixed), so it may not be safe to use the variable for as running index immediately without defining its value first.

Lufia

First thing, you have a int for that serves as a counter. It might be a good idea to set a starting value for it. int for = 1, or something.

Then, I don't really understand your AddEmptyLines function. It's supposed to add three line breaks (one in each label that isn't the one passed as an argument) each time your while loop takes place ?

I find your second function equally confusing, but I think I need to read up on GetTextHeight.

My question is : What are you trying to do ?

NsMn

#6
Well, the purpose of the second function is to add a new line of text to a label (like in a DOS window, or seomthing like that).
But, since I wanted to use different colours, I had to use different Labels which have the same property expecting the text colour, so I added the code and the first function to Add the actual text to the label that has the colour, and pass empty lines to the other Labels, so that the text added to them is in the right position, not over already existing text.

The while loop is made for 2- or 3-line texts. But, I will try to give the for integer a value of 1 and change the for<Amountoflines to for<=Amountoflines, maybe this works. =>Edit: No it doesn't.

Edit: I now find out that the actual problem is the blue label, I will look what's the problem with that, maybe there's something wrong with its properties.

Latest EDit: Forget all the stuff I wrote I suspected... I have another lead. Don't help me now please :P

SMF spam blocked by CleanTalk