How to make list of methods (example Sims)
How can i list methods?
public List<???> Actions = new List<???>();
and then i would call the top one in the list every time previous method is completed or something like that.
Or is there a better way to do this?
Answer by RudyTheDev · Sep 19, 2015 at 08:55 PM
It depends how complex and OOP you want to go with this. In the simplest case:
using System;
using System.Collections.Generic;
using UnityEngine;
public class ActionClass : MonoBehaviour
{
private List<Action> actions;
private void Start()
{
actions = new List<Action>();
actions.Add(Action1);
actions.Add(Action2);
actions.Add(Action3);
}
private void Action1() { }
private void Action2() { }
private void Action3() { }
}
Something like Sims is sure to do it much more involved. An OOP base would look something in the lines of:
using System.Collections.Generic;
using UnityEngine;
public class AbilityClass : MonoBehaviour
{
private List<Ability> actions;
private void Start()
{
actions = new List<Ability>();
actions.Add(new WatchTV());
actions.Add(new TakeBath());
actions.Add(new MakeFood(3));
}
}
public abstract class Ability
{
public abstract void Do();
}
public class WatchTV : Ability
{
public override void Do()
{
Debug.Log("Watching TV");
}
}
public class TakeBath : Ability
{
public override void Do()
{
Debug.Log("Taking a bath");
}
}
public class MakeFood : Ability
{
public int servings;
public MakeFood(int servings)
{
this.servings = servings;
}
public override void Do()
{
Debug.Log("Making food");
}
}
And how can I make Abilities like $$anonymous$$akeFood play an animation and sound?
@$$anonymous$$TheDev @Hullu thanks for your answer. How do you invoke all the methods in the list?
foreach (Ability action in actions) { action(); }
This doesn't seem to work :(
foreach (Ability action in actions) { action.Do); }
How can I sort this list For example, first in list will be $$anonymous$$akeFood than others?
Your answer
Follow this Question
Related Questions
Problem when acessing a list from another script? (ArgumentOutOfRangeException) 0 Answers
Is there anyway to make a list of prewritten variables? (C#) 2 Answers
Arbitrary line in one method prevents another for working? 1 Answer
Insert string into empty list at a specific index 0 Answers
Referencing uninstantiated GameObject within a class? 0 Answers