Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Dor Hajaj on Sat 22/12/2018 19:27:50

Title: Import function in header error
Post by: Dor Hajaj on Sat 22/12/2018 19:27:50
Hi,
What am I doing wrong in the following?

In GlobalScript.asc I have this function:
Code (ags) Select

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) Select

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!
Title: Re: Import function in header error
Post by: Khris on Sun 23/12/2018 13:31:25
For parameters you need to put the [] behind the variable name:
Code (ags) Select
bool IsItemInList(String itemList[], String itemName)
Title: Re: Import function in header error
Post by: Snarky on Sun 23/12/2018 13:43:15
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.)
Title: Re: Import function in header error
Post by: Crimson Wizard on Sun 23/12/2018 15:54:41
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).
Title: Re: Import function in header error
Post by: Dor Hajaj on Sun 23/12/2018 18:37:04
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?
Title: Re: Import function in header error
Post by: Khris on Sun 23/12/2018 19:15:16
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) Select

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) Select
  AddItemToNotebook(note[10]);
will only add the item the first time it's called.
Title: Re: Import function in header error
Post by: Dor Hajaj on Sun 23/12/2018 19:25:09
Exactly what I wanted!
Works great!
A super thanks to you Khris  :smiley: