Jump to content

c# Zombie spawner


cookyman

Recommended Posts

Englisch Google Translate

 

Good evening,

I have a problem with the zombie spawner here.

 

I go through this loop every 30 minutes,

spawning a zombie, it also works until there is a mistake.

 

What could be the reason ?

 

Here is the code excerpt:

 

private static void Spawn(Vector3 _pos, int _rotation){
foreach (KeyValuePair<string, int> kvp in test)
{
	var random = new System.Random();
	int index = random.Next(z_namenlist.Count);
	string z_name = z_namenlist[index];
	string name = z_name;
	Entity Spawn_zombie = EntityFactory.CreateEntity(EntityClass.FromString(name), _pos);
	Vector3 vec = Spawn_zombie.rotation;
	vec.y = _rotation;
	Spawn_zombie.SetRotation(vec);
	Spawn_zombie.SetPosition(_pos);
	GameManager.Instance.World.SpawnEntityInWorld(Spawn_zombie);
}
}

 

Here the error from the log:

Receiving unhandled NULL exception

#0 0x007fd15059b780 in _L_unlock_13

#1 0x007fd15059be10 in physx::Cm::CmEventNameProvider::~CmEventNameProvider()

#2 0x007fd15059be70 in physx::Cm::CmEventNameProvider::~CmEventNameProvider()

#3 0x007fd15059bea0 in physx::Cm::CmEventNameProvider::~CmEventNameProvider()

#4 0x007fd15059bef0 in operator new[](unsigned long, std::nothrow_t const&)

#5 0x007fd15059bf50 in (Unknown)

#6 0x007fd15059bf60 in (Unknown)

 

Deutsch:

 

Schِnen guten Abend,

ich habe hier ein Problem beim Zombie spawner.

 

Ich durchlaufe alle 30 Minuten diese schleife,

um ein ein Zombie spawnen, dies funktioniert auch, bis es ein Fehler gibt.

 

An was kann das liegen ?

 

Code siehe oben.

 

Who can help me ?

 

MFG cookyman

Link to comment
Share on other sites

Look at the spawnvehicle item class or SpawnEntity block class in the DLL.

 

Both can be used to spawn entities. They might help you identify the problem.

 

Google Translate Deutsche.

 

Schauen Sie sich die Spawnvehicle-Elementklasse oder die SpawnEntity-Blockklasse in der DLL an.

 

Beide kِnnen verwendet werden, um Entitنten zu erzeugen. Sie kِnnen Ihnen helfen, das Problem zu identifizieren.

Link to comment
Share on other sites

That works so far,

after about 10 passes this error comes as written above.

I have tried and tested a lot but the error keeps coming back.

 

Here is my whole script with the loop.

        public static void Auto_spawn()
       {
           if (pos.Count > 0 && ConnectionManager.Instance.ClientCount() > 0)
           {
               try
               {
                   foreach (KeyValuePair<string, int> kvp in pos)
                   {
                       Entities = GameManager.Instance.World.Entities.list;
                       string[] _pos = kvp.Key.Split(',');
                       int.TryParse(block_pos[0], out int _pos_x);
                       int.TryParse(block_pos[1], out int _pos_y);
                       int.TryParse(block_pos[2], out int _pos_z);

                       var vector3_block_pos = new Vector3((float)_pos_x, (float)_pos_y, (float)_pos_z);

                       World world = GameManager.Instance.World;
                       bool spieler_da = false;
                       bool trader_da = false;

                       List<EntityPlayer> PlayerList = world.Players.list;
                       for (int i = 0; i < PlayerList.Count; i++)
                       {
                           EntityPlayer _player = PlayerList[i];
                           if (_player != null)
                           {
                               ClientInfo _cInfo = ConnectionManager.Instance.Clients.ForEntityId(_player.entityId);
                               if (_cInfo != null && _player.IsSpawned() && _player.IsAlive())
                               {
                                   EntityPlayer _douche = world.GetClosestPlayer(vector3_block_pos, 75, false);
                                   if (_douche != null)
                                   {
                                       spieler_da = true;
                                       continue;
                                   }
                               }
                           }
                       }

                       if (spieler_da)
                       {
                           List<ClientInfo> _cInfoList = ConnectionManager.Instance.Clients.List.ToList();
                           ClientInfo _cInfo = _cInfoList.RandomObject();

                           int entityID = EntityGroups.GetRandomFromGroup("Z_Spawner");
                           Entity spawnEntity = EntityFactory.CreateEntity(entityID, vector3_block_pos) as EntityEnemy;

                           //world.entityDistributer.Add(spawnEntity);
                           //world.aiDirector.AddEntity((Entity)spawnEntity);


                           world.SpawnEntityInWorld(spawnEntity);
                           //world.AddEntityToMap(spawnEntity);
                           SdtdConsole.Instance.ExecuteSync(string.Format("saveworld"), _cInfo);
                       }
                   }
               }
               catch (Exception e)
               {
                   Logs.MYSQL_Log(Class_Name, MethodBase.GetCurrentMethod().Name, string.Format("Es ist ein Fehler aufgetreten. Fehler Bezeichnung: {0}", e.Message));
               }
           }
       }

Link to comment
Share on other sites

That works so far,

after about 10 passes this error comes as written above.

I have tried and tested a lot but the error keeps coming back.

 

Here is my whole script with the loop.

        public static void Auto_spawn()
       {
           if (pos.Count > 0 && ConnectionManager.Instance.ClientCount() > 0)
           {
               try
               {
                   foreach (KeyValuePair<string, int> kvp in pos)
                   {
                       Entities = GameManager.Instance.World.Entities.list;
                       string[] _pos = kvp.Key.Split(',');
                       int.TryParse(block_pos[0], out int _pos_x);
                       int.TryParse(block_pos[1], out int _pos_y);
                       int.TryParse(block_pos[2], out int _pos_z);

                       var vector3_block_pos = new Vector3((float)_pos_x, (float)_pos_y, (float)_pos_z);

                       World world = GameManager.Instance.World;
                       bool spieler_da = false;
                       bool trader_da = false;

                       List<EntityPlayer> PlayerList = world.Players.list;
                       for (int i = 0; i < PlayerList.Count; i++)
                       {
                           EntityPlayer _player = PlayerList[i];
                           if (_player != null)
                           {
                               ClientInfo _cInfo = ConnectionManager.Instance.Clients.ForEntityId(_player.entityId);
                               if (_cInfo != null && _player.IsSpawned() && _player.IsAlive())
                               {
                                   EntityPlayer _douche = world.GetClosestPlayer(vector3_block_pos, 75, false);
                                   if (_douche != null)
                                   {
                                       spieler_da = true;
                                       continue;
                                   }
                               }
                           }
                       }

                       if (spieler_da)
                       {
                           List<ClientInfo> _cInfoList = ConnectionManager.Instance.Clients.List.ToList();
                           ClientInfo _cInfo = _cInfoList.RandomObject();

                           int entityID = EntityGroups.GetRandomFromGroup("Z_Spawner");
                           Entity spawnEntity = EntityFactory.CreateEntity(entityID, vector3_block_pos) as EntityEnemy;

                           //world.entityDistributer.Add(spawnEntity);
                           //world.aiDirector.AddEntity((Entity)spawnEntity);


                           world.SpawnEntityInWorld(spawnEntity);
                           //world.AddEntityToMap(spawnEntity);
                           SdtdConsole.Instance.ExecuteSync(string.Format("saveworld"), _cInfo);
                       }
                   }
               }
               catch (Exception e)
               {
                   Logs.MYSQL_Log(Class_Name, MethodBase.GetCurrentMethod().Name, string.Format("Es ist ein Fehler aufgetreten. Fehler Bezeichnung: {0}", e.Message));
               }
           }
       }

 

Maybe move yuor SdtdConsole.instance.ExecuteSync() outside of all your loops? I am not sure what that does. I have not used it.

 

Maybe also set spawner type?

 

           spawnEntity.SetSpawnerSource(EnumSpawnerSource.Dynamic);
           world.SpawnEntityInWorld(spawnEntity);

Link to comment
Share on other sites

The SdtdConsole.instance.ExecuteSync () was just a test, I removed that.

 

I have now tested several things and again after several times through the loop he has brought me this mistake.

 

I then tested everything and checked it line by line,

as soon as I add this line:

Entity spawnEntity = EntityFactory.CreateEntity(entityID, vector3_block_pos);

, the error comes.

 

Here is my New code:

 

                    foreach (KeyValuePair<string, int> kvp in pos)
                   {
                       Entities = GameManager.Instance.World.Entities.list;
                       string[] block_pos = kvp.Key.Split(',');
                       int.TryParse(block_pos[0], out int pos_x);
                       int.TryParse(block_pos[1], out int pos_y);
                       int.TryParse(block_pos[2], out int pos_z);

                       var vector3_pos = new Vector3((float)pos_x, (float)pos_y, (float)pos_z);

                       World world = GameManager.Instance.World;
                       int entityID = EntityGroups.GetRandomFromGroup("Z_Spawner");

                       if (entityID > 0)
                       {
                           Entity spawnEntity = EntityFactory.CreateEntity(entityID, vector3_pos);
                           spawnEntity.SetSpawnerSource(EnumSpawnerSource.Dynamic);
                           world.SpawnEntityInWorld(spawnEntity);
                       }
                       Thread.Sleep(5000);
                   }

Link to comment
Share on other sites

The SdtdConsole.instance.ExecuteSync () was just a test, I removed that.

 

I have now tested several things and again after several times through the loop he has brought me this mistake.

 

I then tested everything and checked it line by line,

as soon as I add this line:

Entity spawnEntity = EntityFactory.CreateEntity(entityID, vector3_block_pos);

, the error comes.

 

Here is my New code:

 

                    foreach (KeyValuePair<string, int> kvp in pos)
                   {
                       Entities = GameManager.Instance.World.Entities.list;
                       string[] block_pos = kvp.Key.Split(',');
                       int.TryParse(block_pos[0], out int pos_x);
                       int.TryParse(block_pos[1], out int pos_y);
                       int.TryParse(block_pos[2], out int pos_z);

                       var vector3_pos = new Vector3((float)pos_x, (float)pos_y, (float)pos_z);

                       World world = GameManager.Instance.World;
                       int entityID = EntityGroups.GetRandomFromGroup("Z_Spawner");

                       if (entityID > 0)
                       {
                           Entity spawnEntity = EntityFactory.CreateEntity(entityID, vector3_pos);
                           spawnEntity.SetSpawnerSource(EnumSpawnerSource.Dynamic);
                           world.SpawnEntityInWorld(spawnEntity);
                       }
                       Thread.Sleep(5000);
                   }

 

I would suggest printing out using Debug.Log() the values of the vector3_pos and the entityID you are getting. Maybe a pattern will emerge.

Link to comment
Share on other sites

Here is the log,

for the data:

 

New Loop --> this is the ( 11 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-37.0, 49.0, 511.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = 551429383

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombieCrawl, name=zombieSteveCrawler, id=407]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 12 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-29.0, 49.0, 512.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = 700142925

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieSteve, id=408]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 13 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-40.0, 49.0, 524.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = 1862556927

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieDarlene, id=409]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 14 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-37.0, 49.0, 511.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = 684120619

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieBiker, id=410]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 15 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-29.0, 49.0, 512.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = 700142925

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieSteve, id=411]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 16 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-40.0, 49.0, 524.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = 1957301067

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieStripper, id=412]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 17 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-37.0, 49.0, 511.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = -428337742

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieBoe, id=413]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 18 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-29.0, 49.0, 512.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = -428330054

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieJoe, id=414]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 19 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-40.0, 49.0, 524.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = 700142925

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieSteve, id=415]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 20 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-37.0, 49.0, 511.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = 551429383

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombieCrawl, name=zombieSteveCrawler, id=416]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 21 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-29.0, 49.0, 512.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = 817467356

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieYo, id=417]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 22 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-40.0, 49.0, 524.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = 1862556927

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieDarlene, id=418]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 23 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-37.0, 49.0, 511.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = 817467356

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieYo, id=419]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 24 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-29.0, 49.0, 512.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = 817467356

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieYo, id=420]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 25 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-40.0, 49.0, 524.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = 817467356

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieYo, id=421]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 26 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-37.0, 49.0, 511.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = 350518692

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieOldTimer, id=422]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

2019-04-20T00:01:47 272.876 INF VehicleManager Save
New Loop --> this is the ( 27 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-29.0, 49.0, 512.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = -428327171

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieMoe, id=423]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 28 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-40.0, 49.0, 524.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = 700142925

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieSteve, id=424]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 29 ) run

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the vector3_pos = (-37.0, 49.0, 511.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the entityID = 700142925

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 3:this is the spawnEntity = [type=EntityZombie, name=zombieSteve, id=425]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the ( 30 ) Loop

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

New Loop --> this is the vector3_pos = (-29.0, 49.0, 512.0)

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 1: this is the entityID = -428327171

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Step 2: this is the spawnEntity = [type=EntityZombie, name=zombieMoe, id=426]

(Filename: ./Runtime/Export/Debug.bindings.h Line: 43)

Receiving unhandled NULL exception
#0  0x007f6d728a3700 in _L_unlock_13
#1  0x007f6d728a3d90 in operator new[](unsigned long, std::nothrow_t const&)
#2  0x007f6d728a3e00 in operator new[](unsigned long, std::nothrow_t const&)
#3  0x007f6d728a3e40 in operator new[](unsigned long, std::nothrow_t const&)
#4  0x007f6d728a3e80 in operator new[](unsigned long, std::nothrow_t const&)
#5  0x007f6d728a3ef0 in operator new[](unsigned long, std::nothrow_t const&)
#6  0x007f6d728a3f00 in operator new[](unsigned long, std::nothrow_t const&)
#7  0x007f6d728a3f20 in start_thread
#8  0x007f6d728a3fc0 in clone

Link to comment
Share on other sites

Check the KVP key to see if it's null when looping.

 

You shouldn't sleep the main thread as that will freeze your whole game and you may be getting physics errors because of the time jumps.

 

If this isn't the main thread that could be the problem and you're getting cross-threading issues.

 

Use an coroutine if you want to delay the spawn or something that tracks the time on each frame.

 

I posted an example a while ago: https://7daystodie.com/forums/showthread.php?40959

 

Cheers,

 

Hal

Link to comment
Share on other sites

I tested everything and all the values ​​have an entry,

I have also tested their function and run multiple times

the same error has come.

 

On the stable 16.4 is my option working fine, not on this stable.

 

I'll give you the 2 functions that are responsible.

 

The Timer class this is calling all:

using System.Timers;

namespace test
{
   class Class1
   {
       private static bool timer1Running = false;

       private static System.Timers.Timer t1 = new System.Timers.Timer();
       public static int z_spawn = 0;

       public static void Timer_Start()
       {
           if (!timer1Running)
           {
               timer1Running = true;
               t1.Interval = 1000;
               t1.Start();
               t1.Elapsed += new ElapsedEventHandler(Init);
           }
       }


       public static void Init(object sender, ElapsedEventArgs e)
       {
           if (Spawner.IsEnabled)
           {
               z_spawn++;
               if (z_spawn >= 15) //if (_auto_block_spawner >= Auto_Block_spawner * 60)
               {
                   z_spawn = 0;
                   if (!Spawner.auto_laeuft)
                   {
                       //Spawner.Auto_spawn();

                       GameManager.Instance.StartCoroutine(Spawner.StartSpawn());
                   }
               }
           }
           else
           {
               z_spawn = 0;
           }

       }
   }
}

 

The spawner class:

 

your option

        public static IEnumerator StartSpawn()
       {
           if (block_pos.Count > 0 && ConnectionManager.Instance.ClientCount() > 0)
           {
               if (!auto_laeuft)
               {
                   auto_laeuft = true;
                   int x = -40;
                   int y = 49;
                   int z = 524;

                   World world = GameManager.Instance.World;
                   EntityPlayer _player = world.GetClosestPlayer(new Vector3((float)x, (float)y, (float)z), 75, false);
                   if (_player != null && _player.IsSpawned() && !_player.IsDead())
                   {
                       System.Random zufall = new System.Random();
                       int number = zufall.Next(4, 5);
                       auto_laeuft = true;
                       object wait = new WaitForSeconds(number);
                       string entityGroup = "ZombiesAll";

                       long _chunkval = 0;
                       int spawnCount = 0;

                       for (spawnCount = 0; spawnCount < 1; spawnCount++)
                       {
                           int entityID = EntityGroups.GetRandomFromGroup(entityGroup);
                           Entity spawnEntity = EntityFactory.CreateEntity(entityID, new Vector3((float)x, (float)y, (float)z));
                           spawnEntity.SetSpawnerSource(EnumSpawnerSource.StaticSpawner, _chunkval, entityGroup);
                           GameManager.Instance.World.SpawnEntityInWorld(spawnEntity);
                           spawnEntity = null;
                           yield return wait;
                       }
                       auto_laeuft = false;
                       yield return null;
                   }
               }
           }
       }

My option:

        public static void Auto_spawn()
       {
           if (pos.Count > 0 && ConnectionManager.Instance.ClientCount() > 0)
           {
               foreach (KeyValuePair<string, int> kvp in pos)
               {
                   if (kvp.Key == null) continue;
                   string[] pos = kvp.Key.Split(',');
                   int.TryParse(pos[0], out int pos_x);
                   int.TryParse(pos[1], out int pos_y);
                   int.TryParse(pos[2], out int pos_z);

                   var vector3_pos = new Vector3((float)pos_x, (float)pos_y, (float)pos_z);
                   World world = GameManager.Instance.World;
                   int entityID = EntityGroups.GetRandomFromGroup("ZombiesAll");
                   Entity spawnEntity = EntityFactory.CreateEntity(entityID, vector3_pos);
                   spawnEntity.SetSpawnerSource(EnumSpawnerSource.Dynamic);
                   world.SpawnEntityInWorld(spawnEntity);
               }
           }
       }

 

All these functions bring the same error after some time, not for the first try, but after a short time.

Receiving unhandled NULL exception
#0  0x007fb48489f340 in _L_unlock_13
#1  0x007fb48489f9d0 in operator new[](unsigned long, std::nothrow_t const&)
#2  0x007fb48489fd50 in operator new[](unsigned long, std::nothrow_t const&)
#3  0x007fb48489fe00 in operator new[](unsigned long, std::nothrow_t const&)
#4  0x007fb48489fe40 in operator new[](unsigned long, std::nothrow_t const&)
#5  0x007fb48489fe80 in operator new[](unsigned long, std::nothrow_t const&)
#6  0x007fb48489fef0 in operator new[](unsigned long, std::nothrow_t const&)
#7  0x007fb48489ff00 in operator new[](unsigned long, std::nothrow_t const&)
#8  0x007fb48489ff20 in start_thread
#9  0x007fb48489ffc0 in clone

Link to comment
Share on other sites

If I run

GameManager.Instance.StartCoroutine(Spawner.StartSpawn());

via a chat command or something, then the spawn will run without an error, but if I do this over the timer then the error will come after a while. How can I solve this ?

 

I test it now over 4 hours, without result.

 

Can someone help me ?

Link to comment
Share on other sites

I start because of this timer:

 

public void GameAwake()
{
Timers.TimerStart();
}

 

Here The timer class:

using System.Timers;

namespace test
{
   class Timers
   {
       private static bool timer1Running = false;

       private static System.Timers.Timer t1 = new System.Timers.Timer();
       public static int z_spawn = 0;

       public static void Timer_Start()
       {
           if (!timer1Running)
           {
               timer1Running = true;
               t1.Interval = 1000;
               t1.Start();
               t1.Elapsed += new ElapsedEventHandler(Init);
           }
       }


       public static void Init(object sender, ElapsedEventArgs e)
       {
           if (Spawner.IsEnabled)
           {
               z_spawn++;
               if (z_spawn >= 15) //if (_auto_block_spawner >= Auto_Block_spawner * 60)
               {
                   z_spawn = 0;
                   if (!Spawner.auto_laeuft)
                   {
                       //Spawner.Auto_spawn();

                       GameManager.Instance.StartCoroutine(Spawner.StartSpawn());
                   }
               }
           }
           else
           {
               z_spawn = 0;
           }

       }
   }
}

 

The rest of the code in the timer works perfectly,

only spawn that.

This is a .dll file like this one.

 

https://7daystodie.com/forums/showthread.php?32862-Server-Tools

Link to comment
Share on other sites

I'd get rid of the timer object. I'm not sure how Mono handles it but I'm pretty sure you'll be cross-threading the call into the spawn routine which could cause issues.

 

You can use the coroutine I posted above to be a timer as well. Using the yield return WaitForSeconds() causes that code to pause for the time specified without stopping the whole thread.

 

Cheers,

 

Hal

Link to comment
Share on other sites

EntityFactory.CreateEntity can not be run in a subthread

 

Is this for an api mod?

 

When adding spawns you can add them to a Queue list, this can be the subthreaded part

 

In the GameUpdate I checks if certain conditions are met like enough time since the last run has passed so it doesnt deadlock the main thread trying to process spawns

 

If the conditions are met and a spawn is required it then calls the CreateEntity in the main thread using the spawn data created previously

 

some code samples:

 

   private static void GameStartDone()
   {
     IsAwake = true;
   }

   private static void GameUpdate()
   {
     if (IsAwake)
     {
       EntitySpawner.ProcessSpawnQueue();
     }
   }

           EntitySpawner.SpawnQueue.Enqueue(new Spawn
           {
             EntityClassId = classId, // "EntityClassName".GetHashCode();
             SpawnPos = position,
             TargetPos = position
           });

 class EntitySpawner {
   public static readonly Queue<Spawn> SpawnQueue = new Queue<Spawn>();
   private static long _lastTick;

   public void ProcessSpawnQueue()
   {
     // 10,000,000 ticks per second
     if (DateTime.UtcNow.Ticks <= _lastTick + 500000L) { return; }

     _lastTick = DateTime.UtcNow.Ticks;
     if (SpawnQueue.Count == 0) { return; }

     lock (SpawnQueue)
     {
       var sw = new MicroStopwatch();
       for (; SpawnQueue.Count > 0 && sw.ElapsedTicks < 250000L;)
       {
         try
         {
           //if obey maxspawns use below
           //if (GameStats.GetInt(EnumGameStats.EnemyCount) >= GamePrefs.GetInt(EnumGamePrefs.MaxSpawnedZombies)) return;

           var world = GameManager.Instance.World;
           var spawn = SpawnQueue.Dequeue();
           var pos = new Vector3(0,40,0);

             var entity = EntityFactory.CreateEntity(spawn.EntityClassId, pos) as EntityEnemy;
             if (entity == null) { continue; }

             world.Entities.Add(entity.entityId, entity);

             if (entity.IsEntityAttachedToChunk && !entity.addedToChunk)
             {
               var chunk = world.GetChunkFromWorldPos(entity.GetBlockPosition()) as Chunk;
               chunk?.AddEntityToChunk(entity);
             }
             world.audioManager?.EntityAddedToWorld(entity, world);
             world.entityDistributer.Add(entity);
             entity.Spawned = true;
             world.aiDirector.AddEntity(entity);
             entity.SetInvestigatePosition(new Vector3(spawn.TargetPos.x, spawn.TargetPos.y, spawn.TargetPos.z), 6000);
         }
         catch (Exception e)
         {
           Log.Out($"Error in {GetType().Name}.{MethodBase.GetCurrentMethod().Name}: {e}");
         }
       }
       sw.Stop();
     }
   }
 }

Link to comment
Share on other sites

@ HAL9000

You mean that I create an endless loop,

which then always stop with yield return WaitForSeconds()?

Example:

 

object wait_2 = new WaitForSeconds(2);
while (true)
{
yield return wait_2;
}

 

@StompyNZ

Yes this is for a api mod.

Thanks, I will test it and then get back to you.

Link to comment
Share on other sites

You can stop coroutines by using "yield break" or just letting the method reach the end of the code (as long as you've yielded at some point in the method). In the example in the other post it spawns 10 entities then the routine ends

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...