Non-static method as constructor argument

Godot Version

4.2

Question

I have class ‘action’ that has parameter of ‘delegate void Perfomance’ - ‘perfomance’ that is initialized in constructor. I want to declare and initialize a variable of ‘action’ class in Character’s(script class) field and give a Character’s method as constructor argument, but i can’t do that because Run is non-static method.

public abstract partial class Character : CharacterBody2D
{
    protected class action
    {
        public delegate void Perfomance();
        private readonly Perfomance perfomance;
        public action(Perfomance perfomance)
        {
            actions.Add(this);
            this.perfomance = perfomance;
        }
    }

    protected static List<action> actions = new List<action>();

    protected action run = new action(Run);
    protected void Run()
    {

    }

    public Character()
    {

    }
}

I need to have a variable of class ‘action’ that can be declared and initialized in Character’s field and have ‘perfomance’ setted as ‘Run’ method.

// Create an instance of the delegate.
NotifyCallback del1 = new NotifyCallback(Notify);

Try this


protected action run = new action(new Performance(Run));

1 Like

No, it doesn’t help. In that case delegate constructor sends warning that it can’t use non-static argument.

protected action run = new action(new action.Perfomance(Run));
//CS0236
protected static action.Perfomance runPerfomance = new action.Perfomance(Run);
//CS0236
protected action run = new action(runPerfomance);
2 Likes

Okay,

Maybe the run method just needs to be static.

protected static void Run()

Also looking at other examples, new may not be necessary in some cases.

This error happens because you can’t reference an instance method (Run()) in a field initializer.

using Godot;
using System.Collections.Generic;

namespace Test20241021
{
    public abstract partial class Character : CharacterBody2D
    {
        protected class Action
        {
            public delegate void Perfomance();
            private readonly Perfomance perfomance;

            public Action(Perfomance perfomance)
            {
                actions.Add(this);
                this.perfomance = perfomance;
            }
        }

        protected static List<Action> actions = [];
        protected Action run;

        public Character()
        {
            run = new Action(Run);
        }

        protected virtual void Run()
        {
        }
    }
}

Action can be outside Character as internal class that will be not visible outside namespace. With that change code will be more readable in my opinion.

isnt more clean? :

using Godot;
using System.Collections.Generic;

namespace Test20241021
{
	public abstract partial class Character : CharacterBody2D
	{
		internal static List<Action> actions = [];
		internal Action run;

		public Character()
		{
			run = new Action(Run);
		}

		protected virtual void Run()
		{
		}
	}

	/// <summary>
	///  This is internal class for Character
	/// </summary>
	internal class Action
	{
		public delegate void Perfomance();
		private readonly Perfomance perfomance;

		public Action(Perfomance perfomance)
		{
			Character.actions.Add(this);
			this.perfomance = perfomance;
		}
	}
}