- Home /
How to use unlimited parameters in a function? (JS)
Hi! For my game i'm making a space colony game, were you can build buldings, so i'm making a function that builds the buildings. But I would like to make it so it can have an unlimited amount of integer values put in. These would be the resources required to build the building, and i would check with a for loop if you have enough and if you do you can build the building. But how to I do this? I know in java you would use ... but how do you do this in javascript?
Answer by soft_sound · Jan 03, 2014 at 07:15 AM
This seems vague to me, this would be pretty similar to regular Java or javascript concept wise.
Hmmm... Well, this might give you some ideas.
function Start ()
{
//call function & check if it returns true
if (checkResources(3, 5, 2, 8) == true)
Debug.Log("Building");
}
//setup function...
function checkResources(wood, woodAmountNeeded, metal, metalAmountNeeded) {
if(wood == woodAmountNeeded && metal == metalAmountNeeded)
{
Debug.Log("You have enough wood & Metal");
return true;
}
else
{
Debug.Log("not enough supplies");
return false;
}
}
I'm not sure why you need a for loop for this, nor why you would need unlimited integers or what that even means...Can't you just add parameters and arguments as you need them? Also sorry, I normally write in C# so, my unityscript is rather on the sorry side.
If I understand the question correctly; the only easy way I know is to use an array or list. That said I would go with how you did it.
#pragma strict
import System.Collections.Generic;
var someNumbers = new List.<int>();
function Start () {
for(var cntr = 0; cntr < 50; cntr++)
someNumbers.Add(cntr);
LoopListFunction(someNumbers);
}
function LoopListFunction(myList){
if(typeof(myList) == List.<int>){
for(var value : int in myList){
Debug.Log(value);
}
} else {
throw "Type Error";
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613125420im_/https://answers.unity.com/themes/thub/images/avi.jpg)