How to properly register + instance a new custom entity class (Server.CreateEntity returning nil)
DaanVanYperen
The Netherlands Join Date: 2013-06-16 Member: 185580Members, NS2 Playtester, Squad Five Blue, Reinforced - Shadow
I'm having some difficulty instancing a very barebones custom entity class.
So far my steps are:
1. create a new lua script file
2. extend class Entity, have a unique kMapName, call Shared.LinkClassToMap
3. load script in desired vms (server+client)
4. Instance it using Server.CreateEntity(EntityClassName.kMapName) in server vm.
Server.CreateEntity however always returns nil. Debugging doesn't help me as stepping into Server.CreateEntity doesn't work. I suspect I'm missing a step or naming convention required to get this to work.
Am I missing a step? How do I debug this issue?
Class source
So far my steps are:
1. create a new lua script file
2. extend class Entity, have a unique kMapName, call Shared.LinkClassToMap
3. load script in desired vms (server+client)
4. Instance it using Server.CreateEntity(EntityClassName.kMapName) in server vm.
Server.CreateEntity however always returns nil. Debugging doesn't help me as stepping into Server.CreateEntity doesn't work. I suspect I'm missing a step or naming convention required to get this to work.
Am I missing a step? How do I debug this issue?
Class source
Script.Load("lua/Entity.lua") class 'SWSGameInfo' (Entity) SWSGameInfo.kMapName = "sws_game_info" function GetSwsGameInfoEntity() local entityList = Shared.GetEntitiesWithClassname("SWSGameInfo") if entityList:GetSize() > 0 then return entityList:GetEntityAtIndex(0) end end local networkVars = { isTeamBased = "boolean", } function SWSGameInfo:OnCreate() self.isTeamBased = false end if Server then function SWSGameInfo:SetTeamBased(value) self.isTeamBased = value end end function SWSGameInfo:GetTeamBased() return self.isTeamBased end Shared.LinkClassToMap("SWSGameInfo", SWSGameInfo.kMapName, networkVars)
Comments
I see other mods create totally custom entities, so I must be doing /something/ wrong.
My solution was to make another entity that extended the first, with nothing in it, and try to use that instead. Worked perfectly fine... for some reason.
Not sure if this helps you, but might be worth trying.
Make your entities Script Actors, not Entities.
In addition to that, ScriptActor also got loads of things than one might not want to use, such as some of those mixins, depending on what you are doing. Or you could even be making a mod that doesn't have those mixins in the first place.
In fact, many of the ns2 entities does not use SciptActor, an example would be TeamJoin, a subclass of Trigger, which extends Entity.
Entity.OnCreate(self)
Otherwise you never actually call into the Entity OnCreate function where the ent actually gets created.
Another thing worth mentioning would be self:SetUpdates(true/false). Has to be true if you want your entities think function to be called every frame. I usually include it even when false (probably not required), just to make sure I dont decide to add the think function but forget it actually has to be set.
Iirc, in spark the think function would be:
function SWSGameInfo:OnUpdate(deltaTime)
end
The tutorials I used were a bit outdated. The method of including Server.lua in Mod_Server.lua caused my custom entities to malfunction.
NS2 overrides Shared.LinkClassToMap for entry based modloading at the end of Server.lua, making it defunct for old style modding. The original function gets mapped to SharedLinkClassToMapOriginal, so used that for testing and it started working.
So I'm fixing it by using the entry file method instead of game_setup.xml.
We need a new "how to mod ns2" (with hooks etc.) tut, the number of ppl still using the game_setup.xml for even the smallest mods is too high.