|
| 22 Nov 2014 11:07 AM |
I would be posting this in SH, but nobody there knows java script.
anyways, this is supposed to choose a random item from a table based off of its weight (the number).
I'm currently getting the error
"assignment to undeclared variable Miner"
btw the math.RandomInt is a function from a module script outside. It works.
Sorry if you see this as spam, I just started Tuesday and need some help.
var math = require("MathHandler"); // math.random example V //console.log(math.RandomInt(1,5));
var chance = [ Miner = 20, "Miner", Defender = 15, "Miner", Healer = 15, "Miner", Outer_Defender = 7,"Miner", Builder = 13, "Miner", Repairman = 10, "Miner", Raider = 20 ,"Miner",
]; var NewChance = []; var counter = 0; for (var i = 0; i < chance.length+1; i++) { for (var k = 0; i == chance[i][0]; k++){ NewChance[counter] = chance[i]; counter=counter+1;
} }
function select() { return NewChance[math.RandomInt(1,NewChance.length)][1];
}
console.log(select());
|
|
|
| Report Abuse |
|
|
digpoe
|
  |
| Joined: 02 Nov 2008 |
| Total Posts: 9092 |
|
|
| 22 Nov 2014 11:08 AM |
you would be posting this in a non-existant forum but nobody in the non-existant forum knows JS?
gg |
|
|
| Report Abuse |
|
|
|
| 22 Nov 2014 11:09 AM |
oh yeah. Whoops. haven't been online in a while.
I would post this in GD, but..
yeah you know. |
|
|
| Report Abuse |
|
|
digpoe
|
  |
| Joined: 02 Nov 2008 |
| Total Posts: 9092 |
|
| |
|
Argelius
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 4047 |
|
|
| 22 Nov 2014 11:11 AM |
night omg that was close
you cant let anyone know that sh is hidden now |
|
|
| Report Abuse |
|
|
anaIyze
|
  |
| Joined: 29 May 2014 |
| Total Posts: 2048 |
|
| |
|
|
| 22 Nov 2014 11:16 AM |
Its an RTS game where you have to program the system to make your units defend/attack/whatever.
Called screeps.
|
|
|
| Report Abuse |
|
|
anaIyze
|
  |
| Joined: 29 May 2014 |
| Total Posts: 2048 |
|
|
| 22 Nov 2014 11:18 AM |
| well when you put just "Miner" in the table of course its going to error with nil val/var |
|
|
| Report Abuse |
|
|
|
| 22 Nov 2014 11:19 AM |
crap. Your right.
I must be tired. |
|
|
| Report Abuse |
|
|
|
| 22 Nov 2014 11:25 AM |
alright, this is my table now but it still is throwing the same error as before.
var chance = [ Miner = [20, "Miner"], Defender = [15, "Defender"], Healer = [15, "Healer"], Outer_Defender = [7,"Outer_Defender"], Builder = [13, "Builder"], Repairman = [10, "Repairman"], Raider = [20 ,"Raider"],
]; |
|
|
| Report Abuse |
|
|
| |
|
|
| 22 Nov 2014 11:42 AM |
var chance = []; chance["Miner"] = [20, "Miner"]; chance["Defender"] = [15, "Miner"]; chance["Healer"] = [15, "Miner"]; chance["Outer_Defender"] = [7,"Miner"]; chance["Builder"] = [13, "Miner"]; chance["Repairman"] = [10, "Miner"]; chance["Raider"] = [20 ,"Miner"];
console.log(chance["Miner"]); -- [20, "Miner"] |
|
|
| Report Abuse |
|
|
|
| 22 Nov 2014 11:59 AM |
Thanks! I just had to fix up a few things, but I ran into another problem.
error = "NewChance[math.RandomInt(...)] is undefined"
var math = require("MathHandler"); // math.random example V //console.log(math.RandomInt(1,5));
var chance = []; chance[0] = [20, "Miner"]; chance[1] = [15, "Miner"]; chance[2] = [15, "Miner"]; chance[3] = [7,"Miner"]; chance[4] = [13, "Miner"]; chance[5] = [10, "Miner"]; chance[6] = [20 ,"Miner"]; var NewChance = []; var counter = 0; for (var i = 0; i == chance.length; i++) { for (var k = 0; i == chance[i][0]; k++){ NewChance[counter] = chance[k]; counter=counter+1;
} }
function select() { return NewChance[math.RandomInt(1,NewChance.length)][1];
}
console.log(select());
|
|
|
| Report Abuse |
|
|
|
| 22 Nov 2014 12:20 PM |
var chance = [ [20, "Miner"] , [15, "Miner"] , [15, "Miner"] , [7,"Miner"] , [13, "Miner"] , [10, "Miner"] , [20 ,"Miner"] ];
var NewChance = []; var counter = 0; for (var i = 0; i < chance.length; i++) { for (var k = 0; k < chance[i][0]; k++) { NewChance[counter] = chance[i]; ++counter; } }
// Returns a random integer between min (included) and max (excluded) // Using Math.round() will give you a non-uniform distribution! function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; }
function select() { return NewChance[getRandomInt(1,NewChance.length)][1]; }
console.log(select()); |
|
|
| Report Abuse |
|
|
| |
|