A Question about arrays and cell value comparison

Started by Dor Hajaj, Fri 07/12/2018 23:18:53

Previous topic - Next topic

Dor Hajaj

Hi,
Sorry this is really dumb one.
But it's freaking me out!
What could be the reason for the if statements in the GetEditorExpectationsInTextFormat() function not to be interpreted as true?
Code: ags

bool interviewExpectations[10];

function InitEditorExpectations()
{
  //init the array
  for(int i=0; i < 10; i++)
  {
    interviewExpectations[i] = true;
  }
}

String GetEditorExpectationsInTextFormat()
{
  String expectations = "";
  if(interviewExpectations[0] == true)
    expectations.Append("Aggressive, ");
  if(interviewExpectations[1] == true)
    expectations.Append("Emotional, ");
  if(interviewExpectations[2] == true)
    expectations.Append("Fictional, ");
  if(interviewExpectations[3] == true)
    expectations.Append("Friendly, ");
  ....
  return expectations;
}


Thank you!

morganw

The use of append is a little counter intuitive, since it returns a string rather than modifying the original.
So you would actually need something like this:
Code: ags
expectations = expectations.Append("Aggressive, ");

Crimson Wizard

#2
I would add, it's important to know that Strings in AGS are "immutable", which means they never change, only new strings are created as a result of some action (appending, cutting substring, even when changing single character). So if you want to get modified string in the same variable you always have to assign function result, as shown in above example.

Dor Hajaj

#3
Well sorry, but I'm still facing the same issue.
My string append usage was wrong, but I'm still unable to understand why the if expression is skipped at run time.
For example:
Code: ags

 String expectations = "";
 interviewExpectations[0] = true;
 if(interviewExpectations[0] == true)
   expectations = expectations.Append("Aggressive, ");

Hope you can help me!

Khris

Please post the relevant code in full, and tell us exactly what you're doing and what the result is.

Quote from: Dor Hajaj on Sat 08/12/2018 15:49:59I'm still unable to understand why the if expression is skipped at run time
That's because it isn't. AGS doesn't randomly skip statements or blocks. There's an error in your code, and we should be able to point it out if we see the whole thing.

Dor Hajaj

Well, the code is basically the same as I posted in the original post, plus the fix for the string appending error I did.
So:

I have a Boolean array that should represent a certain desired state for the player.
Some of the cells will be false, some will be true depending on the situation.
For the sake of understanding what am I doing wrong, I set them all to be true.
Now, on the GetEditorExpectationsInTextFormat() function I would like to generate a string representation of the desired state.
So for each cell in true state, add something to a string. In the end return the string.
What I meant is, that despite the entire array set as true, none of my If statements are considered true, so nothing happens.

Code: ags

bool interviewExpectations[10];
 
function InitEditorExpectations()
{
  //init the array
  for(int i=0; i < 10; i++)
  {
    interviewExpectations[i] = true;
  }
}
 
String GetEditorExpectationsInTextFormat()
{
  String expectations = "";
  if(interviewExpectations[0] == true)
    expectations.Append("Aggressive, ");
  if(interviewExpectations[1] == true)
    expectations.Append("Emotional, ");
  if(interviewExpectations[2] == true)
    expectations.Append("Fictional, ");
  if(interviewExpectations[3] == true)
    expectations.Append("Friendly, ");
  ....
  return expectations;
}

Thanks

Snarky

#6
The error is not where you think it is.

Things to check:
-Is InitEditorExpectations() actually called, and before GetEditorExpectationsInTextFormat()?
-Are the two functions in the same script? If not, how are you exporting and importing interviewExpectations[]?
-Are you sure you're actually checking the return value of GetEditorExpectationsInTextFormat()?
-What's in that "...." in the code?

(I've got my money on the second point, and that you've inadvertently created two different interviewExpectations[] arrays.)

Dor Hajaj

1) Yes. I also debugged and made sure it does.
2) Everything is done in the GlobalScript. I didn't export anything, I just stated the array like the code snippet shows(was I supposed to?)
3) Yes.
4) I meant there are a couple of more If statements that follow, didn't want to trouble the readers :)

I really appreciate the help!

Snarky

Interesting...

Your latest sample still has the string append error, but I'm not sure whether that's because you haven't fixed it or because you just copy-pasted from an earlier post. Could you please just post your code exactly as it is (ideally along with the functions that call it and an explanation of exactly how you know it's not doing the expected thing)? There is bound to be a mistake somewhere, but we won't be able to spot it if you don't include it.

Dor Hajaj


1. EMAILInteraction() is called.
2. The last if statement, is a part of a longer function. At some point the if is true and what's inside runs. The problem is that the string is empty after the GetEditorExpectationsInTextFormat() call.


Code: ags



bool interviewExpectations[10];


function InitEditorExpectations()
{
  //init the array
  for(int i=0; i < 10; i++)
  {
    interviewExpectations[i] = true;
  }
}


String GetEditorExpectationsInTextFormat()
{
  String expectations = "";
  if(interviewExpectations[0] == true)
    expectations = expectations.Append("Aggressive, ");
  if(interviewExpectations[1] == true)
    expectations = expectations.Append("Emotional, ");
  if(interviewExpectations[2] == true)
    expectations = expectations.Append("Fictional, ");
  if(interviewExpectations[3] == true)
    expectations = expectations.Append("Friendly, ");
  if(interviewExpectations[4] == true)
    expectations = expectations.Append("Informative, ");
  if(interviewExpectations[5] == true)
    expectations = expectations.Append("Interesting, ");  
  if(interviewExpectations[6] == true)
    expectations = expectations.Append("Intrusive, ");    
  if(interviewExpectations[7] == true)
    expectations = expectations.Append("Personally Viewed, ");
  if(interviewExpectations[8] == true)
    expectations = expectations.Append("Tabloid, ");     
  if(interviewExpectations[9] == true)
    expectations = expectations.Append("Thrilling, ");
  return expectations;
}
function SetEditorExpectations()
{
  int rand = Random(9);
  
  switch(rand)
  {
    case 0:
    interviewExpectations[0] = true;
    break;
    case 1:
    interviewExpectations[1] = true;
    break;
    case 2:
    interviewExpectations[2] = true;
    break;
    case 3:
    interviewExpectations[3] = true;
    break;
    case 4:
    interviewExpectations[4] = true;
    break;
    case 5:
    interviewExpectations[5] = true;
    break;
    case 6:
    interviewExpectations[6] = true;
    break;
    case 7:
    interviewExpectations[7] = true;
    break;
    case 8:
    interviewExpectations[8] = true;
    break;
    case 9:
    interviewExpectations[9] = true;
    break;
  }
}



function EMAILInteraction()
{
  cEgo.ChangeRoom(67);
  CurrentRoom = 67;
  OutputBox.Visible = false;
  BBCOutputBox.Visible = true;
  BBCOutputBox.Text = "Electronic Messaging System:[1.Incoming[2.Outgoing(currently unavailable)";
  if(editorHasSentExpectations)
  {
    InitEditorExpectations();
    SetEditorExpectations();
    SetEditorExpectations();
    SetEditorExpectations();
  }
}



if(TextBox1.Text == "1")
{
      BBCOutputBox.Text = "Retrieving information...";
      Wait(100);
      if(editorHasSentExpectations)
      {
        String expectations = GetEditorExpectationsInTextFormat();
        if(expectations.Length > 0)
        {
          BBCOutputBox.Text = String.Format("The story is expected to be:",expectations);
          WriteToFile(expectations, "editor_Expectations.txt");
        }
      }
}




morganw

Your String.Format is missing a token to say how to insert the text:
Code: ags
String.Format("The story is expected to be:",expectations);
Try this:
Code: ags
String.Format("The story is expected to be: %s", expectations);

Khris

#11
Not really concerning the current issue, but you can replace your SetEditorExpectations() function with this:

Code: ags
function SetEditorExpectations() {  interviewExpectations[Random(9)] = true;}



You're also doing this cEgo.ChangeRoom(67);, then you're calling a bunch of other functions. The change room command will be queued, and only runs once the current function has finished.
It also looks like you'll want to check your InitEditorExpectations() function, since it's setting all those bools to true, which causes the three SetEditorExpectations() to have zero effect.

You simply need to do basic debugging: add lots of Display() commands to your code, and make sure that variables actually contain what you think they contain.

morganw

I imagine the goal of this is to pick 3 unique items from the array, so I thought I'd give it a try as there didn't seem to be an easy or obvious solution. Is there any better way than this, if the array values aren't used elsewhere?
Code: ags
readonly int EXPECTATION_COUNT = 10;

String ChoosePhrase(String phrases[], int count, int current)
{
  String expectations = "";
  
  // everything but the first phrase uses a separator
  if (current > 1)
  {
    if (count > 2)
    {
      expectations = expectations.Append(",");
    }
    
    expectations = expectations.Append(" ");
  }
    
  // the final phrase also includes "and "
  if (current == count && count != 1)
  {
    expectations = expectations.Append("and ");
  } 
  
  // pick a phrase
  int pick = Random(EXPECTATION_COUNT - current);
  expectations = expectations.Append(phrases[pick]);
  
  // if not done
  if (current != count)
  {
    // shift array elements and choose again
    for (int i = pick; i < EXPECTATION_COUNT - current; i ++)
    {
      phrases[i] = phrases[i + 1];
    }
    
    expectations = expectations.Append(ChoosePhrase(phrases, count, current + 1));
  }

  return expectations;
}

String GetEditorExpectations(int count)
{
  String interviewExpectations[];
  
  interviewExpectations = new String[EXPECTATION_COUNT];
  interviewExpectations[0] = "Aggressive";
  interviewExpectations[1] = "Emotional";
  interviewExpectations[2] = "Fictional";
  interviewExpectations[3] = "Friendly";
  interviewExpectations[4] = "Informative";
  interviewExpectations[5] = "Interesting";
  interviewExpectations[6] = "Intrusive";
  interviewExpectations[7] = "Personally Viewed";
  interviewExpectations[8] = "Tabloid";
  interviewExpectations[9] = "Thrilling";
  
  return ChoosePhrase(interviewExpectations, count, 1);
}

function WhateverYourTriggerIs()
{
  String expectations = GetEditorExpectations(3);
  
  if (expectations != null)
  {
    String display = String.Format("The story is expected to be: %s", expectations);
    Display(display);
  }
}

Dor Hajaj

Thank you all for you valuable advices!
I'm still trying to figure this thing out...

Quote from: morganw on Tue 11/12/2018 22:18:16
I imagine the goal of this is to pick 3 unique items from the array, so I thought I'd give it a try as there didn't seem to be an easy or obvious solution. Is there any better way than this, if the array values aren't used elsewhere?
Code: ags

String GetEditorExpectations(int count)
{
  String interviewExpectations[];
  
  interviewExpectations = new String[EXPECTATION_COUNT];
  interviewExpectations[0] = "Aggressive";
  interviewExpectations[1] = "Emotional";
  interviewExpectations[2] = "Fictional";
  interviewExpectations[3] = "Friendly";
  interviewExpectations[4] = "Informative";
  interviewExpectations[5] = "Interesting";
  interviewExpectations[6] = "Intrusive";
  interviewExpectations[7] = "Personally Viewed";
  interviewExpectations[8] = "Tabloid";
  interviewExpectations[9] = "Thrilling";
  
  return ChoosePhrase(interviewExpectations, count, 1);
}


morganw, That's a really lovely piece of code!
I tried using your code - but I get a compilation error : "GlobalScript.asc(78): Error (line 78): Expected array index after 'interviewExpectations'" -  which point to the end of the GetEditorExpectations() function (line 17 on the shortened quote I attached)

About the general idea - The expectations, are supposed to be used later when the player completes the mission, each type of expectation is given a score(I save them on a different array and compare by the indexes).
When the player performance is judged, I take the story(as the player is a journalist) expectations, and check whether the player reached the minimum score for each of them.



morganw

#14
Okay, I did check it before I posted it but if you need to store the choices the above way wouldn't work.

Try this instead:
Code: ags
readonly int EXPECTATION_COUNT = 10;

String[] GetEditorExpectations(int count)
{
  String expectations[] = new String[EXPECTATION_COUNT];

  expectations[0] = "Aggressive";
  expectations[1] = "Emotional";
  expectations[2] = "Fictional";
  expectations[3] = "Friendly";
  expectations[4] = "Informative";
  expectations[5] = "Interesting";
  expectations[6] = "Intrusive";
  expectations[7] = "Personally Viewed";
  expectations[8] = "Tabloid";
  expectations[9] = "Thrilling";
  
  String chosen[] = new String[count];
  
  for (int i = 0; i < count; i ++)
  {
    int max = EXPECTATION_COUNT - 1 - i;
    
    // pick a phrase
    int pick = Random(max);
    chosen[i] = expectations[pick];
    
    // shift array elements
    for (int j = pick; j < max; j ++)
    {
      expectations[j] = expectations[j + 1];
    }
  }
  
  return chosen;
}

String GetExpectationsAsString(String expectations[], int max)
{
  String text = "";
  
  for (int i = 1; i <= max; i ++)
  {
    // everything but the first phrase uses a separator
    if (i > 1)
    {
      if (max > 2)
      {
        text = text.Append(",");
      }
      
      text = text.Append(" ");
    }
    
    // the final phrase also includes "and "
    if (i == max && max != 1)
    {
      text = text.Append("and ");
    }
    
    text = text.Append(expectations[i - 1]);
  }

  return text;
}

function WhateverYourTriggerIs()
{
  // how many expectations to get
  int choose = 3;
  // store these somewhere for use later
  String expectations[] = GetEditorExpectations(choose);
  // get expectations as text
  String text = GetExpectationsAsString(expectations, choose);
  
  if (text != null)
  {
    String display = String.Format("The story is expected to be: %s", text);
    Display(display);
  }
}

Dor Hajaj

Hi,
Just updating that the latest code by morganw + the string append fix worked.
:-D

Thank you!

SMF spam blocked by CleanTalk