delete an element

I don't think there is direct method for that in the engine script functions (there might be a way using free though). A workaround I tend to use in order to have control over the actual size of my array is to add a custom "size" key to it and modify its value as I want. The idea is to create a new layer of control logic on top of the actual array.

But another workaround that doesn't require that kind of new layer is to create a scripted remove function that will iterate through every element of your base array and create a copy of it minus the elements you don't want. It's easy to do with a true array (the ones with numbers as indexes) using a typical for loop, and more tricky with a hashtable array (the ones with strings as indexes).

Here's how you iterate through a hashtable array wiht script (this might interest other modders as I don't think it has been documented anywhere) :


Code:
	void data = array(3);
	set(data, "a", 11);
	set(data, "b", 22);
	set(data, "c", 33);

	reset(data); // This reset the iteration cursor to the first element

	
	do {
	
		log("\nKEY = " + key(data));
		log(" ; VALUE = " + value(data));
	
	
	} while(next(data));
 
Thanks Piccolo! This is a Dictionary Structure with hash tech  ;)
For Hitmantis:
to remove an array element you need to write own method:
1) free the element in "i" index if you need (if it is a pointer)
    ex. if (typeof(elem) == openborconstant("VT_PTR") ) free(elem);
2) set NULL() the value in "i" index
3) create a tmp copy array copying the old array without NULL() values (use a for loop)
4) free original array
5) associate tmp array (address) in the original array var (ex. array = tmp_array)

call this function void remove(void array, int index) that returns the new array address (return array var)
 
here:
Code:
void remove(void array, int index, int nofree_flag) {
    int i = 0, c = 0;
    void tmp_array;

    if ( typeof(get(array,index)) == openborconstant("VT_PTR") && (nofree_flag == NULL() || nofree_flag <= 0) ) free (get(array,index));
    set(array,index,NULL());

    for ( i = 0; i < size(array); ++i ) {
        void elem = get(array,i);

        if ( elem != NULL() ) set(tmp_array,c++,elem);
    }

    free(array);

    return tmp_array;
}

not tested
 
Back
Top Bottom