Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script Help How to search an array?
Cyfiawn Offline
Junior Member

Posts: 27
Threads: 8
Joined: Jun 2012
Reputation: 0
#1
How to search an array?

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: (Select All)
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: (Select All)
    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);
}
}

    } 
(This post was last modified: 07-11-2012, 07:08 PM by Cyfiawn.)
07-11-2012, 06:18 PM
Find
SilentStriker Offline
Posting Freak

Posts: 950
Threads: 26
Joined: Jul 2011
Reputation: 43
#2
RE: How to search an array?

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 )

07-11-2012, 07:13 PM
Find
Cyfiawn Offline
Junior Member

Posts: 27
Threads: 8
Joined: Jun 2012
Reputation: 0
#3
RE: How to search an array?

(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.
(This post was last modified: 07-11-2012, 07:21 PM by Cyfiawn.)
07-11-2012, 07:18 PM
Find
Your Computer Offline
SCAN ME!

Posts: 3,456
Threads: 32
Joined: Jul 2011
Reputation: 235
#4
RE: How to search an array?

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.

Tutorials: From Noob to Pro
(This post was last modified: 07-11-2012, 09:31 PM by Your Computer.)
07-11-2012, 09:28 PM
Website Find




Users browsing this thread: 2 Guest(s)