Frictional Games Forum (read-only)
[SCRIPT] How to search an array? - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: [SCRIPT] How to search an array? (/thread-16892.html)



How to search an array? - Cyfiawn - 07-11-2012

Hello,

I'm wondering how you can search an array for a certain value. I have declared my array: int[] bl(4,20);

I want to then set each array to a random, unique value, like so:
PHP Code:
for(int i 03i++)    
{    
 
//set "bl[i]" to a random number between 0 and 7    
   
bl[i] = RandInt(07);     
//if another value in the array is already set to this number, re-randomise until it is unique.    
   
while(bl[i] == !!!Search array for the same value!!!)    
   {
       
bl[i] = RandInt(07); 
   }


Any ideas what I need to put in to search my array?

Also, it would need to not include itself in the search (ie. if i = 2, it needs to ignore bl[2] in the search and only include bl[0], bl[1] and bl[3].

I see on the AngelScript documentation it has "find(syntax)", but I'm unsure how to use it.

I have a kind of "hacky" solution. It works, but I'm sure there is a better way. I'll use this for now, unless anyone can come up with some cleaner code.

Also, copy and pasting code from Notepad++ loses its formatting on this forum...kinda annoying. >_>
PHP Code:
    for(int i 04i++)
    {
bl[i] = RandInt(07);
if(
== 1)
{
while(
bl[i] == bl[0])
{
bl[i] = RandInt(07);
}
}
if(
== 2)
{
while(
bl[i] == bl[0] || bl[i] == bl[1])
{
bl[i] = RandInt(07);
}
}
if(
== 3)
{
while(
bl[i] == bl[0] || bl[i] == bl[1] || bl[i] == bl[2])
{
bl[i] = RandInt(07);
}
}

    } 



RE: How to search an array? - SilentStriker - 07-11-2012

If I'm not mistaken while loops cause the game to not work properly. (This has nothing to do with anything just wanted to put it out there Smile )


RE: How to search an array? - Cyfiawn - 07-11-2012

(07-11-2012, 07:13 PM)SilentStriker Wrote: If I'm not mistaken while loops cause the game to not work properly. (This has nothing to do with anything just wanted to put it out there Smile )
It appears to work at the moment...getting no duplicate numbers after many tests...not sure if I'm just getting lucky or not though. I'll try a smaller range, but it seems fine so far.

Edit: after testing, I can confirm I get no duplicates, and no crashing. I changed the RandInt to be from 0 to 3, meaning they have to all be unique (0, 1, 2 or 3) or be duplicates. Maybe I'm just lucky, but I've reloaded map 30+ times.


RE: How to search an array? - Your Computer - 07-11-2012

While loops are fine so long as they're not infinite loops. Not all array methods are implemented. However, the length and resize methods i know are implemented for Amnesia. Just use length() in a for loop and you should be fine. Though, you may want to call RandInt outside of the loop.