- Home /
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.
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! :)
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.
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
