• Underdone (UDK) error spawning custom npc
    5 replies, posted
I'm adding a new npc to underdone and I'm getting this error: [b]Hook 'SpawnMapEntities' Failed: [@gamemodes\udk\gamemode\core\sharedfiles\sh_map.lua:139] Tried to use a NULL entity![/b] I haven't added or removed anything from sh_map.lua How can I fix it? Here's a video one what happens [url]http://www.youtube.com/watch?v=vkoGTTNfYdo[/url] [b]sh_npcs_combat.lua (slime line)[/b] [lua]local NPC = QuickNPC("slime", "Slime", "slime", "slime", 1000) AddDrop(NPC, "money", 50, 15, 18) AddDrop(NPC, "item_bananna", 50) AddDrop(NPC, "food_orange", 50) AddDrop(NPC, "item_canmeat", 50) AddMultiplier(NPC, 10, 2) NPC.DeathDistance = 14 Register.NPC(NPC)[/lua] [b]sh_map.lua[/b] [lua]GM.MapEntities = {} GM.MapEntities.NPCSpawnPoints = {} GM.MapEntities.WorldProps = {} function GM:CreateSpawnPoint(vecPosition, angAngle, strNPC, intLevel, intSpawnTime) table.insert(GAMEMODE.MapEntities.NPCSpawnPoints, {}) local intNumSpawns = #GAMEMODE.MapEntities.NPCSpawnPoints GAMEMODE:UpdateSpawnPoint(intNumSpawns, vecPosition, angAngle, strNPC, intLevel, intSpawnTime) end function GM:RemoveSpawnPoint(intKey) local tblSpawnPoint = GAMEMODE.MapEntities.NPCSpawnPoints[intKey] if tblSpawnPoint then if tblSpawnPoint.Monster then tblSpawnPoint.Monster:Remove() end table.remove(GAMEMODE.MapEntities.NPCSpawnPoints, intKey) end if SERVER && SinglePlayer() && player.GetByID(1) && player.GetByID(1):IsValid() then SendUsrMsg("UD_RemoveSpawnPoint", player.GetByID(1), {intKey}) end end function GM:UpdateSpawnPoint(intKey, vecPosition, angAngle, strNPC, intLevel, intSpawnTime) local tblNPCTable = NPCTable(strNPC) local tblToUpdateSpawn = GAMEMODE.MapEntities.NPCSpawnPoints[intKey] if tblToUpdateSpawn then tblToUpdateSpawn.Postion = vecPosition or tblToUpdateSpawn.Postion tblToUpdateSpawn.Angle = angAngle or tblToUpdateSpawn.Angle or Angle(0, 0, 0) tblToUpdateSpawn.NPC = strNPC or tblToUpdateSpawn.NPC or "zombie" tblToUpdateSpawn.Level = intLevel or tblToUpdateSpawn.Level or 5 tblToUpdateSpawn.SpawnTime = intSpawnTime or tblToUpdateSpawn.SpawnTime or 0 if ValidEntity(tblToUpdateSpawn.Monster) then tblToUpdateSpawn.Monster:SetAngles(tblToUpdateSpawn.Angle) end if SERVER && SinglePlayer() && ValidEntity(player.GetByID(1)) then SendUsrMsg("UD_UpdateSpawnPoint", player.GetByID(1), {intKey, tblToUpdateSpawn.Postion, tblToUpdateSpawn.Angle, tblToUpdateSpawn.NPC, tblToUpdateSpawn.Level, tblToUpdateSpawn.SpawnTime}) end else GAMEMODE:CreateSpawnPoint(vecPosition, angAngle, strNPC, intLevel, intSpawnTime) end end function GM:CreateWorldProp(strModel, vecPostion, angAngle, entEntity, boolLoad) if SERVER then local tblNewObject = {} tblNewObject.SpawnProp = function() local entNewProp = ents.Create(GetPropClass(strModel)) tblNewObject.Entity = entNewProp table.insert(self.MapEntities.WorldProps, tblNewObject) self:UpdateWorldProp(#self.MapEntities.WorldProps, strModel, vecPostion, angAngle, entNewProp) entNewProp:SetSkin(math.random(0, entNewProp:SkinCount())) entNewProp:Spawn() end tblNewObject.SpawnProp() return tblNewObject.Entity elseif CLIENT then table.insert(self.MapEntities.WorldProps, {Entity = entEntity}) self:UpdateWorldProp(#self.MapEntities.WorldProps, strModel, vecPostion, angAngle, entEntity) end end function GM:RemoveWorldProp(intKey) local tblWorldProp = GAMEMODE.MapEntities.WorldProps[intKey] if tblWorldProp then if tblWorldProp.Entity && tblWorldProp.Entity:IsValid() then tblWorldProp.Entity:Remove() end table.remove(GAMEMODE.MapEntities.WorldProps, intKey) end if SERVER && SinglePlayer() && player.GetByID(1) && player.GetByID(1):IsValid() then SendUsrMsg("UD_RemoveWorldProp", player.GetByID(1), {intKey}) end end function GM:UpdateWorldProp(intKey, strModel, vecPosition, angAngle, entEntity, boolLoad) local tblToUpdateProp = GAMEMODE.MapEntities.WorldProps[intKey] if tblToUpdateProp && ValidEntity(tblToUpdateProp.Entity) then local entProp = tblToUpdateProp.Entity if SERVER then local strPreModel = entProp:GetModel() entProp:SetModel(strModel or entProp:GetModel() or "models/props_junk/garbage_metalcan001a.mdl") entProp:SetPos(vecPosition or entProp:GetPos()) if strPreModel != entProp:GetModel() && !boolLoad then entProp:SetPos(GetFlushToGround(entProp)) end entProp:SetAngles(angAngle or entProp:GetAngles()) entProp:PhysicsInit(SOLID_VPHYSICS) entProp:SetMoveType(MOVETYPE_NONE) entProp:DrawShadow(false) entProp:SetKeyValue("spawnflags", 8) entProp.ObjectKey = intKey if SinglePlayer() && player.GetByID(1) && player.GetByID(1):IsValid() then SendUsrMsg("UD_UpdateWorldProp", player.GetByID(1), {intKey, entProp:GetModel(), entProp:GetPos(), entProp:GetAngles(), entProp}) end end tblToUpdateProp.Model = entProp:GetModel() tblToUpdateProp.Postion = entProp:GetPos() tblToUpdateProp.Angle = entProp:GetAngles() else GAMEMODE:CreateWorldProp(strModel, vecPosition, angAngle, entEntity) end end if SERVER then function GM:LoadMapObjects() local strFileName = "UnderDone/Maps/" .. game.GetMap() .. ".txt" if !file.Exists(strFileName) then return end local tblDecodedTable = glon.decode(file.Read(strFileName)) for _, SpawnPoint in pairs(tblDecodedTable.NPCSpawnPoints or {}) do GAMEMODE:CreateSpawnPoint(SpawnPoint.Postion, SpawnPoint.Angle or Angle(0, 90, 0), SpawnPoint.NPC, SpawnPoint.Level, SpawnPoint.SpawnTime) end for k, WorldProp in pairs(tblDecodedTable.WorldProps or {}) do timer.Simple(0.05 * k, function() GAMEMODE:CreateWorldProp(WorldProp.Model, WorldProp.Postion, WorldProp.Angle, nil, true) end) end end hook.Add("Initialize", "LoadMapObjects", function() GAMEMODE:LoadMapObjects() end) function GM:SaveMapObjects() local strFileName = "UnderDone/Maps/" .. game.GetMap() .. ".txt" local tblSaveTable = table.Copy(GAMEMODE.MapEntities) for _, SpawnPoint in pairs(tblSaveTable.NPCSpawnPoints or {}) do SpawnPoint.Monster = nil SpawnPoint.NextSpawn = nil end for _, WorldProp in pairs(tblSaveTable.WorldProps or {}) do WorldProp.Entity = nil WorldProp.SpawnProp = nil end file.Write(strFileName, glon.encode(tblSaveTable)) end function GM:SpawnMapEntities() for _, Spawn in pairs(GAMEMODE.MapEntities.NPCSpawnPoints) do if !Spawn.Monster or !Spawn.Monster:IsValid() && #ents.FindByClass("npc_*") < 500 && !GAMEMODE.EventHasStarted then if !Spawn.NextSpawn then Spawn.NextSpawn = CurTime() + Spawn.SpawnTime end if Spawn.SpawnTime > 0 && CurTime() >= Spawn.NextSpawn then Spawn.Monster = GAMEMODE:CreateNPC(Spawn.NPC, Spawn) Spawn.NextSpawn = nil end end end end hook.Add("Tick", "SpawnMapEntities", function() GAMEMODE:SpawnMapEntities() end) function GM:CreateNPC(strNPC, tblSpawnPoint) local tblNPCTable = NPCTable(strNPC) if !tblNPCTable then return end if tblNPCTable.SpawnName == "npc_turret_floor" then return end local entNewMonster = ents.Create(tblNPCTable.SpawnName) entNewMonster:SetPos(tblSpawnPoint.Postion) entNewMonster:SetAngles(tblSpawnPoint.Angle or Angle(0, 90, 0)) entNewMonster:SetKeyValue("spawnflags","512") entNewMonster:DrawShadow(false) if tblNPCTable.Weapon then entNewMonster:Give(tblNPCTable.Weapon ) entNewMonster:SetKeyValue( "additionalequipment", tblNPCTable.Weapon) entNewMonster:SetKeyValue("spawnflags","8192") end if tblNPCTable.Accuracy then entNewMonster:SetCurrentWeaponProficiency( tblNPCTable.Accuracy ) end if tblNPCTable.Model then entNewMonster:SetModel(tblNPCTable.Model) end if tblNPCTable.Color then local r = tblNPCTable.Color[1] local g = tblNPCTable.Color[2] local b = tblNPCTable.Color[3] local a = tblNPCTable.Color[4] entNewMonster:SetColor(r,g,b,a) end entNewMonster:Spawn() if tblNPCTable.DeathDistance then for _, ent in pairs(ents.FindInSphere( tblSpawnPoint.Postion, tblNPCTable.DeathDistance )) do if ValidEntity(ent) && ent:IsPlayer() then ent:Kill() end end end if tblNPCTable.Frozen then entNewMonster:DropToFloor() local phys = entNewMonster:GetPhysicsObject() if ValidEntity( phys ) then phys:EnableMotion( false ) end en
As you see in your own video, there is no NPC entity with the classname 'slime'. ents.Create will thereby return a NULL entity instead. The code is working as it should, except it doesn't have exceptions in case the entity class didn't exist.
[QUOTE=leiftiger;32413790]As you see in your own video, there is no NPC entity with the classname 'slime'. ents.Create will thereby return a NULL entity instead. The code is working as it should, except it doesn't have exceptions in case the entity class didn't exist.[/QUOTE] Okay thanks for that, but now I'm getting this error: [b]Attempted to create unknown entity type slime![/b] I've tried looking for a reference but I can't find one... It would be a lot easier if I did. Could anyone tell me what I should put in the entities folder? gamemodes/underdone/entities/entities
Okay guys, I've been trying to get this working all day and I just realized, how can the default npcs exist without the codes I "need" for the custom NPCs to work. If anyone knows what I've done wrong here's the NPC code and the lines I've edited to make it work. [url=http://www.garrysmod.org/downloads/?a=view&id=125269]Slime Download (In-case I put the wrong model/animations in)[/url] This npc line will most probably be wrong as I have no templates to work off. [lua]local NPC = QuickNPC("slime", "Slime", "slime", "slime", 1000) AddDrop(NPC, "money", 50, 15, 18) AddDrop(NPC, "item_bananna", 50) AddDrop(NPC, "food_orange", 50) AddDrop(NPC, "item_canmeat", 50) AddMultiplier(NPC, 10, 2) NPC.DeathDistance = 14 Register.NPC(NPC)[/lua]
[QUOTE=.Rogi;32417987]Okay thanks for that, but now I'm getting this error: [b]Attempted to create unknown entity type slime![/b][/QUOTE] You're telling the gamemode to spawn a "slime" entity, which doesn't exist. You probably want to tell it to spawn "sent_slime_small" or something similar instead.
[QUOTE=mrflippy;32421929]You're telling the gamemode to spawn a "slime" entity, which doesn't exist. You probably want to tell it to spawn "sent_slime_small" or something similar instead.[/QUOTE] Thank you soooooo much dude I've been trying to figure this out for ages, Thanks.
Sorry, you need to Log In to post a reply to this thread.