• All, Gmail is currently rejecting messages from my host. I have a ticket in process, but it may take some time to resolve. Until further notice, do NOT use Gmail for your accounts. You will be unable to receive confirmations and two factor messages to login.

help with array

msmalik681

OpenBOR Developer
Staff member
I need help to cycle through a array its quite simple when working with index numbers but is there a simple way to cycle through a array with named values in a generic for loop without knowing the string names ?
 
I tested it made a array with 1 named value and tried to get with index 0 and 1 both were null  values
 
You can loop Literal arrays with foreach. In openbor script you have to use while + next() method. Read the manual
PS.. You can use for + size() and next() methods too
 
When I'm working with arrays in OpenBOR, it always helps me to think of them as doubly linked lists. That's not what they are, but it's more or less how they behave. Keep that in mind and it will help you conceptualize things easier.

DC
 
here some news:

OpenBOR v3.0 Build 4420 (Windows/Wii/Android)
improving arrays:
- some improvement to add()

islast(array)
~in literal arrays, it returns 1 if current pointer is on last element of literal array, otherwise it returns 0

isfirst(array)
~in literal arrays, it returns 1 if current pointer is on first element of literal array, otherwise it returns 0


previous(array)
~decrement the pointer from current literal position to previous position

here complete guide:
http://dcemulation.org/?title=OpenBORManual#Arrays
 
thanks for the support WD but ran this test in a updated script and engine shut down without log


Code:
void main()
{
int c;
 if(c==NULL())
 {
 setglobalvar("test",array(3));
 void x = getglobalvar("test");
 set(x,"0","zero");
 set(x,"1","one");
 set(x,"2","two");
 c=1;
 }
	if(isfirst(x)==0)
	{
	previous(x);
	}else if(isfirst(x)==1)
	{
	settextobj(0, 35, 30, 3, 9999999999, value(x));
	}
}


but with the below code I get a textbox with the value "two"


Code:
void main()
{


 setglobalvar("test",array(3));
 void x = getglobalvar("test");
 set(x,"0","zero");
 set(x,"1","one");
 set(x,"2","two");
	settextobj(0, 35, 30, 3, 9999999999, value(x));


}
 
Yes, it's right.
For you second script you get the last value because when you set the last value, array pointer IS ON last value.
If you want to start from the first value just use reset(array); to reset the pointer.

For the first I see many issues.
Here right version:
Code:
void x;
if( getglobalvar("test") == NULL() )
{
    setglobalvar("test",array(3));
    x = getglobalvar("test");
    set(x,"0","zero");
    set(x,"1","one");
    set(x,"2","two");
}

x = getglobalvar("test");
while(!isfirst(x)) previous(x); // or reset(x);
if(isfirst(x))
{
    settextobj(0, 35, 30, 3, 9999999999, value(x));
}
free(getglobalvar("test"));
setglobalvar("test",NULL());

The engine crashes because your var X declaration is wrong.
1) Declare x out of block if you want to us it out too..
2) Dont forget free at the end and nullify the global variable.
3) if you want to move back with pointer, it is not enough if (!isfirst(x)) previous(x);. Instead use while while(!isfirst(x)) previous(x); or better use reset(array);
 
I want to use my array over save games so at no point i would free or null the globalvar.  The engine dumps any non freed array or images so why should i free them ?
 
msmalik681 said:
...The engine dumps any non freed array or images so why should i free them ?

Because while the engine is running, you are wasting resources - not only for OpenBOR, but other applications too. It could mean the difference between being port compatible or not.

Barring that, it's just bad practice. No matter what kind of coding you are doing, leaving resources allocated when you aren't using them any more is begging for trouble. It's almost guaranteed to cause huge memory leaks, crashes, and other sorts of bugs that can be nearly impossible to trace.

DC
 
msmalik681 said:
I want to use my array over save games so at no point i would free or null the globalvar.  The engine dumps any non freed array or images so why should i free them ?

You need to track all your allocated data if you don't want to find bugs or issues or crashes like DC wrote.
See you log and discover your not free data.
There are many points to free a data.
Store it in a globalvar and then free it where you want. If you need to free it at shutdown of engine,.use ondestroy()  event in updated
 
Ok so i noticed you used a free command and then set it to null.  So when you free a array does it still hold the data ?
 
msmalik681 said:
Ok so i noticed you used a free command and then set it to null.  So when you free a array does it still hold the data ?
not array but globalvar can retrieve another value.
Here an image that explain what I wanto to say (invite all to see this image).

index.php


SCENARIO 1) allocate array of size = 2 that setglobalvar: ex.: setglobalvar("test",array(2)). set( getglobalvar("test"),0,1 ); set( getglobalvar("test"),1,2 );
SCENARIO 2) free(getglobalvar("test")); How you can see array is free but the values are in memory yet, but no array pointer to data. just globalvar pointer.
SCENARIO 3) Another data is allocated in memory. Now address 0001005 is free and I can allocate memory yet. PAY ATTENTION: ANYONE can allocate memory there!!
                        Openbor can allocate memory into 0x0001005  or any WINDOWS program or ANYONE!!
                        So if you don't nullify your globalvar after the array free, you can get some issue (sure a bug!!).
                        So you need to setglobalvar("test",NULL()); to complete your free action!!

[attachment deleted by admin]
 
Arrays work on a low level and sound dangerous so i might just stick to globalvars for any long holding data.

I had a idea if i want to hold data for 5 items data could be 1,2,3,4 or 0 if i made a globalvar with the value  00000 then item 4 is on it would change to 00010 is there a mathematical way to do this? Or i might try the same thing as a string using the new string fuctions like strleft to munipulate the data.

Sorry for all the questions.
 
"if i made a globalvar with the value  00000 then item 4 is on it would change to 00010 is there a mathematical way to do this? "

Yes, using bitwise operators.
 
msmalik681 said:

The question is why? Bitwise math WAY over complicates a matter of simple variable storage and it's limited to scalar values. Just null and free when you're finished - the end. There's nothing dangerous about it.

DC
 
Damon Caskey said:
msmalik681 said:

The question is why? Bitwise math WAY over complicates a matter of simple variable storage and it's limited to scalar values. Just null and free when you're finished - the end. There's nothing dangerous about it.

DC

reading up on bitwise gave me a headache so i am sticking with arrays i am declaring all my arrays with a oncreate script then freeing them with a ondestroy script.

just to check if the engine crashes then is the array stuck in memory ?
 
msmalik681 said:

No. First, when the engine catches an error and shuts down, that's not a crash. At all. It's doing exactly what it was designed to do - catch an error and shut down so there won't be a crash. The engine goes through all of its normal shutdown routines, including garbage cleanup.

In the very rare case of an actual crash, your OS destroys the offending application and all of its memory allocations. If it doesn't, then you're probably running a highly modified console or Linux installation with custom exception routines - in which case you know more about low level memory management than I do. :)

IOW, no - the vars are never left over.

DC
 
thanks for the insight DC but now I am having another issue with array being uninitialized but only after I return to menu and start a new game or load a save game.  The engine shuts down with array uninitialized error in the log.

I was wondering if I added a ondestroy script in my updated.c to clear all array,images and globalvars will this activate when I return to main menu ? I recently changed my oncreate script from my updated.c to a if statement in main to run if any of my main arrays or images are not valid and it only if in title screen.


my issue was with keyscripts always being active now I wrapped them in a in_level if statement and no more shutdowns.
 
sorry for the double post but i just tested a array in a globalvar and it will not carry over save games so it cant help me for saving multiple items they must be saved in individual globalvars.
 
Back
Top Bottom