Qvorvm Posted June 11 Posted June 11 Does anyone have an explanation for this? The following code, sometimes, ends up with body == None (and a normal actor ref in npc...): Actor npc = JArray.getForm( stack, -2) as Actor if !npc.hasKeywordString( "ActorTypeNPC") return endif ActorBase body = npc.getLeveledActorBase()
GumboMods Posted June 12 Posted June 12 (edited) I wonder whats in that JArray[] exactly. From what I learned about GetForm(), it doesn't really work on forms that are generated during run time when they aren't loaded. I don't know if that would be the issue, but that's the best explanation I can think considering you say it does work sometimes. EDIT: If GetForm() isn't the issue here, then perhaps GetLeveledActorBase() has a similar problem where it wont run on references that aren't loaded? I am not sure though. Edited June 12 by GumboMods
Qvorvm Posted June 12 Author Posted June 12 Heh, Figured it out. The actor base is ok when I execute the code, but then I store the result in a JContainer. Some time later, the player increases level, the game rebuilds the npcs' actor bases, the temporary base I stored is deleted and a new one created. My JContainer now contains references to deleted leveled actor bases.
scorrp10 Posted June 12 Posted June 12 AFAIK, It is really not about player leveling. It is about memory management and garbage collection. GetLeveledActorBase() allocates a temporary instance of the object in memory, and once the variable it has been assigned to goes out of scope, the memory it occupies is considered free. As long as it remains in scope, the object is there, and player leveling up should have no effect on it. Keeping a reference into free memory is very dangerous. If that memory has not yet been reused for something else, the data written into it is still there, so it may APPEAR as if the object is still valid. However, it may have been overwritten partially. If the vtable area that basically identifies what type of object it is, has been overwritten, the object will no longer cast to ActorBase and return None upon attempt. Note also that for actors who do not level, this function returns their initial ActorBase, which does not get recycled. In general, I would say GetLeveledActorBase() should be avoided, and used only when you actually need to access or change things that do change with level. Such as total HP, skill values and such.
Qvorvm Posted June 12 Author Posted June 12 Thank you for your comments Scorrp10. The only detail my experiments contradict in your statements is that the levelled actor base is allocated when the npc levels, not when you call getLeveledActorBase. In this instance, I was just surprised by the appearance of 0x0 in my data. Now that I understand where it came from, I can adjust my code to take this corner case into account, and as you suggest it will entail being more careful with accessing/storing information relative to the levelled bases.
Recommended Posts