#Hotbar Help

1 messages · Page 1 of 1 (latest)

true loom
#

e

mighty nacelle
#
public class Inventory {
  Hotbar[] Hotbars = new[5];
}
public class Hotbar {
  Slot[] Slots = new[6];
}
true loom
#

ok

#

so i have 6 slots

#

and 5 hotbars

#

and id use those

#

wait

#

why they in there own class

limpid stratus
#

You dont understand arrays as you said you do

mighty nacelle
#

So they can have methods on them to run self-contained logic

true loom
#

i get errors on this

mighty nacelle
#

it was an example of the structure

#

not code to copy

true loom
#

oh ok

#

im gonna read a doc on arrays then ill come back

restive hollow
#

Your C# version is probably too old to use the new[x] you need to use new Hotbar[x] etc.

true loom
#

ye ok

#

i read a doc

#

and it didnt tell me why it should go in its own class

restive hollow
#

Whats the problem with making new classes?

true loom
#

idk why

#

why do i need to

mighty nacelle
#

You're just using GameObject, which has no useful functionality related to the inventory slots, the hotbar, etc. I imagine your code is all this monolithic stuff in Inventory

#

whereas I would put the code that is about the hotbar in the hotbar class. The code that sets up the slot in the slot class

true loom
#

right

mighty nacelle
#

when you create a new hotbar to use it, you don't have to change any code, you just access the new index of your hotbar array and call the method. The code looks exactly the same

true loom
#
public class Hotbar
    {
        Hotbar[] Hotbars = new Hotbar[5];
    }
    public class Slot
    {
        Slot[] Slots = new Slot[6];
    }```
#

so i have this rn

#

so declaring hotbar as 5 means it has a size of 5 correct?

mighty nacelle
#

Sure, 5 empty elements. It won't do anything useful, it was an example of structure vs your numbered lists

true loom
#

but i have 5 hotbars so i should declare it as 4 because i will use 0

#

same with slots i should make that 5

mighty nacelle
#

No

true loom
#

ok

#

so now my question is how do i access these from outside the skript, i want to be able to drag in say a prefab to slot 2 of hotbar 3

#

is that possible with this setup

mighty nacelle
#

The hotbar could be serializable, and the slots could be monobehaviours on your slot prefabs that are dragged into that array in the inventory

#

your above code is also wrong, the hotbar shouldn't have a hotbars array, the inventory has that.

true loom
#

wouldnt i make them new gameObject

mighty nacelle
#

I do not have the time to lead you through the setup and coding of this beyond what I've mentioned. If you don't get it, I'd only recommend watching some tutorials, or just trying to make it work with your original setup

true loom
#

oh ok ig u just give up rip, time to watch tutorials

#

have u atleast got a good tutorial to link me?

#

because the tutorial im watching uses lists just like me

true loom
restive hollow
true loom
#

so if i make the class public ill see it?

restive hollow
#

yes

#

Though with "see it" do you mean in the inspector?

true loom
#

i mean that is 1 goal

#

but i made it public and i still cant access it

restive hollow
#

For unity to save or show your custom classes (Hotbar, Slots) they need the [System.Serializable] attribute

true loom
#

so i add [SerializeField] at the start?

restive hollow
#
[System.Serializable]
class Hotbar``` etc
#

And with etc I mean add it to the Slot class also

true loom
#

ye did

#

still cant see the array

#

or access it

restive hollow
#

Like I said

true loom
#

ok

#

still the same

#
[System.Serializable]
    class Hotbar
    {
        public Slot[] slots = new Slot[6];
    }
    [System.Serializable]
    class Slot
    {
        
    }```
#

if i type "slots" it says it dont exist

#

thats outside of the hotbar class

restive hollow
#

You need to get a reference to an instance of a Hotbar

true loom
#

ohh ok

#

but i still cant see in inspector either

restive hollow
#

Like lets say you want the first:cs Hotbar firstHotbar = Hotbars[0];
Then you access its slots withcs firstHotbar.slots. //...

true loom
#

ok

restive hollow
#

It needs to be public or [SerializeField]

true loom
#
[SerializeField] Hotbar[] Hotbars = new Hotbar[6];

    [System.Serializable]
    class Hotbar
    {
        public Slot[] slots = new Slot[6];
    }
    [System.Serializable]
    class Slot
    {
        
    }``` like this?
restive hollow
#

Yeah, but now that I look at it, that just makes an array of 6 null objects I think

#

Change this: cs [SerializeField] Hotbar[] Hotbars = new Hotbar[6];
To a list:cs [SerializeField] List<Hotbar> Hotbars = new List<Hotbar>();

true loom
#

alr done

restive hollow
#

Can you see the Hotbars list in the inspector of Inventory?

true loom
#

this is truly beautiful

#

thankyou

restive hollow
#

Nice, just to make sure, add a public variable to Slot class and see if you can see it in that list?

true loom
#

add a public var to slot

#

?

#
public Slot[] slots = new Slot[6];```
#

its public

restive hollow
#
    [System.Serializable]
    class Slot
    {
        public int testInt = 123;
    }```
true loom
restive hollow
#

Ok yeah just making sure

#

Remove that and now you can proceed with whatever you wanted to make

true loom
#

so if i just changed that to a gameobject i would be able to change every slot of every hotbar

#

danm this is way better

#

thanks

restive hollow
#

Yeah

true loom
#

one final thing

#

see how they all say g

#

can i make it like slot1 slot2 slot3 etc

#

oh wait nvm

#

i dont needa do that

#

i see now