- Home /
Add a Prefix to a Variable
Hi, all! This would spare me a HUGE amount of time if this is possible...
Is there any way I can add a prefix to a variable? In my game, I have 3 save slots, and a different variable set for each, and a static int in a master script that holds the current and active slot number. For instance: If I make a variable 'apple', I'd have to make it for all 3 slots, which is fine by me. So, in a script labeled 'Master', there would be:
static var S1_apple : int;
static var S2_apple : int;
static var S3_apple : int;
Which is great. But if I want to modify it in another script, I have to do this:
var value = 23;
function Start(){
if(Master.CurrentSlot == 1){
S1_apple = value;
}
if(Master.CurrentSlot == 2){
S2_apple = value;
}
if(Master.CurrentSlot == 3){
S3_apple = value;
}else{
Debug.Log("You forgot a slot!");
}
}
For EVERY VARIABLE which takes forever and a half. So my question is this:
Can I add prefixes to variable names? At the beginning of the script, I would get the CurrentSlot in the Start() function, and then assign either S1_, S2_, or S3_ to a variable depending on the slot. Then, when I want to modify a variable, I could just use the prefix variable like
*prefix variable* + apple = value;
Look how much code that saved! 0_o But obviously that doesn't work.
Is there a way to do this? You'd be my saviour! :D
Thanks!- YA
Why don't you use an arr. . . . Eric beat me Yes, if you use an array your code would look like
apple[$$anonymous$$aster.CurrentSlot] = value;
so I should just change the variable names to apple0, apple1, and apple2?
Answer by Bunny83 · Aug 08, 2012 at 03:14 AM
How about an array? Are you sure you need those variables to be static? Anyway...
static var apple : int[] = new int[3]; // array with 3 elements
var value = 23;
function Start()
{
// array indices start at 0 not 1
var index = Master.CurrentSlot-1;
if(index >= 0 && index < apple.Length)
apple[index] = value;
}
yes they must be static, as the script that modifies them is not local to them. so i would just use these [] to add to a variable name?
> yes they must be static, as the script that modifies them is not local to them.
That's not relevant. Static means there's only one instance of a variable; it has nothing to do with local access. A public variable is accessible from anywhere regardless of whether it's static or not (though the method of access may differ).
> so i would just use these [] to add to a variable name?
At the risk of sounding snarky, I think you'd be doing yourself a huge favor if you took some time out to do some basic computer language learning. Take some classes, or find something online at least. Arrays are a really fundamental concept that you must know if you're going to do any program$$anonymous$$g. Otherwise you're going to continue wasting a lot of time trying to find "workarounds" for things that don't need to be worked around.
No, you can't add something to a variable name. An array is a collection of multiple values of the same type. In the index brackets [] you have to put the index which is always an integer value between 0 and count-1.
In my example the array contains 3 elements so valid indices are 0, 1 or 2
btw: The fact that another script / class have to access this variable doesn't require you to make it static. Take a look at the Accessing_Other_Game_Objects documentation page.
Haha oh my gosh thank you Bunny. Lol I do have a large amount of coding experience. Did I come off as not knowing what I was talking about? I realize that he was indexing the array. Haha I was just hoping that I could add to it. yes I know arrays start at 0. this is a great solution to my problem except for one thing: The variables I am trying to access already belong to arrays in their respective Slot numbers so it is eaey for me to save data... Is their another 'workaround'? :P
Sure it matters. It's nothing to do with being exposed to the editor; public and static are separate things. It's not like public and private, which are opposites. A variable can be public and static, or public and non-static, or private and static, or private and non-static, plus a couple other access modifiers like protected. As I said, static means there can only be one instance of that variable. (Which leads to people repeatedly asking questions about "how come when I change this variable, it changes on all of my objects!!!11111!11")
Your answer
Follow this Question
Related Questions
Array Problem - Error Code BCE0022 1 Answer
Array Problem - Error Code BCE0022 2 Answers
global variable need advice 1 Answer
What are the sytem requirements? 0 Answers