I was helping Sus debug some NPC egos and found an issue in the mod.class.NPC definition. Basically if you do not set "type" in a newEntity definition it grabs the global Lua type function instead, and then causes problems when trying to set the image value based on the type/subtype/name in mod.class.NPC:init.
I tracked the problem down to the global namespace _G being used as the base of all classes. Is there some reason not to use an empty table as a base? I tried changing the definition of base in engine/class.lua to {} and the game appears to work just fine.
[svn2574] Trouble adding NPC egos... global class base?
Moderator: Moderator
[svn2574] Trouble adding NPC egos... global class base?
<DarkGod> lets say it's intended
Re: [svn2574] Trouble adding NPC egos... global class base?
Uh ? are you sure it prevented the problem?
Becasue base (the local defined at line 22) is actually never used.
The problem comes from the fact that most classes use package.seeall which brings _G into _M.
But that cant be easily avoided because otherwise you'd need to change like all the lua files of the game to explicitly import each function/table they need and change each serializable function to not directly access stuff like "engine.Map" but require it and then use it.
I agree it would look better but it aint worth the effort :/
Becasue base (the local defined at line 22) is actually never used.
The problem comes from the fact that most classes use package.seeall which brings _G into _M.
But that cant be easily avoided because otherwise you'd need to change like all the lua files of the game to explicitly import each function/table they need and change each serializable function to not directly access stuff like "engine.Map" but require it and then use it.
I agree it would look better but it aint worth the effort :/
[tome] joylove: You can't just release an expansion like one would release a Kraken XD
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning

Re: [svn2574] Trouble adding NPC egos... global class base?
Ah right you are... I had my other fix creep in when I was testing things. It would be nice to keep the namespace separate, and this is where my limited Lua experience is going to really show. Is it possible to simply change all calls within modules to _G.engine.Map, etc? I realize it would be a lot of editing of files, but this seems preferable to adding a require statement in every file.
Alternatively we can deal with these things as they show up. The root issue here is that we are using a variable name that exists in the global namespace (type), but this variable will not be filled with egos and therefore refer back to global function. Ways to fix this... well a major refactor to use "maintype" and "subtype" instead of "type" and "subtype" would fix the problem. An easier but slightly more confusing way would be to use this patch for mod.class.NPC: http://pastebin.com/cGDtLApF
This of course only band-aids that specific problem, and other checks for NPC.type before the ego is merged with the actual entity could cause issues. I ran it in-game and there do not appear to be any issues with it now, so this is probably the fix for the moment.
EDIT: Tweaked the patch.
Alternatively we can deal with these things as they show up. The root issue here is that we are using a variable name that exists in the global namespace (type), but this variable will not be filled with egos and therefore refer back to global function. Ways to fix this... well a major refactor to use "maintype" and "subtype" instead of "type" and "subtype" would fix the problem. An easier but slightly more confusing way would be to use this patch for mod.class.NPC: http://pastebin.com/cGDtLApF
This of course only band-aids that specific problem, and other checks for NPC.type before the ego is merged with the actual entity could cause issues. I ran it in-game and there do not appear to be any issues with it now, so this is probably the fix for the moment.
EDIT: Tweaked the patch.
<DarkGod> lets say it's intended
Re: [svn2574] Trouble adding NPC egos... global class base?
Major rewrite is .. major :/
But I dont get it, why doesnt ego work for npcs ? they work fine for items
But I dont get it, why doesnt ego work for npcs ? they work fine for items
[tome] joylove: You can't just release an expansion like one would release a Kraken XD
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning

Re: [svn2574] Trouble adding NPC egos... global class base?
Yup, not worth it.darkgod wrote:Major rewrite is .. major :/
My understanding of egos is that they are actually created as an NPC, Object, whatever and then mixed into the original entity. The problem arises when creating the NPC ego, specifically on this line in mod.class.NPC:initBut I dont get it, why doesnt ego work for npcs ? they work fine for items
Code: Select all
if not self.image and self.name ~= "unknown actor" then self.image = "npc/"..(self.type or "unknown").."_"..(self.subtype or "unknown").."_"..(self.name or "unknown"):lower():gsub("[^a-z0-9]", "_")..".png" end
<DarkGod> lets say it's intended