Skip to main content

News

Topic: Newb Question, Appropriate Terminology (Read 7738 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Newb Question, Appropriate Terminology
Reply #15
Ok, I really don't know why you have to do all that code to do something so simple. Here's what I see.

You have a large global array of enemies:
Code: (javascript) [Select]

var enemies = [];
enemies[0] = new Enemy("enemy1");
enemies[1] = new Enemy("enemy2");
enemies[2] = new Enemy("enemy3");
enemies[3] = new Enemy("enemy4");


Then, in your battle system you say, come across 4 random enemies? We don't need Knuth shuffle for that.

Code: (javascript) [Select]

var team = new EnemyTeam(GetRandom(enemies), GetRandom(enemies), GetRandom(enemies), GetRandom(enemies));


Where GetRandom is simply, this (what Jest had):
Code: (javascript) [Select]

function GetRandom(array) {
    return array[Math.floor(Math.random() * array.length)];
}


The Knuth Shuffle is primarily used to shuffle a sorted list. It's more useful for something like this:
Code: (javascript) [Select]

var enemy_team = [enemies[0], enemies[0], enemies[1], enemies[2]];
RandomizeArray(enemy_team);
var team = new EnemyTeam(enemy_team[0], enemy_team[1], enemy_team[2], enemy_team[3]);


The use is slightly different, and in this case the Knuth shuffle is really not all that needed. I hope that clarifies somethings.
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: Newb Question, Appropriate Terminology
Reply #16
Most things are cleared up, I'm just confused on other stuff now.

Code: [Select]
 var team = new EnemyTeam(GetRandom(enemies), GetRandom(enemies), GetRandom(enemies), GetRandom(enemies)); 


I'm not understanding how I can still reference those elements of EnemyTeam individually.
I need to be able to still reference the Enemies individually so that I can use them in the battle system.

Will I be able to use array.length somehow to obtain their index?

I'm using the script like this:
Code: [Select]
 
var enemies = [];  // <---  Enemy Indexes
enemies[0] = 0;
enemies[1] = 52;
enemies[2] = 11;
enemies[3] = 230;

var team[0] = new EnemysTeam(GetRandom(enemies), GetRandom(enemies), GetRandom(enemies), GetRandom(enemies));

function GetRandom(array) { return array[Math.floor(Math.random() * array.length)]; }


Then in the battle system I'm referencing the enemies like so:

Code: [Select]
 team[ET].E1; 


Would it still grab the index, which is now randomized, through the variable E1?
Or is variable E1 now useless?

Basically, E1 is now E1 = GetRandom(enemies);
I'm not sure if the battle system will still retrieve an index from that. Will it?
  • Last Edit: February 17, 2014, 10:42:58 pm by Vakinox

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Newb Question, Appropriate Terminology
Reply #17

I'm not understanding how I can still reference those elements of EnemyTeam individually.
I need to be able to still reference the Enemies individually so that I can use them in the battle system.


Oh, that's in GetRandom:

Code: (javascript) [Select]

function GetRandom(array) { return array[Math.floor(Math.random() * array.length)]; }


Notice the [ ] after "array" It might be a lot of stuff packed into that line but it's all a giant indexer:
Code: (javascript) [Select]

function GetRandom(array) {
    var index = Math.floor(Math.random() * array.length)
    return array[index];
}


It will return the random element, so you don't have to do anything weird with the output. It;'ll return say 52 or 11 from the original array. It'll do just that: return a random element.
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here