EtanSivad
EtanSivad

Reputation: 511

How do I pass a method as a parameter?

I've been tinkering with a game engine and part of it has been building a class for the UI elements. My goal is to make it really simple to add buttons to the UI with just a line that will include the button location and what method it calls when someone presses that button. I just can't figure out how to pass in the target method that will trigger when the button is pressed. I can get methods to subscribe to delegate events, just not when they're wrapped up in a list of button objects I've created.

I've simplified the code down to just what I'm trying to accomplish here. The main sticking point is I'm not sure what to put in as the object type for the method parameters for addButton() in order to be able to pass in another method that can subscribe to delegate events. If I try Void or Object, I get conversion errors.

public Class UserButton
{
    public delegate void triggerEvent();
    public triggerEvent ButtonPress; //This should fire off the event when this particular button is pressed.

    public UserButton(Point buttonlocation, Point buttonsize, string buttontext, Texture2d Texture)
    {
        //Store all this data
    }
}

public Class UserInterface  //This class is for the buttons that appear on screen.  Each button should have a location and a method that it calls when it's "pressed"
{

    List<UserButton> ButtonList = new List<UserButton>(); //List of all the buttons that have been created.  


        //Add a button to the list.
    public void addButton(Point location, Point buttonsize, Texture2d texture, Method triggeredEvent) //Compile fails here because I can't figure out what type of object a method should be.
    {
        UserButton button = new UserButton(Point location, Point buttonsize, Texture2d texture);

        button.ButtonPress += triggeredEvent; //The target method should subscribe to the triggered events. 

        ButtonList.Add(button);

    }

    public void checkPress(Point mousePos) //Class level method to sort through all the buttons that have been created and see which one was pressed.
    {
        foreach (UserButton _button in ButtonList)
        {
            if (_button.ButtonBounds.Contains(mousePos))
            {
                _button.ButtonPress(); //Call the event that should trigger the subscribed method.
            }
        }
    }
}

public class Game1 : Game
{

    //Main methods

    //Load
    protected override void LoadContent()
    {
        UI = new UserInterface(); //Create the UI object
        UI.addButton(new Point(32,32), Texture,toggleRun()); //Pass in the method this button calls in here.
    }   

    private void toggleRun() //The button should call this method to start and stop the game engine.
    {
        if (running)
        {
            running = false;
        } else {
            running = true;
        }


        protected override void Update(GameTime gameTime)
        {
            if (MouseClick) //simplified mouse check event
            {
                UI.checkPress(mousePos); //Pass the current mouse position to see if we clicked on a button.
            }
        }
    }

Upvotes: 1

Views: 96

Answers (1)

McGarnagle
McGarnagle

Reputation: 102753

Your "triggeredEvent" parameter should be the same delegate type "triggerEvent":

public void addButton(Point location, Point buttonsize, Texture2d texture, triggerEvent triggeredEvent) 

To pass a method as the parameter, use the method name without invoking it (this will only work if the method "toggleRun" does not have any overloaded methods, and matches the signature of the "triggerEvent" delegate):

UI.addButton(new Point(32,32), Texture, toggleRun);

Upvotes: 2

Related Questions