How to properly register + instance a new custom entity class (Server.CreateEntity returning nil)

DaanVanYperenDaanVanYperen The Netherlands Join Date: 2013-06-16 Member: 185580Members, NS2 Playtester, Squad Five Blue, Reinforced - Shadow
edited January 2014 in Modding
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
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

  • Soul_RiderSoul_Rider Mod Bean Join Date: 2004-06-19 Member: 29388Members, Constellation, Squad Five Blue
    Would you not be better making that a sub of NS2 Gamerules? As it specifically relates to the rules of the game, you would be better doing it that way. Then the SWSGamerules can be added as a map entity, so you are able to get this info from in game.
  • DaanVanYperenDaanVanYperen The Netherlands Join Date: 2013-06-16 Member: 185580Members, NS2 Playtester, Squad Five Blue, Reinforced - Shadow
    This is just a barebones example for clarity. I understand how to work around my limitations on the ns2 platform, but I want to actually understand the entity creation process.

    I see other mods create totally custom entities, so I must be doing /something/ wrong.
  • FehaFeha Join Date: 2006-11-16 Member: 58633Members
    edited January 2014
    I remember that back when I did stuff I noticed I had issues (as in, the thing not showing up, so might be the same?) with entities that extended Entity. My entities were created through loading a map with them.
    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.
  • Soul_RiderSoul_Rider Mod Bean Join Date: 2004-06-19 Member: 29388Members, Constellation, Squad Five Blue
    You need to be using Script Actor as the base class for all in-game entities. Entity is an Engine class, and Script Actor is the base game class that hooks into it.

    Make your entities Script Actors, not Entities.
  • FehaFeha Join Date: 2006-11-16 Member: 58633Members
    Isn't ScriptActor only possible to use if you base your mod on regular ns2? If you mod your entirely own game it wouldn't exist.

    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.
  • xDragonxDragon Join Date: 2012-04-04 Member: 149948Members, NS2 Playtester, Squad Five Gold, NS2 Map Tester, Reinforced - Shadow
    edited January 2014
    I would imagine its because you need this in your OnCreate() function:

    Entity.OnCreate(self)

    Otherwise you never actually call into the Entity OnCreate function where the ent actually gets created.
  • FehaFeha Join Date: 2006-11-16 Member: 58633Members
    Nice catch xdragon, as you say, unless that line is added it will definitely not work correctly.
    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
  • DaanVanYperenDaanVanYperen The Netherlands Join Date: 2013-06-16 Member: 185580Members, NS2 Playtester, Squad Five Blue, Reinforced - Shadow
    edited January 2014
    Thanks for all the posts. I tried everything, to no avail. Had a hunch though, and found the issue!

    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.
  • GhoulofGSG9GhoulofGSG9 Join Date: 2013-03-31 Member: 184566Members, Super Administrators, Forum Admins, Forum Moderators, NS2 Developer, NS2 Playtester, Squad Five Blue, Squad Five Silver, Reinforced - Supporter, WC 2013 - Supporter, Pistachionauts
    edited January 2014
    Thanks for all the posts. I tried everything, to no avail. Had a hunch though, and found the issue!

    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.
Sign In or Register to comment.