Announcement

Collapse
No announcement yet.

Help with C#/.dll Zombie Spawning Code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Help with C#/.dll Zombie Spawning Code

    First off, would it be possible to get a C# or .DLL Modding Sub Forum? Good idea or not because of cheaters?


    EDIT: Original question answered. THANKS HAL! Additional C# questions in the thread.

    I'm writing another mod to dmustanger's Server Tools. This will add another forward slash command /spawn that will spawn zombies at (for now) hardcoded x, y, z coords inside our server's arena.

    Everything about the code below works except for the 2 second wait time. Currently the command is sent, it waits 20 seconds, and then spawns all 10 zombies at once. I'd like to see it spawn a zombie every 2 seconds. I've tried Thread.Sleep and System.Threading.Timer and they both have the same outcome. I'm guessing it has something to do with my dll reporting back to the main game code before it actually spawns.

    I'm sort of new to this, but I'm learning.

    Any help is appreciated!

    Spoiler: 
    Code:
    namespace ServerTools
    {
        public class Arena
        {
    
            public static void StartSpawn()
            {
                int x = 1000;
                int y = 73;
                int z = 1000;
                string entityGroup = "ZombiesWasteland";
    
                long _chunkval = 0;
                int spawnCount = 0;
    
                for (spawnCount = 0; spawnCount < 10; 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;
                    Thread.Sleep(2000);
                }
            }
    Last edited by schwanz9000; 07-12-2016, 12:39 PM.

    #2
    Originally posted by schwanz9000 View Post
    First off, would it be possible to get a C# or .DLL Modding Sub Forum? Good idea or not because of cheaters?

    I'm writing another mod to dmustanger's Server Tools. This will add another forward slash command /spawn that will spawn zombies at (for now) hardcoded x, y, z coords inside our server's arena.

    Everything about the code below works except for the 2 second wait time. Currently the command is sent, it waits 20 seconds, and then spawns all 10 zombies at once. I'd like to see it spawn a zombie every 2 seconds. I've tried Thread.Sleep and System.Threading.Timer and they both have the same outcome. I'm guessing it has something to do with my dll reporting back to the main game code before it actually spawns.

    I'm sort of new to this, but I'm learning.

    Any help is appreciated!

    Spoiler: 
    Code:
    namespace ServerTools
    {
        public class Arena
        {
    
            public static void StartSpawn()
            {
                int x = 1000;
                int y = 73;
                int z = 1000;
                string entityGroup = "ZombiesWasteland";
    
                long _chunkval = 0;
                int spawnCount = 0;
    
                for (spawnCount = 0; spawnCount < 10; 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;
                    Thread.Sleep(2000);
                }
            }
    Why don't you use Mortelentus sdx spawner block. You place it as a block in your arena certain sections and it will spawn zs automatically. You can set it to how ever you like in the configs

    Comment


      #3
      Originally posted by stallionsden View Post
      Why don't you use Mortelentus sdx spawner block. You place it as a block in your arena certain sections and it will spawn zs automatically. You can set it to how ever you like in the configs
      Thanks for the suggestion, but I want to avoid SDX mods and having to have my players download and install files.

      Comment


        #4
        Not sure how Server Tools actually works but perhaps you can try with async/await with Task.Delay()?

        Comment


          #5
          If you're running this code on the main thread then that would be expected behaviour. What you're doing is spawning a zombie, freezing the game for two seconds, then looping. So to the game it's like no time has passed at all and you'll get all the zombies at once. You said you're learning so here's rule one of game dev: never, ever, EVER pause the main loop ^^ If you did that in SP you'd just freeze the entire game for 20 seconds. You don't notice it on a dedi because your client isn't on that computer but the server is basically doing nothing for 20 seconds.

          The easiest (not necessarily best) way is to use Unity's co-routine feature. Have a read up on them to see the pros and cons.

          Change your spawn method to this:

          Code:
            public static IEnumerator StartSpawn()
              {
          
                  object wait = new WaitForSeconds(2);
                  int x = 1000;
                  int y = 73;
                  int z = 1000;
                  string entityGroup = "ZombiesWasteland";
          
                  long _chunkval = 0;
                  int spawnCount = 0;
          
                  for (spawnCount = 0; spawnCount < 10; 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;
                  }
          
                  yield return null;
          
              }
          And then wherever you call the method, call it like this:

          Code:
          GameManager.Instance.StartCoroutine(Arena.StartSpawn());

          Comment


            #6
            Thanks for pointing me in the right direction Hal! Works great!

            Comment


              #7
              Can a console command be invoked without a player online?

              I'm trying to use the following...

              SdtdConsole.Instance.ExecuteSync("shutdown", _cInfo);

              ...but it will not work without a player online.

              What else can I do here?

              Comment


                #8
                Have a look at the shutdown command class in a disassembler.

                All it's doing is calling Application.Quit() so use that instead to turn the server off.

                Comment


                  #9
                  +1

                  Besides that if you call ExecuteSync the command will definitely *always* be executed. That's why it's sync (Make sure you only do this from the main thread though)
                  So in this case I'd assume that your code that makes this call is simply not executed itself.

                  Comment


                    #10
                    Originally posted by stallionsden View Post
                    Why don't you use Mortelentus sdx spawner block. You place it as a block in your arena certain sections and it will spawn zs automatically. You can set it to how ever you like in the configs
                    You REALLY need to stop suggesting SDX as a solution for everything. Im a huge fan and user of it, but for general use on servers SDX is NOT cheap or in any way possible for a good majority of server renters.

                    Comment


                      #11
                      Originally posted by JaxTeller718 View Post
                      You REALLY need to stop suggesting SDX as a solution for everything. Im a huge fan and user of it, but for general use on servers SDX is NOT cheap or in any way possible for a good majority of server renters.
                      Ummm i was more suggesting the spawner. And how is it not cheap lol. And it was a complete different question anyways lol. But i was more saying also it could of assisted with what sdx can do it might be able to. Besides swanz was ok with me suggesting. He didnt want or need it and thats cool to.

                      And that reply was like 3 months ago lol. I will suggest what i like if it a good idea then cool of not well i offered help whether it was useful or not

                      Comment


                        #12
                        Originally posted by HAL9000 View Post
                        Have a look at the shutdown command class in a disassembler.

                        All it's doing is calling Application.Quit() so use that instead to turn the server off.
                        Once again, Hal to the rescue! I don't know why I didn't think about that in the first place. Doh!

                        What is your go to disassembler? I've used ILSpy. Does anyone have or been able to put together a clean deobfuscated Assembly-CSharp file? I haven't seen much about it since this.

                        Originally posted by Alloc View Post
                        +1

                        Besides that if you call ExecuteSync the command will definitely *always* be executed. That's why it's sync (Make sure you only do this from the main thread though)
                        So in this case I'd assume that your code that makes this call is simply not executed itself.
                        I thought it wasn't working because cInfo was null. I'll have to check upstream calls. Works great if I'm logged onto the server though.



                        Basically trying to add my own customizable automatic server restart class. Blue Fang servers have a server side script that automatically restarts the server. This will be used to reboot the server once or twice a day on a set schedule.

                        When the server boots up, it starts a thread with a timer for x amount of time setup in the config file. If someone is online, it will give a warning message at the 10 minutes remaining mark and every minute there after. Then it will warn at the 30, 10, 5, 4, 3, 2, 1 second marks. If no one is online, I want it to shutdown and restart on it's own.

                        This is being added to my branch of dmustanger's ServerTools.

                        Comment


                          #13
                          Originally posted by schwanz9000 View Post
                          Once again, Hal to the rescue! I don't know why I didn't think about that in the first place. Doh!

                          What is your go to disassembler? I've used ILSpy. Does anyone have or been able to put together a clean deobfuscated Assembly-CSharp file? I haven't seen much about it since this.
                          My favourite is DotPeek, it has a very handy search function on Control+T that halves the amount of time it takes me to track things down in the code. Very handy bit of kit.

                          If you want to de-obfuscate the DLL check out de4dot. That gave me the best results. I don't usually de-obfuscate these days as I've mainly grown accustomed to the source for most things but if I do want to see a cleaner version I run it through SDX. Dominix de-obfuscates the DLL and added a bit of code that adds all the string literals back in so you don't have to mess around converting all that A.O(234536) nonsense. Absolutely fantastic for increasing readability.

                          Comment


                            #14
                            Originally posted by HAL9000 View Post
                            My favourite is DotPeek, it has a very handy search function on Control+T that halves the amount of time it takes me to track things down in the code. Very handy bit of kit.

                            If you want to de-obfuscate the DLL check out de4dot. That gave me the best results. I don't usually de-obfuscate these days as I've mainly grown accustomed to the source for most things but if I do want to see a cleaner version I run it through SDX. Dominix de-obfuscates the DLL and added a bit of code that adds all the string literals back in so you don't have to mess around converting all that A.O(234536) nonsense. Absolutely fantastic for increasing readability.
                            I don't care that this is old, doing a google search brought this up and to me that makes it relevant. especially since I don't think the info it has is out-dated.

                            Hal, I wanted to ask.

                            1) When you use de4dot on 7Days, do you apply any of the options de4dot has? or does it only need me to drag the dll onto the .exe?
                            2) Are you saying that merely installing SDX also cleans up the code? should I still do both de4dot and SDX?
                            3) What is Dominix?

                            Comment


                              #15
                              Originally posted by layarion View Post
                              I don't care that this is old, doing a google search brought this up and to me that makes it relevant. especially since I don't think the info it has is out-dated.

                              Hal, I wanted to ask.

                              1) When you use de4dot on 7Days, do you apply any of the options de4dot has? or does it only need me to drag the dll onto the .exe?
                              2) Are you saying that merely installing SDX also cleans up the code? should I still do both de4dot and SDX?
                              3) What is Dominix?
                              A non-Hal answer:

                              SDX Launcher can run de4dot for you. Just do a SDX Build without having any selected mods, and it will clean it up for you.

                              Dominix is the original creator of SDX.

                              Comment

                              Working...
                              X