Import function in header error

Started by Dor Hajaj, Sat 22/12/2018 19:27:50

Previous topic - Next topic

Dor Hajaj

Hi,
What am I doing wrong in the following?

In GlobalScript.asc I have this function:
Code: ags

bool IsItemInList(String[] itemList, String itemName)
{
  for(int i=0; i< itemList.Length; i++)
  {
    if(itemList[i] == itemName)
      return true;
  }
  return false;
}


In GlobalScript.ash:
Code: ags

import bool IsItemInList(String[] itemList, String itemName);


I'm getting:
"Failed to save room room301.crm; details below
GlobalScript.ash(23): Error (line 23): PE02: Parse error at 'itemList'"

Why room 301? What's the connection to the function I added?
I use the function in other rooms but not in 301.

Thank you!

Khris

For parameters you need to put the [] behind the variable name:
Code: ags
bool IsItemInList(String itemList[], String itemName)

Snarky

Also, does AGS really have an array-length property? I have no memory of that existing. (And if this compiles, I would suspect that it's actually checking the String.Length property.)

Crimson Wizard

No, sadly in AGS arrays still don't have Length property.

There are two workarounds:
1) store array's length as a separate variable and also pass it to functions;
2) always keep one extra element in array which serves as the indicator of its end (this is how strings work in C programming language for example);
the second method will only suit you if there is a distinct value that is not allowed anywhere else in array (like "null" pointer).

Dor Hajaj

Thank you for your help.

Maybe I can further elaborate what I tried to do, because given the mentioned restrictions I don't think what I tried to do can work.
I'm trying to create a notebook, that when important information is discovered by the player, that data will be "written" to the notebook.
For that I tried to use a GUI with a listbox, so I can use the listbox.AddItem(), and add new items.
Of course, the problem is not adding the same thing twice.
So how do I check if a value exists before adding?

Khris

Why not test the listbox items directly?

Assuming you have an array of strings containing the items that will get added, you can just compare the item you want to add directly against the listbox items.
I usually dislike this approach because it doesn't separate game logic from presentation properly, but it should be fine here.

Code: ags

void AddItemToNotebook(String item)
{
  bool found = false;
  for(int i=0; i< lbNotebook.ItemCount; i++)
    if(lbNotebook.Items[i] == item) {
      found = true;
      break;
    }
  if (!found) lbNotebook.AddItem(item);
}


That way, using
Code: ags
  AddItemToNotebook(note[10]);

will only add the item the first time it's called.

Dor Hajaj

Exactly what I wanted!
Works great!
A super thanks to you Khris  :smiley:

SMF spam blocked by CleanTalk