- Home /
Assign a String to Each int
Hello, I need some help with my script which is a simple skill bar. At the moment I have an int variable which is the current skill slot the player is at and you can get to the next or previous skill with the mouse wheel. Since each skill is different I use something like if (currentskillslot == 1) to check if the player is currently on the 1st slot. So far so good, but I want the player to be able to change or reassign each skill to a slot of his choice. Since I'm checking in which slot the player is at it will be a problem to change the skill on that slot because it will still keep the previous skill settings. Here is a scheme that I hope can help you guys understand a little better what I want to achieve .
If I were you I would investigate into the use of a dictionary: $$anonymous$$SDN Dictionary
A dictionary is a collection of key, value pairs. In this case the simplest could be Dictionary<int,string>
. The keys must be unique and I think they could be the number of the slot (which is unique in your case). The values will be the string associated with each of the key . You will have to create some logic to manage the retrieving of a key and assigning a new string to it.
I hope this could point you out to a correct direction.
Answer by ArcaneAura · Apr 05, 2018 at 01:40 PM
//Use a dictionary <int,string>, as <key , value>.
Dictionary<int, string> skill_inventory;
//populate the dictionary in start
void start(){}
//define your game logic in update
void update{
// quick example
if(skill_slot ==1 && able_to_change == true){
skill_inventory[1] = "your new desired string"
}
}
I took a look at this which was useful but he is using it for something different and I still have a lot of questions in my case. For example how can i check at which slot the player is currently at, how can I add or remove a number from the int variable so i can get to the next or previous skill.
Q. How to check which slot the player is currently at A. Your slot is your dictionary's "key value", thus we make an int variable call "current_skill". Then, you need to know what is your games controller that "updates" "current_skill". For example if(input.getkeydown("1") == True) current_skill = 1;
Q. how can I add or remove a number from the int variable so i can get to the next or previous skill
Dictionary <int,string> skill_inventory;
int current_skill = 1;
// define your game logic using the functions below
void update() {}
string GetPrevious ()
{
// key of the previous skill
int prevSkill = current_skill - 1;
// incase out of bound error
if(nextSkill < 0) nextSkill = skill_inventory.size - 1
// the value of previous skill
string prevString = skill_inventory[prevSkill];
return prevString;
}
string GetNext()
{
// key of the next skill
int nextSkill = current_skill + 1;
// incase out of bounds error
if(nextSkill >= skill_inventory.size) nextSkill = 0
// the value of nextskill
string nextString = skill_inventory[prevSkill];
return nextString;
}
void Add_Skill (int key, string val)
{
skill_inventory.add( key,val )
}
void Remove_Skill (int key)
{
skill_inventory.remove( key,val )
}
void $$anonymous$$odify_Skill (int key, string val)
{
skill_inventory[key] = val;
}
At last, don't forget to mark my answer as "answered" :)