#Okay perfect - yeah we ended back at
1 messages · Page 1 of 1 (latest)
hi
@forest shuttle we can continue here
do you have an Enum already and how does your scriptable objetc look like
-# can't find a button to join the thread so this message is just to join the thread
I do, I have WeaponType which contains the different weapons e.g. Gun, Knife etc.
please show both sos
public class Weapon : ScriptableObject
{
public enum WeaponType
{
Gun,
Knife,
Grenade
}
public enum WeaponSlot
{
Primary = 0,
Pistol = 1,
Melee = 2,
Throwable = 3
}
[SerializeField]
private WeaponSlot weaponSlot;
[SerializeField]
private WeaponType weaponType;
[SerializeField]
private float fireRate, range, damage, recoil, reloadTime;
[SerializeField]
private bool automatic;
[SerializeField]
private int maxAmmo;
[SerializeField]
private bool scoped;
[SerializeField]
private Sprite texture;
[SerializeField]
private string weaponName;
}
So this is the Weapon SO
With the Knife asset (as an example) looking like this
And
public class WeaponRegistry : ScriptableObject
{
[SerializeField]
private Weapon[] weapons;
}
This is just the WeaponRegistry SO, which contains the Weapon assets
And if I understood Chris' suggestion correctly, it was to change the weapons array to a dictionary using <WeaponType, Weapon>
(would have to be a SerializedDictionary, an asset, since unity doesn't serialize Dictionarys by default)
you dont have to use SerializeField on sos
Okay good to know both :)!
really? does it not follow unity's typical serialization rules, or what exactly?
well i think the only reason is because they are handled as assets might have other reasons too but i am not sure
assets would have transient fields too though. would they just need a lot of [Nonserialized] for that case?
-# i don't remember the exact name
would have to research a little on that.
could just open like inputactionsasset to check probably
or a settings asset
(the source class i mean)
i won't have a way to do that for a few days so just thinking out loud
you´d have to move your enums out of the class so you cann access it from the other classes
you could still access it namespaced as is
if you change your array to list you could use somehtign like this afteron
GetWeapon(Category.Rifle); //call
Weapon GetWeapon(Category category)
{
var weapon = weapons.Find(w => w.category == category);
return weapon;
}
so you only pass in the type and it wil find the type you are looking for
you could also use the id or whatver you want
Would that not essentially be the same as using a dictionary with the category as key?
it would change where you keep the info about what type a given weapon is
Wouldn't it just change how I obtain the info? If the Category (or WeaponType) enum is stored in the Weapon object, and the array or list are stored in the WeaponRegistry still?
Or am I missing something?
with a dict you define the types in the registry
with this impl you define the types in the individual SOs
Ohh right, of course!
i mean you could simply create an id field on your weapon and find it like that too, but i thought you ar elooking for a way to call the specific type
Yeah I guess there is no reason to store it in the Weapon object if it is defined in the dict
there are many ways to do that but i would really not work with a dictionary to compare weapon and weaponregistry array
it is kind of unprofessionel
I mean it's kind of same difference for me, as each type corresponds to a single weapon object anyway
No I wouldn't be comparing any array if I was to use a dictionary, the WeaponType would map directly to a Weapon object
it may still be useful to have for other reasons, which would introduce issues with duplication and source of truth. that could be a potential issue with using the dict
in the end it is up to you, we only suggested a few working soloutions
That's true yes
Yes thank you a lot both! :)
I know it's negligible in my case, but using a dictionary would be faster, no?
with just 3 elements probably not
because of how cpu caching works
actually there's a lot of complications
Yeah that's what I mean that maybe not in my case, but if it was a significantly larger set
yeah
dict access is O(1) amortized
the list solution is O(N)
but keep in mind this is worst case
eg if it's the first one in the array then a dictionary would always be slower
When you say it may still be useful for other reasons, are you thinking of something specific, or just in case I would need to access the weapon type of a given weapon at some point?
having a reverse mapping is often handy, but it depends on how you're using the SO, what you need it to do, etc