Calling a method from every class that implements an interface - c#

I'm working on a game engine. Now assume we have a Game class:
using System;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Drawing;
using static OpenTK.Graphics.OpenGL.GL;
namespace Test2
{
class Game : GameWindow
{
public Game(int width, int height)
:base(width, height)
{
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
}
}
}
And we have a medium in which to call a method for every frame.
Now assume the person creating a game, creates a file called ball_behaviour script:
using System;
using System.Drawing;
namespace Test2.Project.Behaviour
{
class Ball
{
public void Update()
{
}
public void init()
{
A2D.Background = Color.CornflowerBlue;
}
public void preInit()
{
}
}
}
And assume there are other files like it, which have the methods Update, and init. How can I call these methods from protected override void OnRenderFrame for example, in every class that's registered under Test2.Project.Behaiviour namespace? Thank you so much in advanced.
EDIT:The GameWindow class comes from OpenTK, which is the API I'm using to interface c# with OpenGL.
EDIT #2: Kind of like the way Unity does it.

you could use a virtual void that you than override which would be easier and faster to implement,have done this thing yesterday

Related

MRTK error: does not implement abstract member 'InputSystemGlobalHandlerListener.UnregisterHandlers()

I'm trying to test a very simple script to learn to use the input-action system in MRTK. I use a standard function to read the input, from a tutorial and also found on the MRTK documentation here: https://microsoft.github.io/MixedRealityToolkit-Unity/api/Microsoft.MixedReality.Toolkit.Input.IMixedRealityPointerHandler.html
However, the code below gives me a error that I cannot figure out....
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Microsoft.MixedReality.Toolkit.Input;
public class GameManager : BaseInputHandler, IMixedRealityPointerHandler
{
public MixedRealityInputAction Showmenu;
public Text ButtonText;
int clicktimes = 0;
// Start is called before the first frame update
public void ButtonClicker()
{
clicktimes++;
ButtonText.text = "You clicked: " + clicktimes.ToString();
}
public void OnPointerUp(MixedRealityPointerEventData pd)
{
}
public void OnPointerDown(MixedRealityPointerEventData pd)
{
}
public void OnPointerClicked(MixedRealityPointerEventData pd)
{
}
public void OnPointerDragged(MixedRealityPointerEventData pd)
{
}
}
The error is: CS0534 'GameManager' does not implement inherited abstract member 'InputSystemGlobalHandlerListener.UnregisterHandlers()'
Can't figure it out... any help appreciated!!!
Stupid me.... here is the answer in case it's useful to someone:
protected override void UnregisterHandlers()
{
}
protected override void RegisterHandlers()
{
}

Unity/C# Trouble with virtual and override

So I am trying to create my own methods (like Update, Start etc)
Basically, something that will detect input but with a bool method. More like an "OnKeyDown".
I get no errors but nothing happens, here are my classes:
using UnityEngine;
using System.Collections;
public class BeastScript : MonoBehaviour
{
void Update()
{
#region INPUT
//KEYS
if (Input.anyKeyDown)
{
OnKeyDown(Input.inputString);
}
if (Input.anyKey)
{
OnKeyHold(Input.inputString);
}
//MOUSE
if (Input.GetMouseButtonDown(0))
{
OnMouseClick(0);
}
if (Input.GetMouseButtonDown(1))
{
OnMouseClick(1);
}
if (Input.GetMouseButtonDown(2))
{
OnMouseClick(2);
}
if (Input.GetMouseButton(0))
{
OnMouseClickHold(0);
}
if (Input.GetMouseButton(1))
{
OnMouseClickHold(1);
}
if (Input.GetMouseButton(2))
{
OnMouseClickHold(2);
}
if (Input.GetMouseButtonUp(0))
{
OnMouseRelease(0);
}
if (Input.GetMouseButtonUp(1))
{
OnMouseRelease(1);
}
if (Input.GetMouseButtonUp(2))
{
OnMouseRelease(2);
}
#endregion INPUT
}
public virtual void OnKeyDown(string key) { }
public virtual void OnKeyHold(string key) { }
public virtual void OnMouseClick(int button) { }
public virtual void OnMouseClickHold(int button) { }
public virtual void OnMouseRelease(int button) { }
}
Here is the derived
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using Beast;
using Beast.BUI;
using Beast.Extensions;
using System;
using UnityEngine.Events;
using Beast.Web;
public class Tester : BeastScript {
void Start()
{
}
void OnGUI ()
{
}
void Update ()
{
}
public override void OnMouseClick(int button)
{
base.OnMouseClick(button);
if (button == 0)
{
print("Works");
}
}
}
Make sure to call base.Update() if you are running script in the base class method Update.
void Update ()
{
base.Update();
// special Update code
}
When inheriting a class which's methods you want to override, you have to declare these methods virtual. In your example, both classes have a private void Update() (when not declaring an access modifier it is implicitly private).
This means, the inheriting class (Tester) does not even know that its base class already has an Update() method. Thus it will not override it, but hide it. Once Unity calls the Update() method of Tester, it will have absolutely no effect, as it's empty.
What you want is to have Tester's Update() method to include all the contents of BeastScript's Update() method, and optionally extend it. That can be accomplished like this:
public class BeastScript : MonoBehaviour
{
protected virtual void Update() // "protected" so subclasses can see it
{ // and "virtual" so subclasses can override it
if (Input.anyKeyDown)
...
}
}
public class Tester : BeastScript
{
protected override Update() // "protected" because overriding methods must not be less accessible than the method they override
{ // and "override" to specify that this method overrides a virtual method
// optional: do stuff before calling BeastScript.Update()
base.Update(); // this calls BeastScript.Update()
// optional: do stuff after calling BeastScript.Update()
}
}
You made an empty Update method that replaced the base Update method. Remove it in Tester class

How do you call in Libraries correctly?

I've looked around a lot.. and I haven't found anything to help me out.
This is the outcome I am looking for:
I am using XNA Game Studio 4.0, and I want to create my own library to use when making my game, so I don't have to rewrite code that I have previously used over and over every time I want to make a new game. So, I want to call in a library as a reference. My Library:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace iRaaptorLibraryXNA
{
public class XNALibrary
{
public static void consolePrint(string text)
{
var timeStamp = System.DateTime.Now.ToString(#"hh:mm:ss tt");
Console.WriteLine("[" + timeStamp + "] CONSOLE: " + text);
}
}
}
I use the consolePrint function a lot while debugging.
For example, if I wanted to print a line to show success, I'd like to be able to type
consolePrint("SUCCESS!")
and that prints it to console. I DONT want to have to repeatedly type in:
XNALibraryXNA.consolePrint("SUCCESS!");
I want to shorten it to just
consolePrint("SUCCESS!");
Here is my game code:
using iRaaptorLibraryXNA;
namespace xnaFun
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize()
XNALibrary.consolePrint("Initialized!");
}
protected override void LoadContent()
{
iRaaptorLibraryXNA.consolePrint("Loading content!"); // DOES PRINT TO CONSOLE
spriteBatch = new SpriteBatch(GraphicsDevice);
}
etc......
I hope I included enough information. ~iRaaptor
TL;DR I want to shorten
XMALibrary.consolePrint("text");
to
consolePrint("text");
Add this class to your library:
public class BaseGame : Microsoft.Xna.Framework.Game
{
protected void consolePrint(string message)
{
XNALibrary.consolePrint("Initialized!");
}
}
Your game class can then inherit from the basegame and use the consolePrint method:
public class MyGame : BaseGame
{
protected override void Initialize()
{
base.Initialize()
consolePrint("Initialized!");
}
}

Cascading Multiple Inheritance and Events

I really am not sure what else to google to try to find the answer to this, can anyone tell me the proper way to implement these events? In ClassOne the event is considered null, and I just don't get it ....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MultipleInheritance
{
static class Program
{
static void Main()
{
ClassThree cThree = new ClassThree();
cThree.fireEventOne += cThree_fireEventOne;
cThree.Start();
cThree.Start2();
cThree.Start3();
}
static void cThree_fireEventOne()
{
Console.WriteLine("one two three");
}
}
}
and here is the classes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultipleInheritance
{
public abstract class ClassOne
{
public delegate void EventOne();
public event EventOne fireEventOne;
public ClassOne()
{
}
public void Start()
{
fireEventOne();
}
public abstract void Start2();
}
public abstract class ClassTwo :ClassOne
{
public delegate void EventOne();
public event EventOne fireEventOne;
public override void Start2()
{
fireEventOne();
}
public abstract void Start3();
}
public class ClassThree :ClassTwo
{
public delegate void EventOne();
public event EventOne fireEventOne;
public override void Start3()
{
fireEventOne();
}
}
}
The problem is that you're redefining the following in each class:
public delegate void EventOne();
public event EventOne fireEventOne;
So when you call Start() you are trying to fire the event fireEventOne in the class ClassOne, but you've hooked up the shadowed event in ClassThree like this:
cThree.fireEventOne += cThree_fireEventOne;
I can see why you coded it that way. You get a compiler error when trying to directly invoke the event from a parent class. The correct way is to provide a protected method that you call in the parent class to fire the event.
Try writing your classes like this:
public abstract class ClassOne
{
public delegate void EventOne();
public event EventOne fireEventOne;
public ClassOne()
{ }
public void Start()
{
this.DoFireEventOne();
}
protected void DoFireEventOne()
{
var feo = fireEventOne;
if (feo != null)
{
feo();
}
}
public abstract void Start2();
}
public abstract class ClassTwo :ClassOne
{
public override void Start2()
{
this.DoFireEventOne();
}
public abstract void Start3();
}
public class ClassThree :ClassTwo
{
public override void Start3()
{
this.DoFireEventOne();
}
}
You create the fireEventOne event in each class. So, you have three events. But, the fireEventOne method is implemented on the first level (ClassOne). So, it will raise the delegate of the ClassOne class. Remove the fireEventOne event for the descendant classes.

Error: Expected class, delegate, enm, interface, or struct in C#

Error in line : public virtual void BuyFavoriteStuff()
Error : Expected class, delegate, enm, interface, or struct
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LearnAbstractClass
{
class Program
{
static void Main(string[] args)
{
}
}
class Shopper
{
private int TotalSpent=0, CreditLimit=10;
public void ShopTillYouDrop()
{
while (TotalSpent < CreditLimit)
BuyFavoriteStuff();
}
}
public virtual void BuyFavoriteStuff()
{
// No implementation here - we don’t know
// what our student likes to buy!
}
class ArtStudent : Shopper
{
public override void BuyFavoriteStuff()
{
BuyArtSupplies();
BuyBlackTurtlenecks();
BuyDepressingMusic();
}
private void BuyBlackTurtlenecks()
{}
private void BuyDepressingMusic()
{}
private void BuyArtSupplies()
{}
}
class EngineeringStudent : Shopper
{
public override void BuyFavoriteStuff()
{
BuyPencils();
BuyGraphingCalculator();
BuyPocketProtector();
}
private void BuyPencils()
{}
private void BuyGraphingCalculator()
{}
private void BuyPocketProtector()
{}
}
}
What is the wrong in implementation above?
your method:
public virtual void BuyFavoriteStuff()
{
// No implementation here - we don’t know
// what our student likes to buy!
}
it outside of the Shopper class
You have method that is outside of any class:
public virtual void BuyFavoriteStuff()
{
// No implementation here - we don’t know
// what our student likes to buy!
}
just move it to Shopper class.
The method BuyFavoriteStuff is not inside class
public virtual void BuyFavoriteStuff()
{
// No implementation here - we don’t know
// what our student likes to buy!
}
You should declare your method inside class.

Categories

Resources