O Ilusionista
Captain 100K
Guys, I want to learn how better handle arrays in OpenBOR. I already understand how an array works in other languages, like Javascript, ActionScript, etc.
What I want to do:
I have a code for random itens, where I assign an item container for a killed enemy, which will spawn an random item. Right now, the code uses a random seed and, for each value, I use a SWITCH to spawn the item:
But I feel this could be A LOT better using an array. So, what I want to do:
1) set an array with a list of itens, kinda like:
2) Get the array lenght to be used as a seed:
3) check the array index value. This is where I am lost. And don't think I will need a SWITCH for that.
- How I can get the array index? Or should I just get the "r" value above?
So I can use it for a spawn:
Is my logic correct? How is the right synthax to make this in OpenBOR scripting?
What I want to do:
I have a code for random itens, where I assign an item container for a killed enemy, which will spawn an random item. Right now, the code uses a random seed and, for each value, I use a SWITCH to spawn the item:
Code:
void spawnRandomItem(int vx, int vy, int vz)
// Spawn random item
// 22.02.2017 - Douglas Baldan - O Ilusionista
// Needs Damon Caskey "spawn01" function to make it works.
// vx = x position
// vy = y position
// vz = z position
{
int r = rand()%5; // Choose between 6 values (0 to 5)
void vName;
if (r <0){ // If the value is negative,
r = r*-1; // Make it positive
}
switch(r) { // check the item name
case "0" : // item 0, for example, "box"
clearspawnentry();
vName = "box";
spawn01(vName,vx,vy,vz);
break;
case "1" : // item 1, for example, "box2"
clearspawnentry();
vName = "box2";
spawn01(vName,vx,vy,vz);
break;
case "2" : // item 2, for example, "crate"
clearspawnentry();
vName = "crate";
spawn01(vName,vx,vy,vz);
break;
case "3" : // item 3, for example, "drum"
clearspawnentry();
vName = "drum";
spawn01(vName,vx,vy,vz);
break;
case "4" : // item 4, for example, "tnt"
clearspawnentry();
vName = "tnt";
spawn01(vName,vx,vy,vz);
break;
case "5" : // item 5, for example, "apple"
clearspawnentry();
vName = "apple";
spawn01(vName,vx,vy,vz);
break;
default : // in case of none of above
break;
}
}
But I feel this could be A LOT better using an array. So, what I want to do:
1) set an array with a list of itens, kinda like:
Code:
var itemList = ["box", "drum", "tnt", "apple", "create","heart"];
2) Get the array lenght to be used as a seed:
Code:
int itemListSize = size(itemList);
int r = rand()%itemListSize;
if (r <0){ // If the value is negative,
r = r*-1; // Make it positive
}
3) check the array index value. This is where I am lost. And don't think I will need a SWITCH for that.
Code:
char ItemName = get(itemList,index);
So I can use it for a spawn:
Code:
vName = ItemName;
spawn01(vName,vx,vy,vz);
Is my logic correct? How is the right synthax to make this in OpenBOR scripting?