Can References to Functions Be Stored in an Array?

Let me begin by explaining what I am doing. I am developing a Need based AI system. Basically, the agent polls surrounding objects and determines which will provide the best reward in terms of the agent’s current needs. In order to get the reward as advertised, the agent will need to perform certain actions that are dictated by the advertising object. So, for example, to get a +10 reward towards hunger, the agent would have to do certain actions that are determine by the advertising object. When the agent selects an advertising object, that object pushes the actions the agent needs to do onto the agent’s action queue.

Now to my question. I need to somehow create the action queue for the agent such that reward objects can push their actions onto it. My first thought was to define the actions and their logic somewhere on the reward object as functions, and then, somehow, pass a reference to the functions to the agent to be stored on the action queue. Is this possible? Can a create an array on the agent that can store references to functions on another object? As I believe it has some relevance, I am currently attempting this using JS, but am not opposed to C# if it is easier to implement.

Or, perhaps, there is a better way of doing all of this?

It’s easy enough if all the functions have the same parameter types (in the case of this example, no parameters):

function Start () {
	var functionList = new List.<function()>();
	functionList.Add(Foo);
	functionList.Add(Bar);
	for (var i = 0; i < functionList.Count; i++) {
		functionList*();*
  • }*
    }

function Foo () {

  • Debug.Log (“foo”);*
    }

function Bar () {

  • Debug.Log (“bar”);*
    }

Nevermind. I sorted it out(basically, I overlooked the obvious and assumed it would be more complex):

function PostTask(func : function(Agent):int)