#Okay perfect - yeah we ended back at

1 messages · Page 1 of 1 (latest)

copper marsh
#

hi

#

@forest shuttle we can continue here

#

do you have an Enum already and how does your scriptable objetc look like

hoary swan
#

-# can't find a button to join the thread so this message is just to join the thread

forest shuttle
copper marsh
#

please show both sos

forest shuttle
#
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>

hoary swan
#

(would have to be a SerializedDictionary, an asset, since unity doesn't serialize Dictionarys by default)

copper marsh
#

you dont have to use SerializeField on sos

forest shuttle
#

Okay good to know both :)!

hoary swan
copper marsh
#

well i think the only reason is because they are handled as assets might have other reasons too but i am not sure

hoary swan
#

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

copper marsh
#

would have to research a little on that.

hoary swan
#

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

copper marsh
#

you´d have to move your enums out of the class so you cann access it from the other classes

hoary swan
#

you could still access it namespaced as is

copper marsh
#

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

forest shuttle
#

Would that not essentially be the same as using a dictionary with the category as key?

hoary swan
#

it would change where you keep the info about what type a given weapon is

forest shuttle
#

Or am I missing something?

hoary swan
#

with a dict you define the types in the registry
with this impl you define the types in the individual SOs

forest shuttle
#

Ohh right, of course!

copper marsh
#

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

forest shuttle
#

Yeah I guess there is no reason to store it in the Weapon object if it is defined in the dict

copper marsh
#

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

forest shuttle
forest shuttle
hoary swan
copper marsh
#

in the end it is up to you, we only suggested a few working soloutions

forest shuttle
#

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?

hoary swan
#

with just 3 elements probably not

#

because of how cpu caching works

#

actually there's a lot of complications

forest shuttle
#

Yeah that's what I mean that maybe not in my case, but if it was a significantly larger set

hoary swan
#

yeah

#

dict access is O(1) amortized
the list solution is O(N)

#

but keep in mind this is worst case

forest shuttle
#

Yess makes sense

#

Yeah it shouldn't be a problem :D

hoary swan
#

eg if it's the first one in the array then a dictionary would always be slower

forest shuttle
hoary swan
#

having a reverse mapping is often handy, but it depends on how you're using the SO, what you need it to do, etc