Ironman for zombie, need event help

Ironman for zombie, need event help

Postby ismellike » 09 Mar 2013, 17:42

Ok so I pretty much have the ironman for zombies coded out but I cant figure the events out.

I keep getting these errors and I dont know how to fix them.

Spoiler:


Here is the current code-

Spoiler:
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: Ironman for zombie, need event help

Postby Ultima » 06 Apr 2013, 10:44

viewtopic.php?p=4569#p4569

You need to add the _mcdzienny.dll as reference and then add on top "use mcdzienny"
User avatar
Ultima
 
Posts: 953
Joined: 19 Aug 2011, 23:45

Re: Ironman for zombie, need event help

Postby ismellike » 07 Apr 2013, 20:53

It wasnt that, it turns out I had replaced object sender with player p,
But now it says no viable way to override *event*
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: Ironman for zombie, need event help

Postby dzienny » 11 Apr 2013, 17:31

ismellike wrote:It wasnt that, it turns out I had replaced object sender with player p,
But now it says no viable way to override *event*


Also, it should be:
Code: Select all
InfectionSystem.InfectionSystem.RoundStart += RoundStart;


As the original class declaration is:
Code: Select all
namespace MCDzienny.InfectionSystem
{
    public static class InfectionSystem
    {
       ...
    }
}


So, you have to either add using MCDzienny.InfectionSystem; in the header of your class or reference the class objects with InfectionSystem.InfectionSystem.
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: Ironman for zombie, need event help

Postby ismellike » 13 Apr 2013, 00:58

omg Im so dumb, I had "public override void RoundStart"

all I had to do was remove the override part, anyways here is the zman.

Code: Select all
using System;
using MCDzienny.InfectionSystem;
using System.Threading;
using System.Collections.Generic;

namespace MCDzienny
{
    public class CmdZman : Command
    {
        private static List<Player> zman = new List<Player>();
        public override string name { get { return "zman"; } }
        public override string shortcut { get { return "zm"; } }
        public override string type { get { return "other"; } }
        public override bool museumUsable { get { return true; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }
        public CmdZman() { }
        /*                              *
         * ~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
         * Made by Ismellike t(*_*t)    *
         *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
         */
        public override void Use(Player p, string message)
        {
            if (zman.Contains(p))
            {
                Player.SendMessage(p, "You already took the zman challenge");
                return;
            }
            foreach (Player check in Player.players)
            {
                if (check.isZombie == true)
                {
                    Player.SendMessage(p, "The infection has already begun.");
                    return;
                }
            }
            zman.Add(p);
            Player.GlobalMessage(p.color + p.name + Server.DefaultColor + " has taken the zman challenge!");
            Player.SendMessage(p, "Watch those zombies closely!");

        }
        public override void Init()
        {
            InfectionSystem.InfectionSystem.RoundStart += RoundStart;
            InfectionSystem.InfectionSystem.AnnounceWinners += InfectionRoundEnded;

        }
        public void RoundStart(Object sender, RoundStartEventArgs e)
        {
            foreach (Player p in zman)
            {
                if (p.isZombie == true)
                {
                    zman.Remove(p);
                }
                Thread zThread = new Thread(new ThreadStart(delegate
                    {
                        while (zman.Contains(p))
                        {
                            if (p.isZombie == true || p.disconnected == true||p.IsRefree==true||p.level!=Server.InfectionLevel)
                            {
                                zman.Remove(p);
                                p.money = p.money - 10;
                                Player.GlobalMessage(p.color + p.PublicName + Server.DefaultColor + " has failed the zman challenge");
                                Player.SendMessage(p, "%aThe zombies have overpowered you! %4-10");
                            }
                        }
                    }));
                zThread.Start();
            }
        }
        public void InfectionRoundEnded(Object sender, InfectionSystem.AnnounceWinnersEventArgs e)
        {
            foreach (Player p in zman)
            {
                p.money = p.money + 15;
                zman.Remove(p);
                Player.SendMessage(p, "%aYou gained an extra 15 " + Server.moneys + " from zman");
            }
        }
        public override void Help(Player p)
        {
            Player.SendMessage(p, "/zman, take the zman challenge if you dare.");
        }
    }
}
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: Ironman for zombie, need event help

Postby dzienny » 13 Apr 2013, 20:28

ismellike wrote:omg Im so dumb, I had "public override void RoundStart"


Such things happen in the beginning :)
Well I'm curious how the command works, I will test it soon. Anyway if it's in a working state, you should post in the Actual Commands section otherwise some people may never find it;)
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: Ironman for zombie, need event help

Postby ismellike » 14 Apr 2013, 00:15

Well I was testing it and it looks like it stops the round voting and returns this error.

Code: Select all
Type: InvalidOperationException
Source: mscorlib
Message: Collection was modified; enumeration operation may not execute.
Target: ThrowInvalidOperationException
Trace:    at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at MCDzienny.CmdZman.InfectionRoundEnded(Object sender, AnnounceWinnersEventArgs e)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at MCDzienny.InfectionSystem.InfectionSystem.OnAnnounceWinners(Object sender, AnnounceWinnersEventArgs e)
   at MCDzienny.InfectionSystem.InfectionSystem.AnnounceAndAwardWinners()
   at MCDzienny.InfectionSystem.InfectionSystem.EndInfectionRound()
   at MCDzienny.InfectionSystem.InfectionUtils.RoundTimeManager(Object sender, ElapsedEventArgs e)


Now that I think back on it, it may just be me updating the command, but idk.


A few people told me that they did get some money from it.
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: Ironman for zombie, need event help

Postby dzienny » 14 Apr 2013, 12:34

A collection that is looped with foreach cannot be modified, or else it will throw this error. This requirement is not met in case of Player.players as it is constantly modified, whenever a player joins or disconnects. There are a few solutions for this problem. You can create a copy of Player.players and iterate over the copy, in this case you have to add List<Player> copyOfPlayers = new List<Player>(Player.players);. This way, if some players join or leave the copyOfPlayers will stay the same. It may lead to the situation when you iterate over a player that is already disconnected, or you omit one that has recently connected, but in most situations it shouldn't be a problem. The other solution is to use Player.players.ForEach that doesn't require the collection to be immutable during the iteration. However some other quirks may occasionally happen when the collection is modified, for example iterating over the same player twice, or omitting an other. The third solution would include temporarily locking the collection to make sure it's not modified during the iteration, although this option is not currently available. I would have to significantly modify the code, still I may add it in some time.
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27


Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 5 guests