Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Maker16 · Jan 26, 2012 at 10:34 PM · referencefunctions

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Maker16 · Jan 26, 2012 at 10:42 PM 0
Share

If it helps, this is the article I am using as a starting point for this system: http://robert.zubek.net/publications/Needs-based-AI-draft.pdf

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Jan 26, 2012 at 10:48 PM

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[i]();
     }
 }
 
 function Foo () {
     Debug.Log ("foo");
 }
 
 function Bar () {
     Debug.Log ("bar");
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Maker16 · Jan 26, 2012 at 11:02 PM 0
Share

Do you say "if they have the same parameter types" because List will only store objects of the same type? If that is the case, then, could I use Array ins$$anonymous$$d, since it doesn't care about the type, and use it to store any set of functions, even with differing parameter types?

avatar image Eric5h5 · Jan 26, 2012 at 11:18 PM 0
Share

There's no reason to use Array, but you can use a List of Object (which would mostly amount to the same thing, except faster), though you'd have to cast to the correct type when accessing each element, so I don't know how useful that would be in practice:

 function Start () {
     var functionList = new List.<Object>();
     functionList.Add(Foo);
     functionList.Add(Bar);
     (functionList[0] as function())();
     (functionList[1] as function(int))(123);
 }
 
 function Foo () {
     Debug.Log ("foo");
 }
 
 function Bar (x : int) {
     Debug.Log ("bar " + x);
 }
avatar image Maker16 · Jun 12, 2012 at 01:11 AM 0
Share

Ok. I'm trying this system a little differently, and I am having issues with syntax. How can I, if possible, declare a variable of type function(Type)? Or better yet, how can I translate the aspect of what you have given me into an argument I can pass to another function?

What I am trying to do now is have an object that, when it needs something done, it will send a request for service via a task manager. Think of the task manager as a bulletin board of sorts. When an object needs something done, it posts a task request to the task manager. The task request would identify the script on whichever object is requesting service and which function in that script should be called by whichever agent is going to fulfill the request.

What I have got so far is some object with a function, CallRepair(), and a function Repair(Agent). Repair(Agent) will be the code that assumes control of the agent that is passed to it until the repair is complete. CallRepair() will call Task$$anonymous$$anager.PostTask(this,Repair). I need a way to define Task$$anonymous$$anager.PostTask() such that it knows to expect a script of any type and a as parameters so that it can store those as a Task object in a list. A Task object is basically a struct that has variables for the script and the function to call in that script.

avatar image
0

Answer by Maker16 · Jun 12, 2012 at 02:02 AM

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

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Referncing variables such as OnTriggerEnter(Collider other) thumb rule? 1 Answer

Reference Function Trouble 2 Answers

General function knowledge 1 Answer

Check if a value is null from another script? 1 Answer

Adding a reference to a project not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges