if (MapEngine.MapArray.ReadItem(Posx, (Posy + 1)) = "Empty")
The '=' should be '=='. The = sign is used to assign things, while the == operator is used to check if they are equal.
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
if (MapEngine.MapArray.ReadItem(Posx, (Posy + 1)) = "Empty")
using System;public class Character{ public event EventHandler Death; public void OnDeath(object victim, EventArgs e) { Character a = (Character)victim; // unboxing Console.WriteLine("{0} has defeated {1}", this.name, // this.name should be the killer a.Name); // a is the invoker of the function } int hitpoints; // The amount of life a character has string name; // The characters game name public string Name // set and retrieve the characters name { get { return name; } set { name = value; } } public int Life // set and retrieve the character's life { // points. If hitpoints is less or equal to 0 get // invoke the The killer's death routine { return hitpoints; } set { hitpoints = value; if (hitpoints <= 0) { if (Death != null) Death(this, null); } } }}class program{ static void Main() { Character villan = new Character(); Character hero = new Character(); villan.Name = "Shredder"; // set names hero.Name = "Ryu"; hero.Death += villan.OnDeath; // Swap eath other's death routine villan.Death += hero.OnDeath; hero.Life = -3; // Manually kill our hero, but pretend // the villan did it. }}
Quote: Original post by Knuckler
I was trying to come up with a more intuitive for handling a character's death in my battle system using event handlers. I came up with this:
*** Source Snippet Removed ***
I was just trying this out to test how well I knew events and event handles. It works and all. But this is just bad design, right?
public void TakeDamage(Character attacker, int amount) { hitPoints = Math.Max(hitPoints - amount, 0); if (hitPoints == 0) { ++attacker.KillCount; Console.WriteLine("{0} killed {1}, he has killed {2} characters.", attacker, this, attacker.KillCount); if(Died != null) Died(this, attacker); } }
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
Quote: Original post by Knuckler
I had a feeling it didn't seem right. I see it now. The character handler would be better suited for the surrounding area or the game battle system. Each character would register their "death" handler with the battle system or area so that when invoked the system could use that info to remove the player for the area or game. Characters shouldn't directly know about one another, but through the game system.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
Quote: Original post by WashuQuote: Original post by Knuckler
I had a feeling it didn't seem right. I see it now. The character handler would be better suited for the surrounding area or the game battle system. Each character would register their "death" handler with the battle system or area so that when invoked the system could use that info to remove the player for the area or game. Characters shouldn't directly know about one another, but through the game system.
Maybe, or maybe not. Events aren't always the proper way to do things. I would instead probably just have the character notify the system on his own death. It depends on exactly what you're trying to do as well.