Having 1 object constantly check through 35 numbers for no match

Hey GS,
Sorry if the post title was confusing. Was trying to figure out the best way to explain it in the title! LOL So here it is.

   In my game I have a main character that changes to a random number between 0 and 3 every 5 seconds. In the scene I have 35 enemies that change to a random number between 0 and 3 at the beginning of the game and every time it is touched. If the enemy is touched and matches the main characters number then you get a point and if it doesn't match you lose a point. 

   The one thing I need to do now is have the main character detect if there is no more enemies in the scene with a matching number and if not then to randomly change the number between 0 and 3. 

   Have been trying to thing of the best way for this and I was thinking of using a table. Was thinking of creating a table with 1 column and 35 rows. Assign each of the enemies to their own row. Every time the enemy chooses it's random number between 0 and 3 it logs it in its row. Then the main character constantly checks all 35 rows and if a matching number does not exist in any of the rows to change to a new random number between 0 and 3. 

    Was wondering if there was a simple line of code that I could basically say, if main character random number is not equal to table rows 1 through 35 then to change its random number. Or is it more complicated than that?

    Basically what I am needing to accomplish is if there is no more enemies in the scene with matching random numbers, the main character will change its random number so the player is not sitting there waiting for the main character to change its random number after the 5 seconds. Hopefully that all makes sense and what I am trying to accomplish

Comments

  • JamieOneilJamieOneil Member Posts: 877

    You could use tables, or simply have a 3 variables if you prefer that, as an example we will call these variables no1, no2 and no3.
    When you generate the random number on the enemy, if it is equal to 1 then add 1 to the no1 variable, do the same with no2 and no3. By doing this, the no1 variable will be equal to the number of enemies that have the randomly generated number 1, and the same with the other variables.
    Then if an enemy is touched you can remove 1 from this variable, then when the variable is equal to 0 you know there is no enemies left with that number.

  • HopscotchHopscotch Member, PRO Posts: 2,782
    edited May 2015

    @KillerPenguinStudios said:
    Was wondering if there was a simple line of code that I could basically say, if main character random number is not equal to table rows 1 through 35 then to change its random number. Or is it more complicated than that?

    Lucily there is!

    Use the tableSearch function on your enemy table.

    a. It is fast
    b. It returns the row that matches the condition, if no matches are found it returns a 0.

    Change Attribute self.AtRowFound = tableSearch(game.T_Enemies,self.CurrentNumber,"col",1,1,tableRowCount(game.T_Enemies),"exact")
    Rule If self.AtRowFound = 0 then
              Change Attribute self.CurrentNumber = new random number
    
  • KillerPenguinStudiosKillerPenguinStudios Member Posts: 1,291

    @Hopscotch said:
    Change Attribute self.AtRowFound = tableSearch(game.T_Enemies,self.CurrentNumber,"col",1,1,tableRowCount(game.T_Enemies),"exact")
    Rule If self.AtRowFound = 0 then
    Change Attribute self.CurrentNumber = new random number

    In my enemies and main character for the random number I have it choosing between 0 and 3. I am assuming I will now need it to choose between 1 and 4 since the main characters self.AtRowFound will change if it equals 0. So if say the main characters random number between 0 and 3 chooses 0 as it's number it will change because it detects the enemies random 0 number as it being no condition returning 0.

    Is that correct? And thank you for that. Totally forgot about table search and glanced right over it.

  • HopscotchHopscotch Member, PRO Posts: 2,782
    edited May 2015

    @KillerPenguinStudios , the number returned by the tableSearch function is the row number, not the contents.

    Assuming that all rows that you check are filled by the random number of the enemies, then the above will work.

    You could change the random range to 1 to 4 so as not to not confuse yourself and also make it more robust for cases where the table has more rows than enemies populating it.

Sign In or Register to comment.