Jump to content
  • Advertisement
Sign in to follow this  
KKTHXBYE

cant write to new object in linkedlist

This topic is 956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So basically i have a struct TEntity

 

where

struct TEntity
{
int PID;
TEntity * next;

TEntity()
{
PID = 0;
next = NULL;
}

};

now when i add object i try to find last object in the linked list then, with that last object i set its next to the object that waits to be added +

new object PID is last_object PID + 1, but somehow i am unable to set that integer at all.

 

12-08 21:45:35.410: V/WNB_LOG(8285): INSERTING: /mnt/sdcard/com.example.wirednavalbattle/files/objects/dock_wood_long.objspec

12-08 21:45:35.410: V/WNB_LOG(8285): LOADING OBJECT: /mnt/sdcard/com.example.wirednavalbattle/files/objects/dock_wood_long.objspec
12-08 21:45:35.410: V/WNB_LOG(8285): LOADING OBJECT: /mnt/sdcard/com.example.wirednavalbattle/files/objects/dock_wood_long.objspec
12-08 21:45:35.420: V/WNB_LOG(8285): Loading model: /mnt/sdcard/com.example.wirednavalbattle/files/objects/buildings/docks/dock_wood_long.tachogl
12-08 21:45:35.430: V/WNB_LOG(8285): THIS IS FIRST_ENTITY pid: 0
12-08 21:45:36.670: V/WNB_LOG(8285): ------------------------------------------------------
12-08 21:45:39.130: V/WNB_LOG(8285): INSERTING: /mnt/sdcard/com.example.wirednavalbattle/files/objects/dock_wood_long.objspec
12-08 21:45:39.130: V/WNB_LOG(8285): LOADING OBJECT: /mnt/sdcard/com.example.wirednavalbattle/files/objects/dock_wood_long.objspec
12-08 21:45:39.130: V/WNB_LOG(8285): LAST OBJECT PID: 0
12-08 21:45:39.130: V/WNB_LOG(8285): NOT FIRST ENTITY: lastpid: 1  mypid: 1
12-08 21:45:40.680: V/WNB_LOG(8285): ------------------------------------------------------
12-08 21:45:43.420: V/WNB_LOG(8285): INSERTING: /mnt/sdcard/com.example.wirednavalbattle/files/objects/dock_wood_long.objspec
12-08 21:45:43.420: V/WNB_LOG(8285): LOADING OBJECT: /mnt/sdcard/com.example.wirednavalbattle/files/objects/dock_wood_long.objspec
12-08 21:45:43.420: V/WNB_LOG(8285): LAST OBJECT PID: 1
12-08 21:45:43.420: V/WNB_LOG(8285): NOT FIRST ENTITY: lastpid: 1  mypid: 1
12-08 21:51:29.080: V/WNB_LOG(8285):  ------------------------------------------------------
12-08 21:51:31.420: V/WNB_LOG(8285): INSERTING: /mnt/sdcard/com.example.wirednavalbattle/files/objects/dock_wood_long.objspec
12-08 21:51:31.420: V/WNB_LOG(8285): LOADING OBJECT: /mnt/sdcard/com.example.wirednavalbattle/files/objects/dock_wood_long.objspec
12-08 21:51:31.420: V/WNB_LOG(8285): LAST OBJECT PID: 1
12-08 21:51:31.420: V/WNB_LOG(8285): NOT FIRST ENTITY: lastpid: 1  mypid: 1
 
 

 

 

 

 

 

now:

 

theres a struct calleld TMission where i store TEntity * object; <- the first object in linked list

 

 

 

this finds last object in linked list.

TEntity * FindLastObject()
{
if (object == NULL) return NULL;


TEntity * p = object;
while (p->next != NULL)
p = p->next;


return p;
}

and this is the code responsible for adding new object


void AddObject(TEntity * p)
{
TEntity * last = FindLastObject();
if (last != NULL)
{

last->next = p;
p->next = NULL;
p->PID = last->PID + 1;

ALOG("NOT FIRST ENTITY: lastpid: "+IntToStr(last->PID) + "  mypid: "+IntToStr(p->PID));

object_len = object_len + 1;

} else //if we add first object
{
	ALOG("THIS IS FIRST_ENTITY pid: 0");
object = p;
object->PID = 0;
object_len = 1;
}

}

now why the heck p->PID = last->PID + 1; ain't working?

Edited by WiredCat

Share this post


Link to post
Share on other sites
Advertisement

Did you try a debugger?

 

I cant spot anything, does your IntToStr function work?

 

EDIT:

Make sure youre not inserting the same object multiple times, that wont work

Edited by Waterlimon

Share this post


Link to post
Share on other sites
typedef std::string AnsiString;


inline AnsiString IntToStr(int i)
{
	std::stringstream s;

	s << i;

	AnsiString converted(s.str());
	return converted;
}

im 100% sure IntToStr works, i yesterday rewrited whole scenatio editor code to use linked lists instead of new delete array, and now i check if it has any bugs, i have problem with object picking because of this. so i used logging to find the error and here it is it doesnt write into that p->PID, and yeah i'm not putting something multiple times.

 

 

EDIT/

Well damn about that ideone.com code, it works there :/

 

maybe this will clarify something:


void Insert(vec3 position, AnsiString fname)
{
ALOG("INSERTING: "+fname);


TObject ahue;
ahue.LoadObject(fname, false, true);
int index = find_object_index(ahue.lname, 32);
if (index < 0) { ALOG("ERROR ERROR object not found ffs"); return; }
TObject * p = OBJECT_LIST.container[ index ];

//temp_arr_object[mission.object_len].object = p;


TEntity * last_o = mission.FindLastObject();
TEntity * new_entity = new TEntity();

if (last_o != NULL)
{
	last_o->next = new_entity;
	ALOG("LAST OBJECT PID: "+IntToStr(last_o->PID));
}
new_entity->object = p;

for (int i=0; i < 32; i++)
{
	new_entity->caption[i] 	= ahue.caption[i];
	new_entity->lname[i] 	= ahue.lname[i];
}

new_entity->namelen = ahue.namelen;

new_entity->hp			= ahue.default_hp;
new_entity->type		= insert_type;
new_entity->pos 		= position;
new_entity->old 		= position;

if (!OBJECT_LIST.container[ index ]->preloaded) //load model if not loaded yet
{
OBJECT_LIST.container[ index ]->LoadObject(fname, true, false);
GetAllObjectsAABB();
}



mission.AddObject(new_entity);


(*insert_mode) = false;

}

i am really not sure what is going on now 

Edited by WiredCat

Share this post


Link to post
Share on other sites

managed to solve that:

 

 

in Insert() function there was

 

TEntity * last_o = mission.FindLastObject();
TEntity * new_entity = new TEntity();

if (last_o != NULL)
{
    last_o->next = new_entity

}

 

 

then i called AddObject where i did the same

last->next = p; <-

p->next = NULL;

 

 

 

 

omg D:::::unsure.png wacko.png blink.png ph34r.png 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!