Rockythechao wrote:Okay, it sounds like there's something seriously wrong with the Actor generation loop. I'll have to dig into that code after I get back from dinner.
In the meantime,
I've started the Actor class page over on the wiki. Most of the member functions and variables are listed there but they're not all inserted into the table, given descriptions, etc. yet.
I'll keep poking at it for a while to see if I can find anything out. Interestingly, it appears to be reporting the "wordBubbleIcon" value as nil when it tried to draw it (even before it breaks). This might be junk data from another NPC though, so I'll keep probing.
EDIT: Never mind, it was junk data. It also seems the NPC also only occasionally breaks after collecting a dragon coin in the same area.
EDIT EDIT: The NPC appears to be functioning correctly now for some reason, but collecting 2 dragon coins still crashes the editor.
Ahh... the problem has migrated to another NPC. This is interesting.
Okay, so the error now occurs when I collect the second dragon coin, and doesn't crash the editor. It seems, however, that "updateNPCMessages" isn't referencing that NPC. This suggests that it's being removed from the indexed actors list. It's possible that the dragon coin being collected is incorrectly removing the NPC from the list.
Right. I think the cause might be that the "npcs" call in LunaLua returns surviving npcs, but the order in which it does so is not guaranteed. When the dragon coin is killed, it removes an NPC from somewhere in the middle of that list. This invalidates some saved indices, and the crash is caused by an out of bounds array index. This is just speculation, but it does make a certain amount of sense. The only bit that doesn't is that this doesn't occur for all NPCs. It seems to only occur for dragon coins.
Okay, new info. When the NPC breaks, it seems that it moves. I printed out the distances cinematX logs, and it jumps from about 500 (which cinematX deems on screen and draws the icon) to 225946 (which is obviously offscreen). I suspect this is how SMBX deals with objects being killed (moving them into a "dead" zone), and cinematX is reading the wrong value.
The indexActors function is behaving as expected.
AHA! When collecting the dragon coin, one of the indexed object's SMBX NPC id is set to 0. This is the NPC that breaks. It appears to be a null struct - that is, default values. It looks like the indexed item has been replaced with a new one.
Okay. The actor that breaks appears to be shifted to the first in the list when npcs() is called - replacing the position held by the dragon coin. A potential solution to this would be to sort the npc list in Lua. I'll see if that helps...
I implemented a quick sorting algorithm, and set it up so the dragon coins were just popped off the end. The error still occurs.
Right, I'm going off to bed now. Hopefully the information in this post will be helpful at diagnosing the problem.
One more bit of information: If I place a debug printText that accesses one of the string fields of the SMBX object, the game crashes when the dragon coin is collected. I suspect this is due to an out of bounds pointer (probably set to 0, like all the other fields).
I've drilled right into all of the Lua code in cinematX, and it seems like the error is probably coming from the underlying C++, rather than the Lua. It's either that, or the destructive act of deleting the NPCs message string is breaking things (which is possibly the case).
You could possibly fix this by re-indexing the actors each frame. This is more difficult than it sounds because the message string is removed after the actor has been indexed. If it were somehow possible to recover the original message string, and restore it (or cache it once and use the cached version), then it might be doable. The problem is that the SMBX object isn't usable as a key (as that is being reset), and it needs to be able to ensure it's the same NPC from SMBXs side. I'll see if I can do anything.
Hmm... given it a try. I could do this relatively easily
if SMBX NPCs had a unique, unchanging ID. Unfortunately, this doesn't appear to be the case... I've got it working on the condition that there is only one CinematX NPC of each NPC ID. Any more, and it will break. I need a way to determine individual NPCs directly from the SMBX data, and the internal array index is useless because it isn't guaranteed to be the same during execution.