Bruce
Active member
I am learning about array right now, and I seem to have difficulties with it.not array but globalvar can retrieve another value.
Here an image that explain what I wanto to say (invite all to see this image).
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]
I have the follow code in the updated.c:
Code:
void oncreate(){
setglobalvar("LevelFirstStart", 1);
SetupArrayLists();
}
if(SYS_In_Level)
{
int LevelFirstStart = getglobalvar("LevelFirstStart");
if(LevelFirstStart > 0)
{
setglobalvar("LevelFirstStart", 0);
ClearGlobalArrays();
}
}
if(SYS_Current_Scene=="data/levels/scenes/credits/credits.txt")
{
setglobalvar("LevelFirstStart", 1);
SetupArrayLists();
}
void SetupArrayLists()
{
void TitleBG_List = getglobalvar("List_TitleBG");
if(!TitleBG_List || TitleBG_List == NULL())
{
TitleBG_List = array(2); //array(2) means total of 3 items
set(TitleBG_List, 0, loadsprite("data/scenes/TitleScreen.png"));
set(TitleBG_List, 1, loadsprite("data/scenes/MenuScreen.png"));
set(TitleBG_List, 2, loadsprite("data/scenes/black.gif"));
setglobalvar("List_TitleBG", TitleBG_List);
}
}
void ClearGlobalArrays()
{
int i;
void BG_List = getglobalvar("List_TitleBG");
if(BG_List)
{
for(i=0; i<size(BG_List); i++)
{
set(BG_List, i, NULL());
}
free(BG_List); //remove all the sprites off the array list
setglobalvar("List_TitleBG", NULL());
}
}
I only use these sprites for title and menu screens. Meaning that I want to complete remove them of the memory after the game is started (in the level).
Then if the player quits or completes the game, the credit screen will display and it would re-initialize the SetupArrayLists();
For some reasons, after I quit the game, the sprites won't show up with the error code xxxx uninitialized.
But if I remove the 2 lines in the ClearGlobalArrays() function below
Code:
free(BG_List);
setglobalvar("List_TitleBG", NULL());
The sprites would show up after I quit the game.
Can someone please explain and help me understand how the array setup works and why I am having errors?
Thank you
Edited: I made some changes to the updated.c and now it is working with
Code:
free(BG_List); //remove all the sprites off the array list
setglobalvar("List_TitleBG", NULL());
hmm, how weird...
Last edited: