I have a ‘Weapon.cs’ scriptable object that holds a list of attacks ('Attack.cs) as seen below. Everything currently works well and the system works fine. The weapon uses all of its attacks on use. However now i would now like to add the ability for the Player to craft their own weapon. What i mean by this is Players will earn ‘Weapons’ and ‘Attacks’ through the course of the run. I want to be able for the Player to add ‘Attacks’ to ‘Weapons’ in the game dynamically. For example, when a Player reaches a forge they have the option of coming their weapons with attack cards that adds a certain ‘Attack’ to the weapon thus increasing its power. This is all pretty simple to do, however i’m not sure how to create blank Weapons (ScriptableObjects) on runtime for the game to then manipulate and delete when the run is finished. From my understanding ScriptableObjects don’t work that way.
So for example, during a ‘Battle’ scene, enemies can drop blank weapons or weapons with a attack already attached to them. How do i store these weapons? I don’t want the pre-made weapons to be effected by any changes i make to the blank or dropped weapons. The game is a RogueLike so everything needs to reset on the next run (all generated weapons deleted, etc).
I have looked into ‘CreateInstance’ however i’m not sure that will work says the docs say it is ‘bound to a .asset file via the Editor user interface’
In the future, combining weapons will also be a goal.
Any help would be appreciated, thanks.
I’ll add i also have the EasySave asset
Weapon class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Weapon", menuName = "Inventory/Weapon")]
public class Weapon : Equipment
{
public Attack attack;
[HideInInspector] public List<Attack> attacks;
void OnEnable(){
attacks = new List<Attack>();
}
public bool CanAttack(GameObject attacker)
{
return attack.CanAttack(attacker);
}
public void addAttack(Attack attackToAdd){
attacks.Add(attackToAdd);
}
public void Attack(GameObject attacker)
{
foreach (var attackInList in attacks)
{
Debug.Log("Attacking with: " + attackInList.name);
attackInList.OnAttack(attacker);
}
}
}
Attack Class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Attack : ScriptableObject
{
public abstract bool CanAttack(GameObject attacker);
public abstract void OnAttack(GameObject attacker);
public float AS = 1f;
public bool offCD;
}