Hi guys,
On my 2D platformer, I want to have a skills system : when the player collect an item, a new behaviour is “unlock” ( like wall jump, stuff like that )
I think I can use an Array, but I never used it…
I’m thinking of a classic int array “skills” with for each “i” a skill ID.
When the item is collected, the matching ID is unlock, but I’m really not sure about all the technics aspect…
Could you guys lead me in the right direction ?
I think I have to create an individual script for the items to collect, obviously scripts for the actual skills-behaviours, and a script for the “array skill inventory”
you could use an array but i would use a list. they are like arrays but more versatile.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class item
{
public string itemName;
//put whatever stats you ant the item to have
}
public class helpSuttf : MonoBehaviour
{
public List<item> collectedItems = new List<item>();
//function for item in the game
void addItemToCollection()
{
item newItem = new item ();
newItem.itemName = " ring or something";
collectedItems.Add (newItem);
}
}
thats a basic set up. you could create an item database list to hold all items in the game etc.
Why not embrace component-based design a little more fully, and handle this by component? You said each skill is already a behavior. Add all of the skill behaviors to your player. Disable the ones that the player doesn’t have yet.
Then add a script like this to the skill item pickup:
public class SkillPickup : MonoBehaviour {
public Component skillToEnable; //<-- Assign a skill on the player.
public void OnTriggerEnter(Collider other) {
if (other.CompareTag("Player")) {
skillToEnable.enabled = true;
Destroy(this.gameObject);
}
}
}
When the player enters the skill item pickup’s trigger area, it enables a skill behavior on the player.
Thank you guys, I’m gonna give a try with both methods tomorrow !
Right now, the component based solution seems easier, but I think that it might be time to get into array ^^That seems a little bit more “proper”, I feel like going for component all the way is not really elegant, dont know why ^^
Well, after few minutes of thinking, I think there I’ve got a major issue with this method :
I need to disable the scripts on start, but if I want to keep track between the differents levels of the skills unlock, it’s gonna be really hard isn’t it ?
Level 1 :
disabling all the skills On Start
all skills disabled
pick up WallSlideItem → wall slide unlock.
Level 2 : argh… ^^’ I definitely need a way to store my items, don’t I ?
[EDIT]
after really few changes, this is definitely working, but the issue of keeping track between scenes still bothers me.
I guess I got this coming ^^’
To be more specific, I already watched the beginner section but I find it really un-intuitive to access an array from another script, then having another script checking the value of the index (if this is the right term to use).
You can also give each behavior a bool value on whether it is enabled or not, and call that. That way you don’t need to have the scripts disabled, but instead just default that bool to false and set them to true when you pick up the item.
I’m not trying to be offensive or anything. There are a certian set of fundamentals you need to grasp before you can get anywhere. These are completely independent of game type, and are typically not even unique to game programming.
Collections in general, and arrays specifically, are almost as fundamental as the concepts of branch, loop and variable.
I read it that way, no offense taken, and I know it’s a commun mistake to take shortcuts. I definitely take your advice in account.
To be fair, I’m working on a game prototype, and I don’t really aim at becoming a real programmer, that’s the reason why I wasn’t to concern about the “legit” of my programming. But yeah, I guess arrays are basics, and will need them eventually.
you mean matching each “enabled state” scripts to booleans ? How can this help ?
Maybe you mean that it’s easier to keep track of a bool value in a kind of manager script which would be persistent between scenes than keeping track of the “able state” ?
Here’s an example that uses PlayerPrefs to save skills between levels. This is just one way of doing it, certainly not definitive. Other (more complex) approaches are being discussed here, and previously here.
Make your skills inherit from a new class SkillBehaviour (defined further down):
public class HealSkill : SkillBehaviour {
public void Update() {
if (Input.GetKeyDown(KeyCode.H)) { // Just some example code.
HealthManager.health += 50;
}
}
}
SkillBehaviour keeps track of the enabled state:
public class SkillBehaviour : MonoBehaviour {
public string playerPrefsKey; //<-- Set a unique key for each skill such as "Heal".
private bool started = false;
public virtual void Start() {
enabled = PlayerPrefs.GetBool(playerPrefsKey);
started = true;
}
public virtual void OnEnable() {
if (started) PlayerPrefs.SetBool(playerPrefsKey, true);
}
}
Start and OnEnable are virtual methods so you can override them if you need to do something extra in subclasses such as HealSkill.
Thx for the detailed reply Tony, I’m gonna look deeper in that PlayerPrefs stuff.
I’m also getting started with arrays, I’m not sure yet which method I’m gonna use but I definitely appreciate your inputs guys !