Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 spinaljack · May 03, 2010 at 11:16 PM · arraysrpgmmoquest

Array of scripts

I've got a script on a game object and I want it hold an Array of other scripts on the same game object. I thought this might be a good way of storing MMO style quests as each quest can have its own script and its own rules and functions.

I've tried declaring the array as Component[] and it lets me drag the scripts into the array in the inspector but it wont let me call any functions on the quest script because it says "x is not a member of component"

Is there a way to store scripts in an array which lets you access functions in specific scripts? I thought about using SendMessage but it would be called on every single script in the game object which might slow the game down if there are hundreds of quests.

Is there a better way to manage quests?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by fherbst · Jun 15, 2010 at 09:02 AM

Your question reminded me of something in my programming class - type polymorphism! And I was curious whether this can be used in Unity. So I took the time to put it together.

The idea is to have a basic quest class:

using UnityEngine; using System.Collections;

// BaseQuest derives from MonoBehaviour, so it can be added to GameObjects

// abstract means: this class does not implement any functions, it only says which functions // must be implemented in derived classes public abstract class BaseQuest : MonoBehaviour { // here we have the basic quest behaviour - each derived class knows what to do with this function public abstract string QuestText(); }

This script is never actually added to any GameObject, its just the base for other quest scripts:

A simple quest:

using UnityEngine; using System.Collections;

// a simple quest is a quest - so it derives from the BaseQuest class public class SimpleQuest : BaseQuest { void Start () {}

 // the "old" QuestText method from BaseQuest gets overwritten
 public override string QuestText()
 {
     return ("I am a simple quest!");
 }

}

A more advanced quest:

using UnityEngine; using System.Collections;

// an advanced quest is a quest - so it derives from the BaseQuest class public class AdvancedQuest : BaseQuest { void Start () {}

 // the "old" QuestText method from BaseQuest gets overwritten
 public override string QuestText()
 {
     return ("I am an advanced quest!");
 }

}

Right now, you can add the AdvancedQuest and SimpleQuest scripts to a GameObject as many times as you want. Now comes the script that handles everything:

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class QuestHandler : MonoBehaviour {

 // This list handles all the quests
 private List<BaseQuest> quests;

 void Start ()
 {
     // the quests list gets created
     quests = new List<BaseQuest>();

     // collecting all the quests from the GameObject (the player)
     Component[] questsComps = gameObject.GetComponents(typeof(BaseQuest));  
     foreach(Component comp in questsComps)
     {
         // adding the quest to the list
         addQuest(comp as BaseQuest);
     }

     // each quest shows his message
     foreach (BaseQuest quest in quests)
         // here comes the magic - type polymorphism!
         // all quests in the list derive from BaseQuest, which has this function
         // all the derived quests override the QuestText function with their own behaviour
         Debug.Log(quest.QuestText());
 }

 void addQuest(BaseQuest q)
 {
     // adding the quest to the list
     quests.Add(q);
 }

}

It gets added to the same GameObject. On Start(), it collects all the scripts of BaseQuest class and all its derivates (this was were I wasn't sure Unity would handle it in this (right) way). All the quests get added to a quests List of BaseQuest objects.

And then every quest is asked to return its QuestText - and due to the type polymorphism, the right QuestText method is chosen and executed.

Thanks for this cool question!

Comment
Add comment · Show 4 · 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 _Petroz · Jun 15, 2010 at 09:21 AM 0
Share

Excellent answer.

avatar image spinaljack · Jun 15, 2010 at 03:09 PM 0
Share

That's a good way of handling quests lists but it would be difficult to be sure of the order that the quests appear in the list. For instance if you wanted to update a single quest script you'd have to do a search through the list each time you wanted to do an update. The only alternative would be to set a public array and drag every script into the inspector. It's an interesting solution though.

avatar image fherbst · Jun 15, 2010 at 03:38 PM 0
Share

The List class is ordered, so maybe one could use that. There can even custom Sort-methods be defined. And as (with this way) each quest is a $$anonymous$$onoBehaviour resting on the GameObject, they expose their properties to the editor and do things like Update() and all the other stuff. Thanks you like the solution!

avatar image fireDude67 · Nov 26, 2010 at 09:13 PM 0
Share

Ins$$anonymous$$d of using a list, you might want to try using a Dictionary, as then you can look up quests by name ins$$anonymous$$d of number

avatar image
-1

Answer by Lucas Meijer 1 · May 04, 2010 at 07:07 AM

Best I can think off right now is to have remember which quests to use by their name. string[] quests. It doesn't give a great UI experience out of the box (you cannot drag quest scripts onto them), however that is a great excuse to write a custom inspector, where you can drag quests scripts onto it, which will then in turn just read the name from the script, and add it to the stringarray. At runtime, you can get at the .net type of your script using Type.GetType("BigAssDragonQuest");

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 spinaljack · May 04, 2010 at 09:46 AM 0
Share

Thanks for the answer, now if there were a way to use the string to call another script, I dunno if GetComponent will do it..

avatar image Lucas Meijer 1 ♦♦ · May 06, 2010 at 07:22 AM 0
Share

You can. You could use System.Activator to create an instance a script by name.

avatar image spinaljack · May 18, 2010 at 10:33 AM 0
Share

What if the script is already on the game object? If each trigger instantiates a new script it wouldn't be tracking the state changes.

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

No one has followed this question yet.

Related Questions

How do I get multiplayer to my game? 1 Answer

How To Make A Simple MMO? 3 Answers

RPG turns system - arranging turns freezes up 1 Answer

RPG controller 1 Answer

Looking for a chat text box that allows voice chat... 0 Answers


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