Bruce
Active member
Hello everyone,
I've posted my question regarding to this issue in another topic, but I think it is better for me to create a topic and post all my code.
I started learning how to add backgrounds and animations with arraylist to the title, menu, and character selection screens.
If I just start the game normally, there is no issue.
If I just loop the title, menu, then character selection screens back and forth for a while (40-50 times),
it would cause out of memory. I know that no one would be crazy enough to loop the screens back and forth like this,
but still it should not be out of memory if it was coded correctly.
Furthermore, if I start the game and then quit the game, and repeat the loop above, it causes out of memory alot quicker.
Also, if I don't comment out these 2 functions in the title screen
and
it causes out of memory alot quicker. It seems like it adds more stuff to the memory instead of removing them when I start looping back and forth , but why?????
Obviously, I didn't code it correctly....
Please any help, advises, comments to help me with better coding/route/understanding, would be greatly appreciated
Video link: Out of memory at the end of the video
Notice: this is just a tryout...
updated.c
ScreenAnim.c:
SelectAnim1.txt
Thank you very much
I've posted my question regarding to this issue in another topic, but I think it is better for me to create a topic and post all my code.
I started learning how to add backgrounds and animations with arraylist to the title, menu, and character selection screens.
If I just start the game normally, there is no issue.
If I just loop the title, menu, then character selection screens back and forth for a while (40-50 times),
it would cause out of memory. I know that no one would be crazy enough to loop the screens back and forth like this,
but still it should not be out of memory if it was coded correctly.
Furthermore, if I start the game and then quit the game, and repeat the loop above, it causes out of memory alot quicker.
Also, if I don't comment out these 2 functions in the title screen
Code:
ClearGlobalArrays();
SetupArrayLists();
Code:
ClearGlobalArrays() in inLevelLoop(),
Obviously, I didn't code it correctly....
Please any help, advises, comments to help me with better coding/route/understanding, would be greatly appreciated
Video link: Out of memory at the end of the video
Notice: this is just a tryout...
updated.c
Code:
#include "data/scripts/common/constants.h"
#include "data/scripts/story/story_clear.c"
#include "data/scripts/story.c"
#include "data/scripts/gettick.c"
#include "data/scenes/ScreenAnim.c"
#define BG_TITLE1 0
#define BG_TITLE2 1
#define BG_MENU1 2
#define BG_MENU2 3
#define BG_SELECT1 4
#define BG_BLK 5
// ANIM_TITLE0 0 // NOT USED
#define ANIM_TITLE1 1
#define ANIM_TITLE2 2
#define ANIM_TITLE3 3
#define ANIM_TITLE4 4
#define ANIM_SELECT1 5
#define ANIM_SELECT2 6
void oncreate(){
InitializeVariables();
}
void ondestroy(){
log("LOG: ON_DESTROY \n");
ClearGlobalArrays();
clearStory();
clearglobalvar();
clearlocalvar();
}
void main()
{
if(openborvariant("current_scene")=="data/levels/scenes/credits/credits.txt")
{
clearStory();
InitializeVariables();
}
mainLoop();
if(openborvariant("in_selectscreen")){
int TitleCounter = getlocalvar("TitleCounter");
if(TitleCounter > 0){
/* ClearGlobalArrays(); //these causing the out of memory much quicker!
SetupArrayLists(); */
setlocalvar("TitleCounter", 0);
void TitleAnimList = getglobalvar("List_TitleAnim");
if(TitleAnimList != NULL())
{ // GlobalVarIndex, Entity, XYZ
SpawnScreenAnim(ANIM_SELECT1, "SelectAnim1", 961, 541, -2000);
SpawnScreenAnim(ANIM_SELECT2, "SelectAnim2", 800, 500, 1);
}
}
void BG_List = getglobalvar("List_TitleBG");
void SelectBG = get(BG_List, BG_SELECT1);
if(SelectBG != NULL()){
changedrawmethod(NULL(), "alpha", 0);
drawsprite(SelectBG, 0, 0, 0);
}
}
}
void mainLoop()
{
if(openborvariant("in_level")){
inLevelLoop();
}
else
{
if(openborvariant("current_scene") == "data/scenes/intro.txt"){
int TitleCounter = getlocalvar("TitleCounter");
if(TitleCounter == NULL() || TitleCounter > 0){
setlocalvar("TitleCounter", 0);
}
}
else if(openborvariant("in_titlescreen")){
int TitleCounter = getlocalvar("TitleCounter");
if(TitleCounter != 1){
clearStory();
// InitializeVariables();
playwebm("data/scenes/Press_Start_Screen.webm", 0); // 0 = skippable
void TitleAnimList = getglobalvar("List_TitleAnim");
if(TitleAnimList != NULL())
{
void SelectAnim1 = get(TitleAnimList, ANIM_SELECT1);
if(SelectAnim1 != NULL()){
killentity(SelectAnim1);
}
void SelectAnim2 = get(TitleAnimList, ANIM_SELECT2);
if(SelectAnim2 != NULL()){
killentity(SelectAnim2);
}
}
if(TitleAnimList != NULL())
{ // GlobalVarIndex, Entity, XYZ
SpawnScreenAnim(ANIM_TITLE1, "TitleAnim1", 500, 500, -1);
SpawnScreenAnim(ANIM_TITLE2, "TitleAnim2", 800, 500, -1);
}
setlocalvar("TitleCounter", 1);
playmusic("data/music/Title.ogg", 1);
}
drawstring(20,20,0, "TITLE");
void BG_List = getglobalvar("List_TitleBG");
void TitleBG = get(BG_List, BG_TITLE1); // see SetupArrayLists() for details
if(TitleBG != NULL()){
changedrawmethod(NULL(), "alpha", 0);
drawsprite(TitleBG, 0, 0, -2);
}
void BlackMask = get(BG_List, BG_BLK);
if(BlackMask != NULL()){
changedrawmethod(NULL(), "alpha", 6); // 50% transparency
drawsprite(BlackMask, 0, 0, -1);
}
}
else if(openborvariant("in_menuscreen")){
drawstring(20,20,0, "MENU");
int TitleCounter = getlocalvar("TitleCounter");
if(TitleCounter != 2)
{
setlocalvar("TitleCounter", 2);
playmusic("data/music/Menu.ogg", 1);
void TitleAnimList = getglobalvar("List_TitleAnim");
if(TitleAnimList != NULL())
{
void TitleAnim1 = get(TitleAnimList, ANIM_TITLE1);
if(TitleAnim1 != NULL()){
killentity(TitleAnim1);
}
void TitleAnim2 = get(TitleAnimList, ANIM_TITLE2);
if(TitleAnim2 != NULL()){
killentity(TitleAnim2);
}
}
}
void BG_List = getglobalvar("List_TitleBG");
void MenuBG = get(BG_List, BG_MENU1);
if(MenuBG != NULL()){
changedrawmethod(NULL(), "alpha", 0);
drawsprite(MenuBG, 0, 0, -1);
}
void BlackMask = get(BG_List, BG_BLK);
if(BlackMask != NULL()){
changedrawmethod(NULL(), "alpha", 6); // 50% transparency
drawsprite(BlackMask, 0, 0, -1);
}
}
}
}
void InitializeVariables()
{
setglobalvar("LevelFirstStart", 1);
SetupArrayLists();
}
void inLevelLoop()
{
turnWhite();
storySystem();
int LevelFirstStart = getglobalvar("LevelFirstStart");
if(LevelFirstStart > 0)
{
setglobalvar("LevelFirstStart", 0);
// ClearGlobalArrays();
}
}
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, BG_TITLE1, loadsprite("data/scenes/TitleScreen.png"));
set(TitleBG_List, BG_TITLE2, NULL());
set(TitleBG_List, BG_MENU1, loadsprite("data/scenes/MenuScreen.png"));
set(TitleBG_List, BG_MENU2, NULL());
set(TitleBG_List, BG_SELECT1, loadsprite("data/bgs/select.png"));
set(TitleBG_List, BG_BLK, loadsprite("data/scenes/black.gif"));
setglobalvar("List_TitleBG", TitleBG_List);
}
void AnimList = getglobalvar("List_TitleAnim");
if(!AnimList || AnimList == NULL())
{
AnimList = array(3); //array(3) means total of 4 items
set(AnimList, 0, NULL()); // NOT USED
set(AnimList, ANIM_TITLE1, NULL()); // Title Animation 1
set(AnimList, ANIM_TITLE2, NULL());
set(AnimList, ANIM_TITLE3, NULL());
set(AnimList, ANIM_TITLE4, NULL());
set(AnimList, ANIM_SELECT1, NULL());
set(AnimList, ANIM_SELECT2, NULL());
setglobalvar("List_TitleAnim", AnimList);
}
}
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);
setglobalvar("List_TitleBG", NULL());
}
void Anim_List = getglobalvar("List_TitleAnim");
if(Anim_List)
{
for(i=0; i<size(Anim_List); i++)
{
set(Anim_List, i, NULL());
}
free(Anim_List);
setglobalvar("List_TitleAnim", NULL());
}
}
ScreenAnim.c:
Code:
void SpawnScreenAnim(int ArrayIndex, void AnimatedEntity, int X, int Y, int Z)
{
void vSpawn;
void subent;
void AnimList = getglobalvar("List_TitleAnim");
if(AnimList != NULL())
{
void TitleAnim = get(AnimList, ArrayIndex);
if(TitleAnim != NULL()){
killentity(TitleAnim); //Kill previous spawned enity to avoid duplicated entities
}
}
loadmodel(AnimatedEntity); // name of the entity to be loaded
clearspawnentry(); // clean the spawn entry
setspawnentry("name", AnimatedEntity); // define the entity to be spawn
setspawnentry("coords", X, Y, Z); // set the position of the entity
subent=spawn(); // spawn the entity
if(AnimList != NULL())
{
set(AnimList, ArrayIndex, subent);
setglobalvar("List_TitleAnim", AnimList);
}
changeentityproperty(subent, "position", X, Y, Z);//for safe, set again the position
}
SelectAnim1.txt
Code:
name SelectAnim1
health 1
type panel
speed 10
shadow 0
setlayer -2000
facing 1
anim idle
loop 1 1
offset 961 541
delay 15
@cmd changeentityproperty getlocalvar("self") "direction" 1
frame data/scenes/SelectAnim/SelectAnim01.png
@cmd changeentityproperty getlocalvar("self") "setlayer" -2000
frame data/scenes/SelectAnim/SelectAnim02.png
frame data/scenes/SelectAnim/SelectAnim03.png
frame data/scenes/SelectAnim/SelectAnim04.png
frame data/scenes/SelectAnim/SelectAnim05.png
frame data/scenes/SelectAnim/SelectAnim06.png
frame data/scenes/SelectAnim/SelectAnim07.png
frame data/scenes/SelectAnim/SelectAnim08.png
frame data/scenes/SelectAnim/SelectAnim09.png
frame data/scenes/SelectAnim/SelectAnim10.png
frame data/scenes/SelectAnim/SelectAnim11.png
frame data/scenes/SelectAnim/SelectAnim12.png
frame data/scenes/SelectAnim/SelectAnim13.png
frame data/scenes/SelectAnim/SelectAnim14.png
frame data/scenes/SelectAnim/SelectAnim15.png
frame data/scenes/SelectAnim/SelectAnim16.png
frame data/scenes/SelectAnim/SelectAnim17.png
frame data/scenes/SelectAnim/SelectAnim18.png
frame data/scenes/SelectAnim/SelectAnim19.png
frame data/scenes/SelectAnim/SelectAnim20.png
frame data/scenes/SelectAnim/SelectAnim21.png
frame data/scenes/SelectAnim/SelectAnim22.png
frame data/scenes/SelectAnim/SelectAnim23.png
frame data/scenes/SelectAnim/SelectAnim24.png
frame data/scenes/SelectAnim/SelectAnim25.png
frame data/scenes/SelectAnim/SelectAnim26.png
frame data/scenes/SelectAnim/SelectAnim27.png
frame data/scenes/SelectAnim/SelectAnim28.png
frame data/scenes/SelectAnim/SelectAnim29.png
frame data/scenes/SelectAnim/SelectAnim30.png
Thank you very much