Problem with Create(solved)


(christopf) #1

Hi, i recently changed alot of my core mechanic and added more animation to the entities. now i got a problem and can’t find a solution for it. i guess my code is way to messy and huge to find the precise error but i was wondering if someone else had maybe problems with the create function?

to desribe it detailed:
my hero gets in a state so enemies are coming to him and if they contact him they play a special animation and in the end they get recycled. if the hero moves in this state he is going to become one of the enemies (the last that touched him). that works fine.
but if the hero moves in this state while the enemy plays the special leaving animation the new created entity is kinda messy, playing not animation and seems to aint working (at least not at the other enemies of the same class). additionally after this has happened, other problems occur, like every new enemy spawned gets removed directly after spawning.

to now i’m trying to change every single parameter and debug to find the solution but i have a bad feeling about this :confused:


(Ultima2876) #2

This sounds like a typical problem with recycling when you forget to reset all of the entity’s variables in their added() function. Be sure that you set everything to a reasonable default value there!

I find a good way to do it is to copy the entire variable list in the class to that added function, then go through and ‘format’ it correctly to have appropriate default values. That usually ensures that I’m definitely resetting everything that needs it!

It might be faster for you to first go through all of your entities and check that before trying to debug using other methods.


(christopf) #3

This sounds absolutly logical.
Is there a difference when i write something into the added() function of a mother class (in this case “Boid”) or the child class (leutnana, dogz, etc). What has a higher priority? If i have a value in the mother class and no mentioning in the child class, is it still active? Or the other way around child - mother?

Generally, maybe no fitting the topic here, i have a lot of logic issues. Is there somewhere a guide you could recommend?


(christopf) #4

trace() ftw.
i found out that its calling the callback function again after added. thats weird because i couldnt finde a difference to my hero class when calling callback() but there it aint happen. i post some code maybe you find a hint

override public function update():void
{
	super.update();
	layer = -this.y;	
		
		
	if ((einst != 4) && (ti == 0)) { leutnan.play("ost_stehen") }	
	else if ((einst != 4) && (ti > 0)) { leutnan.play(disrect) }
	else if (einst == 4) { leutnan.play("bye");  leutnan.callback = freeme }
							
	if (HP <= 0) { zuruck() }
}

and the callback is freeme()

private function freeme():void
{
	nureinEP = false;
	FP.world.recycle(this);
}

after added its playing the “ost_stehen” animation, so i dont know. normally it shouldnt have been called or?


(Bora Kasap) #5

actually i couldn’t understand your problem exactly because of my english level. but i feel you don’t know something important about create class?

when you create an entity with create function, codelines in the entity’s constructor function are works also added() function works when it’s added to the world(1 frame later), but then, when you recycled and tried to creating it again, this time it skips constructor functions and only “added()” function works.


(christopf) #6

Yes i figured this out. The problem is kinda solved too.
But what makes me wondering is that after created out of the recycle-cycle the entity play the callback method although the variable to trigger it (einst == 4) isnt active (einst == 0). My concluson was that the callback is used for every animation.play(“sameSpritesheet”) but in an other entity it aint a problem.
Probably its just a logic thinking error i couldnt figured out yet so i keep wondering :smiley:


(Ultima2876) #7

If I understand you right, you can check whether the entity is ‘active’ in the callback before performing any logic to avoid callbacks triggering and causing issues with recycled entities (it will be set to active == false when recycled).

As for the order of added() calls in entities, this is something you can control. In your added() override, you should always have a super.added() call. This is where the base/super/mother class will run its own added() function. This is the same for any override method - it’s a good idea to form a habit of always calling the super.blah() function!


(christopf) #8

Jep, i already formed that habit :smiley: If read here so often of it, i was doing it every time without really knowning why. (maybe aint so good either).
That with the callback was no problem after finding the root of the misbehavior. I just added the same premises as for the callback iniation. I was just wondering why its still getting called after the recycling process (although the premises for the original initiation aint given anymore)