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
0
Question by MrPantaIoons · Mar 26, 2015 at 04:13 PM · variablesmethods

How to go to a function using a variable?

I'm relatively new to programming, so please forgive me if I'm using the wrong terms.

I can explain it better if I show you an example: using UnityEngine; using UnityEngine.UI; using System.Collections;

     public string preLoc;
 
     private enum Locs {deadend_0, deadend_1}
 
     public Text text;
 
         void Start () {
         myLoc = Locs.deadend_0;
     }
 
     void Update () {
         print (myLoc);
         if (myLoc == Locs.deadend_0)                    {deadend_0();}
         else if (myLoc == Locs.deadend_1)                {deadend_1();}
     }
 
     void deadend_0() {
         text.text = "You are in a dense forest. All you have with you is a compass. In the  darkness you can make out a dirt path leading to the east. Press Z to teleport";
         else if (Input.GetKeyDown(KeyCode.Z))    {
             preLoc = deadend_0();
             myLoc = Locs.combat_0;
         }
     }
         void deadend_1() {
         text.text = "The path stops. You have reached a dead-end. At the end of the path is a stick that fell from one of the many trees around you. Press 1 to go back";
         if (Input.GetKeyDown(KeyCode.Alpha1))        {myLoc = preLoc;}
         }
     }

(Please note this is only an example, a small part of a text-based game I'm trying to make).

So in the actual game, I have a separate function (again, I'm hoping that's what the deaden_0() things are called) for combat. What I'm wanting to do with preLoc is setting the previous location's name using a variable before the fight. In the code, once the fight is over, it checks the preLoc variable to see where you were previously and brings you there (in this case, deadend_0). The only problem is, it's not working. If I can't use a string, what type of variable do I use?

I hope I explained this clearly, and I would appreciate any help I can get.

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 AlwaysSunny · Mar 26, 2015 at 04:21 PM 0
Share

To actually answer your question, the sane, appropriate way to associate a function with a variable involves delegates. However, this is not what you need to do here to improve your code or your own experience as the programmer. Get those fundamentals down first! :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by AlwaysSunny · Mar 26, 2015 at 04:15 PM

Just because I have a soft spot for text-based...

There are a lot of ways you could make designing a text-based game so, so, so much easier than this. For one thing, you're hard-coding everything about the game. As a general rule, only the fundamental structures of a game should be hard-coded; all the flavor stuff should exist in the form of objects you can create, save, change, etc without (much) scripting. Also, you're evidently treating rooms like methods, when they should definitely be objects. It's a novel idea, but the object approach is much more useful and flexible. Changing both of these practices would be a smart move.

Creating a map-editor to automate the population of these values would be very wise. A map editor would do a lot of the tedious work for you, and allow you to easily expand or make changes or re-use the bones of your program for future games. In either event, here's some oldschool fundamentals:

Decide how many different directions you want the player to be able to travel in. N, S, E, W, Up, Down, etc. Maybe a special case or two like fly or teleport.

Define a "room" as an object with a string description, a unique integer index, and other fields as needed. Each room gets a "directions" array of integers. The size of the array corresponds to how many different "travel directions" you have. The array's elements are the indexes of the adjacent rooms to which you can travel. The zeroth element is the index of the room to the north, the first element is the index of the room to the south, etc.

 // the room class
 public class Room {
   public string description;
   public int index;
   public int[] directions;
   public List<Item> items;
   public List<Monster> monsters;
 }
 
 // This is how you'd create room objects by scripting.
 // This is where a map editor would do ALL the heavy lifting.
 // You'd have a nice interface to type in a description, add any 
 // items to the room, automatically find possible walk directions,
 // and never need to code each room by hand.
 // It would also eliminate all hard-coded "content".  
 // Only a game's "bones" should be hard-coded.
 Room kitchen = new Room();
 kitchen.description = "a quaint, rustic kitchen."
 kitchen.index = 10;
 // room with index 9 is to the north, room 3 to the south, etc
 kitchen.directions = new int[] { 9, 3, 2, 4 };

When user input signifies a particular travel request, such as "go north" or pressing the N key, whatever, call your WalkInDirection() method with the corresponding index to travel to that room.

 void WalkInDirection(int direction) {
   int nextRoomIndex = currentRoom.directions[ direction ];
   GoToRoom( nextRoomIndex );  
 }  

When you arrive in a new room, print its description, along with a list of any items or monsters in the room, and a list of available directions. By checking which indicies are in that new room's directions array, you know which directions are available, and print descriptions accordingly.

Now that I've written this out, I realize I haven't addressed your original question. Your approach to this situation is going to create so much needless work for you, I felt bad even trying. Choosing to hard-code all your content will likewise create a ton of work, but even that is more manageable than treating rooms like methods.

There's bound to be some great information regarding programming text-based games floating around. I have a book on my nightstand about programming such games in BASIC. The fundamentals haven't changed a bit, but you could make your job 100x easier by taking the time to code yourself a map editor. I realize that's probably not what you want to hear, but if you can make the computer do the tedious work for you, you'll have so much more fun.

Comment
Add comment · Show 1 · 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 MrPantaIoons · Mar 26, 2015 at 06:15 PM 0
Share

Thank you so much for all your help! I'll have to give that new way a shot. Although I understood most of what you said, I still didn't get some of it. That's not your fault, though, I'm still learning :D

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

cant change variable from a method? timer stays at 0! 1 Answer

Does a reference to a script have up-to-date info ? 1 Answer

Method variables change value all at once (2020.1.2f1) 1 Answer

Variable Not Changing In Method 1 Answer

Calling a variable based on a variables name? 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