Page 1 of 1

[svn2574] Trouble adding NPC egos... global class base?

Posted: Mon Jan 31, 2011 6:37 am
by yufra
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.

Re: [svn2574] Trouble adding NPC egos... global class base?

Posted: Mon Jan 31, 2011 8:15 am
by darkgod
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 :/

Re: [svn2574] Trouble adding NPC egos... global class base?

Posted: Mon Jan 31, 2011 5:11 pm
by yufra
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.

Re: [svn2574] Trouble adding NPC egos... global class base?

Posted: Mon Jan 31, 2011 5:37 pm
by darkgod
Major rewrite is .. major :/

But I dont get it, why doesnt ego work for npcs ? they work fine for items

Re: [svn2574] Trouble adding NPC egos... global class base?

Posted: Mon Jan 31, 2011 8:40 pm
by yufra
darkgod wrote:Major rewrite is .. major :/
Yup, not worth it.
But I dont get it, why doesnt ego work for npcs ? they work fine for items
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:init

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
Here we try to set a default image for the NPC ego. The ego's type/subtype were not set and probably won't be in the majority of cases, so these values should be nil, but the type is actually the global Lua function. So when you evaluate (self.type or "unknown") you get the global Lua function instead of "unknown", which then throws an error when you try to concatenate it. The patch I linked above simply rewrites that image section and makes sure that self.type is a string rather than a function, and if it is not a string sets it to "unknown".