How do I change values in a scriptable object? - unity3d

I'm trying to make a class selecting system and I have 2 scriptable objects one for the player and the other for classes and I'm trying to figure out how to change the values in PlayerSettings to the ones in BaseClass when a class is selected.
public class BaseClass : ScriptableObject
{
public float health;
public float walkSpeed;
public float fallSpeed;
public float jumpForce;
public string displayName;
public List<BaseAbility> abilities;
}
public class PlayerSettings : ScriptableObject
{
public float health;
public float walkSpeed;
public float fallSpeed;
public float jumpForce;
public BaseClass playerClass;
}
Heres the code I have for setting a class I use an event that gets called when a button is pressed and then I just change the values in the PlaerSettings SO to the ones from the newClass argument
public class Player : MonoBehaviour
{
public PlayerSettings playerSettings;
public Controls keybinds;
void Start()
{
ClassSelectButton.OnClassSelected += SetPlayerClass;
}
void SetPlayerClass(BaseClass newClass)
{
Debug.Log("Setting player class to: " + newClass.displayName);
playerClass = newClass;
playerSettings.walkSpeed = playerClass.walkSpeed;
//etc
}
}
I'm not sure if what I have is fine although I have a feeling it's probably not though. Also, a quick second question as you can see PlayerSettings and BaseClass are basically the same would I want to use inheritance here or is it fine to keep it separate?

So what you could do is to make the playerClass a property and set the values in the set function, but maybe it's easier to just make the fields be properties that forward the playerClass values. I'll use the null coalescing and null conditional operators here, but hopefully this gives you an idea.
Also a good IDE should complain about the properties not beginning with a capital letter here but I'm trying to give you an example that won't break your current code:
public class PlayerSettings : ScriptableObject
{
public float health => playerClass?. health ?? 100f;
public float walkspeed => playerClass?.walkspeed ?? 1f;
public float fallSpeed => playerClass?.fallSpeed ?? 1f;
public float jumpForce => playerClass?.jumpForce ?? 1f;
public BaseClass playerClass;
}
The numbers after ?? are the default values you'd use if playerClass is null. These are all getters, though, so they're read-only. Again you could have the playerClass assign those values when it's set as part of the setter function, if you make it a property, but I don't know enough about your project to really make suggestions here. If you're leaving playerClass public here though it might just be easier to reach into that and get those values directly. I don't know what the point of your ScriptableObject is if it's just a public class to hold one public class.

At the top of your scriptable object code, you should add the line:
[UnityEngine.CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/BaseClassScriptableObject", order = 1)]
Then, you should see this added to your right click menu.
Upon creating an object, it should look like this in your inspector:
An example with your code:
[UnityEngine.CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/BaseClassScriptableObject", order = 1)]
public class BaseClass : UnityEngine.ScriptableObject
{
public float health;
public float walkSpeed;
public float fallSpeed;
public float jumpForce;
public string displayName;
}

Related

Calling protected variable from another class

I need to call a protected variable from a public class into an if statement in a private method of another public class
I am programing a video game in unity and I need to use a bool variable (that shows if the character is out of stamina) in an if statement to determine whether or not the character can run
This is what my code looks like excluding everything unrelated to the problem
Public class CharacterStats : MonoBehaviour
{
[SerialzeField] protected bool Tired;
}
Public class PlayerMovement : MonoBehaviour
{
Private void HandleRunning()
{
If (Input.GetKeyDown(KeyCode.LeftShift) && X != True)
{
Speed = RunSpeed;
}
}
}
X is where I want the Tired variable to be.
Use a public readonly property like e.g.
public class CharacterStats : MonoBehaviour
{
// Keep you serialized field protected
[SerialzeField] protected bool tired;
// Have a public read-only accessor property
public bool Tired => tired;
}
and then e.g.
public class PlayerMovement : MonoBehaviour
{
// Somehow you will need to get a reference to the CharacterStats instance
// e.g. via the Inspector
[SerializeField] private CharacterStats stats;
[SerializeField] private float RunSpeed;
private float Speed;
private void HandleRunning()
{
if (Input.GetKeyDown(KeyCode.LeftShift) && !stats.IsTired)
{
Speed = RunSpeed;
}
}
}
Alternatively (and my apologies to #aybe who had answered this) you can actually directly serialize a property using explicitely
[field: SerializeField] public bool Tired { get; protected set; }
this is a property which can be accessed by everyone but only this and inherited classes (and due to the serialization now the Inspector) have the permission to set the value.
In general: Fix your casing! In c# all keywords are lower key!

Serialisation not working when inheriting from monobehaviour

Im trying to do a property drawer for a class that i need to be updated as well as editable from the editor.
If i make the class a monobehaviour the serialisation stops working, but if i remove the monobehaviour inheritance it wont update with the game loop.
Is there any way to have both? I would need the object to be able to instantiate with default (empty) values if a monobehaviour script has non instantiated reference.
[Serializable]
public class MySmallTestProp : MonoBehaviour, ISerializationCallbackReceiver
{
[SerializeField]
private string name;
[SerializeField]
private string _name;
[SerializeField]
private float _someFloat;
public float someFloat;
public MySmallTestProp()
{ }
public void OnBeforeSerialize()
{
_name = name;
}
public void OnAfterDeserialize()
{
name = _name;
}
}
[CustomPropertyDrawer(typeof(MySmallTestProp))]
public class MySmallTestPropPropertyDrawer : PropertyDrawer
{
float rowHeight;
int rowSpacing = 5;
int index;
Rect currentPosition;
public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)
{
rowHeight = base.GetPropertyHeight(prop, label);
var rows = 2;
if (Application.isPlaying)
{
rows++;
}
else
{
rows++;
}
return rowHeight * rows;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
index = 1;
currentPosition = position;
EditorGUI.BeginProperty(position, label, property);
var nameProp = property.FindPropertyRelative("_name");
EditorGUI.PropertyField(NextPosition(), nameProp, new GUIContent("Name"));
EditorGUI.EndProperty();
}
Rect NextPosition()
{
currentPosition.height = rowHeight;
currentPosition.y = rowSpacing + (rowHeight + rowSpacing) * index++;
return currentPosition;
}
}
So if I understand you correctly what you want to achieve is having a class which is
Serializable
has some default field values
Receives an Update call every frame
Actually I don't think you need any custom property drawer for this.
First two points are as simple as having e.g.
[Serializable]
public class Example
{
// By default this has the value "Default String"
public string someString = "Default String";
// This can be edited only via the Inspector
// by default it is 42
[SerializeField] private float someFloat = 42.0f;
// This is a read-only public access
public float SomeFloat => someFloat;
}
Now to the last and tricky part - the update calls.
The easiest way is to have a dedicated MonoBehaviour like e.g.
public class UpdateDispatcher : MonoBehaviour
{
// Storing the instance for Singleton Pattern
private static UpdateDispatcher _instance;
// Register to this event to receive one call each Update
public static event Action OnUpdate;
// This method is automatically called by Unity when the application is started
// or you enter play mode in the editor
[RuntimeInitializeOnLoadMethod]
private static void Init()
{
// _instsnce is already assigned and alive?
if(_instance) return;
// Otherwise search for one in the scene
_instance = FindObjectOfType<UpdateDispatcher>();
// Found one?
if(_instance) return;
// Otherwise create it now
_instance = new GameObject(nameof(UpdateDispatcher)).AddComponent<UpdateDispatcher>();
}
private void Awake ()
{
// Does another instance already exist?
if(_instance && _instance != this)
{
// Destroy this one
Destroy (gameObject);
return;
}
// Otherwise assign this as the instance and make sure it isn't destroyed when the scene chsnges
_instance = this;
DontDestroyOnLoad (gameObject);
}
private void Update ()
{
// Call the event every frame if something is registered
OnUpdate?.Invoke ();
}
}
And then you can use ISerislizationCallbackReceiver but not for actually doing the serialization (it is already done automatically for the fields) but rather for registration to the update callback like e.g.
[Serializable]
public class Example, ISerializationCallbackReceiver
{
// By default this has the value "Default String"
public string someString = "Default String";
// This can be edited only vis the Inspector
// by default it is 42
[SerializeField] private float someFloat = 42.0f;
// This is a read-only public access
public float SomeFloat => someFloat;
// Nothing to do here, only needed for the interface
public void OnBeforeSerialize() { }
public void OnAfterDeserialize()
{
// Register to the Update event
// It is save to unregister before registering even if we haven't been registered before
// this makes sure we are registered only exactly once
UpdateDispatcher.OnUpdate -= Update;
UpdateDispatcher.OnUpdate += Update;
}
private void Update ()
{
someFloat += Time.deltaTime;
}
}
This answer should be more like a comment, but due to the extension I've decided to post it here.
The objective of a PropertyDrawer is to display properties differently on the editor.
To achieve that you need 2 things:
1.One class that inherits from PropertyAttribute, this will be the reference used in your future scripts.
2.Another class that inherits from PropertyDrawer, here you can type HOW to display the attribute.
One implementation example of a property drawer that shows an attribute without leting the user to edit it from editor:
using UnityEditor;
using UnityEngine;
public class DisplayWithoutEdit : PropertyAttribute
{
}
[CustomPropertyDrawer(typeof(DisplayWithoutEdit))]
public class DisplayWithoutEditDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label, true);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
GUI.enabled = false;
EditorGUI.PropertyField(position, property, label, true);
GUI.enabled = true;
}
}
Then you can use it on another script doing something like:
[DisplayWithoutEdit] public float randomNumber = 0f;

Unity scriptable object only serializing the unity objects

I am trying to make a spell system. My base class for the system looks like this:
public abstract class MagicBook : ScriptableObject {
public Sprite bookSprite;
public float manaCost;
public float coolDown;
public bool rapidFire;
[HideInInspector] public bool coolDownElapsed;
public virtual void OnActivate() {
}
public virtual void OnActivate(Vector3 position, Quaternion rotation) {
}
}
Then I have another class extending from MagicBook where I override the OnActivate function. My problem now is that in the inspector only the variable bookSprite is showing and all the other values are not there. I tried adding an [SerializeField] in front of the variables and define them new in the extending class. But they still dont show. Has anyone an idea why they are not showing or how I can fix this?
Thanks for your help and time.
Did you create Asset for ScriptableObject?
Add attribute to extended class:
[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/NewBook", order = 1)]
And then create asset with
Another idea:
Probably you have custom drawer for this object, but...
Turn your inspector into debug mode and check if properties are visible there.
Cannot reproduce this issue with Unity 2020.1.0f1.
public class NewBook : MagicBook
{
}

Instantiating random or chosen prefabs with sub-container thru Factory or Pool

I have an array of prefabs and I want to be able to Instantiate randomly picked prefabs thru Zenject Factory and perform their bindings in their sub-containers.
What I want to do is the same as in this code sample from Zenject documentation, but for randomly selected prefabs.
https://github.com/modesttree/Zenject/blob/master/Documentation/SubContainers.md#using-game-object-contexts-no-monobehaviours
using UnityEngine;
using Zenject;
public class GameInstaller : MonoInstaller
{
[SerializeField]
GameObject ShipPrefab;
public override void InstallBindings()
{
Container.BindInterfacesTo<GameRunner>().AsSingle();
Container.BindFactory<float, ShipFacade, ShipFacade.Factory>()
.FromSubContainerResolve().ByNewPrefabInstaller<ShipInstaller>(ShipPrefab);
}
}
I was able to partially make it work with
[SerializeField] private GameObject[] ships;
...
Container.BindFactory<float, ShipFacade, ShipFacade.Factory>()
.FromSubContainerResolve().ByNewGameObjectMethod(SpawnShip);
...
private void SpawnShip(DiContainer container, float speed)
{
container.Bind<ShipFacade>().AsSingle();
container.Bind<Transform>().FromComponentOnRoot();
var shipPrefab = ships[Random.Range(0, ships.Length)];
var ship = container.InstantiatePrefab(shipPrefab);
container.Bind<ShipHealthHandler>().FromNewComponentOn(ship).WhenInjectedInto<ShipFacade>();
container.BindInstance(speed).WhenInjectedInto<ShipInputHandler>();
}
But it's awkward and in this case I guess I'm not using an advantage of sub-container. And also prefabs spawns in an empty GameObject.
What I want to achieve is to be able to use ShipInstaller for sub-container binding.
You're right, there wasn't really a very elegant way to choose the sub-container prefab dynamically.
I took some time to make this better today with this commit. If you use the latest version of Extenject then you can now do things like this:
public class QuxInstaller : Installer {
float _speed;
public QuxInstaller(float speed) {
_speed = speed;
}
public override void InstallBindings() {
Container.BindInstance(_speed);
Container.Bind<QuxFacade>().AsSingle();
}
}
public class CubeInstaller : MonoInstaller
{
public List<GameObject> QuxPrefabs;
public override void InstallBindings()
{
Container.BindFactory<float, QuxFacade, QuxFacade.Factory>()
.FromSubContainerResolve().ByNewPrefabInstaller<QuxInstaller>(ChooseQuxPrefab);
}
UnityEngine.Object ChooseQuxPrefab(InjectContext _) {
return QuxPrefabs[Random.Range(0, QuxPrefabs.Count)];
}
}

Is there a limit to use the [Header("text")] header?

After finding this useful tool I want to use to organize my scripts. However, when I add the third "category", it gives me the following error:
attribute 'Header' is not valid on this decleration type. It is only valid on 'field' declerations.
I have tried to use the "order = x" argument, but without succes. Any idea what's going on? I cant seem to find anything about it in the Unity docs.
[Header("Feedback settings")]
public string gameName = "";
public string sendToEmail = "";
[Space(5)]
[Header("Canvas settings")]
public Sprite emptyStar, fullStar, button;
[Range(20, 100)]
public float canvasSize;
[Range(-1, 1)]
public float canvasXPosition, canvasYPosition;
public float spritePadding, buttonYOffset;
[Header("Rate settings")] //<-- this one is marked with the above error
public enum MarketPlaces {PC, mobileTablet};
public MarketPlaces compileFor = MarketPlaces.PC;
public string rateLink;
Additional code for Joe Blow
[Header("Canvas settings")]
public Sprite emptyStar, fullStar, button;
[Range(20, 100)]
public float canvasSize;
[Range(-1, 1)]
public float canvasXPosition, canvasYPosition;
public float spritePadding, buttonYOffset;
public enum MarketPlaces { PC, mobileTablet };
[Header("Feedback settings")]
public string gameName = "";
public string sendToEmail = "";
[Header("Rate settings")]
public MarketPlaces compileFor = MarketPlaces.PC;
public string rateLink;
[HideInInspector]
public GameObject currentCanvas, tempButton, subCanvas;
private Button[] starButtons;
private Vector2 canvasPosition;
private GameObject rateMeCanvas, rateButton, contactField, openClient;
The way Unity have done it, you can't follow it with an enum.
Fortunately, the solution is simple - just move the enum behind it!
[Header("Feedback settings")]
public string gameName = "";
public string sendToEmail = "";
[Space(5)]
[Header("Canvas settings")]
// not possible...
// public Sprite emptyStar, fullStar, button;
// you must do this...
public Sprite emptyStar;
public Sprite fullStar;
public Sprite button;
[Range(20, 100)]
public float canvasSize;
[Range(-1, 1)]
public float canvasXPosition, canvasYPosition;
public float spritePadding, buttonYOffset;
public enum MarketPlaces {PC, mobileTablet};
[Header("Rate settings")] // just move to here!
public MarketPlaces compileFor = MarketPlaces.PC;
public string rateLink;