About Arrays in OpenBOR

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:
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);
- How I can get the array index? Or should I just get the "r" value above?
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?
 
yes arrays would clean up your code nicely !

Code:
//set your array
setglobalvar("itemList",array(6));
void items = getglobalvar("itemList");
set(items,0,"box");
set(items,1,"drum");
set(items,2,"tnt");
set(items,3,"apple");
set(items,4,"create");
set(items,5,"heart");

//call your array
get(items,r);

hope that helps !
 
ah, so I need to set each item on a separated line, not like in Javascript?
And why I would need a globalvar for that? A local var would do the job.
 
sure a local var should be fine it was just a example and remember to fee it later !


I love the way you made that random code positive but your random code seems long winded I made this simple one !


Code:
int random = (openborvariant("elapsed_time")/2)%6; //random number from 0 to 5


it gets the job done for me.
 
If I am not wrong, both does the same thing. The difference is that your version doesn't use the internal rand() function (and it seeds) to generate the number.
I need to test, but maybe your version doens't suffer from the C-based issue about random (random, in C, is not THAT random, if you doesn~t randomize its seeds too).

In your array explanation, there is missing the array.size, which will make me not use a integer (rand()%5 for example)
 
O Ilusionista said:
In your array explanation, there is missing the array.size, which will make me not use a integer (rand()%5 for example)

sorry but I do not follow what you mean here can you clarify please !
 
Here:

Code:
int itemListSize = size(itemList);

With this, I can get the array size, so I can use this value for my random code. For example, if the array has 5 values, it will use 5 as the number to get vartions. If its 10, 10 variations and so on.

Instead of writing:
Code:
int r = rand()%5; // Choose between 6 values (0 to 5)
(which makes me change this code manually, based on how much indexes the array has)

I could simply write
Code:
int itemListSize = size(itemList); // get the array size index
int r = rand()%itemListSize;

Got it?
 
Hey DC/White Dragon, can we set a list of array values in one line like in Javascript/php or we have to set one by one?
 
O Ilusionista said:
Hey DC/White Dragon, can we set a list of array values in one line like in Javascript/php or we have to set one by one?

For the time being, one by one - in general OpenBOR script requires a static number of arguments for a given function call.

DC
 
Back
Top Bottom