#make a thread real quick
1 messages · Page 1 of 1 (latest)
here
public void AddItem(ItemData item) //Adds item into Inventory
{
for (int i = 0; i < inventorySlots.Length; i++)
{
if (inventorySlots[i] == item)
{
continue;
}
if (inventorySlots[i] != item)
{
inventorySlots[i].itemInSlot = item;
break;
}
}
}
i believe this should work for adding items to the inventory
the continue part is unecessary (the whole first if statement)
and the other part will replace the first different item it seems
rather than finding an empty slot
you also want to handle the case where there aren't any empty slots found somehow
yeah
uhh
slotv.transform.GetComponent<Image>().sprite = slotv.GetComponent<Slot>().itemInSlot.icon;
why
you already have the slot you're like working backwards
shouldn't the slot class just have a SetItem function that does all that?
do i not need to change the slot objects image sprite to the item thats inside it?
i.e. rather than inventorySlots[i].itemInSlot = item; you should be doing inventorySlots[i].SetItem(item);
unless itemInSlot is a property
inventory manager should not care about UI
in which case the setter can handle things
That's true also^
the UI should listen to changes in the inventory and update things via the observer pattern
itemInSlot is a field
then you shouldn't set it directly, you should use a setter function or a property
so you can do things like fire off an event or update the UI when it changes
oh ive never done that before
they're very simple to do its just like calling a method
SetItem is in the slot
oh ok
inventory manager calls it
Slot deals with changing the item and invoke a possible UI change
if events is too complex for now just call .sprite = item.sprite from the Slot in the same method
but you should consider making UI separate script
public void SetItem(ItemData item)
{
itemInSlot = item;
}
id rather make it well
get set
yes you can do it as a property as well
ive seen this but idk what it does or how to use it
e.g.
ItemData _itemInSlot;
public ItemData ItemInSlot {
get => _itemInSlot;
set {
_itemInSlot = value;
UpdateUIWithNewItem(value);
}
}```
they're special methods that do something when you do anything with itemslot
it's just like running a function when you do
something = theProperty < calls the getter
or
theProperty = something < calls the setter
well we're defining what happens when you get and what happens when you set
set is inside of get
=> is a special operator
get => _itemInSlot; is just shorthand for:
get {
return _itemInSlot;
}```
this replaces the SetItem method entirely
im unsure where to put the get and set
in the ItemSlot class
This^
public ItemData SetItem
{
get { return itemInSlot; }
set { itemInSlot = value; }
}
ItemInSlot
then you put the item in the slot if you ran the method like, ItemInSlot(itemAdded)
public ItemData ItemInSlot
{
get { return itemInSlot; }
set { itemInSlot = value; }
}
sure except remember the whole point of this was so we can update the UI cleanly when the item changes
ye
so you'll want to add that to the set
UpdateUIWithNewItem(value);
yeah that was just an example though - idk how you actually have things set up
public void AddItem(ItemData item) //Adds item into Inventory
{
for (int i = 0; i < inventorySlots.Length; i++)
{
if (inventorySlots[i] == null)
{
inventorySlots[i].itemInSlot = item;
break;
}
}
}
Ideally you'd want some type of event or method
do i need to change this then?
this is wrong
thought so
You need to set ItemInSlot (the property) not the field
which is why the field should be private
to prevent you from making a mistake like this
ItemInSlot(item);
no
just change itemInSlot to ItemInSlot
also instead of if (inventorySlots[i] == null) it should be if (inventorySlots[i].ItemInSlot == null)
or perhaps make an IsEmpty property
cant cause ItemInSlot is private
wat
^
Again
THE PROPERTY
not the field
the property is public
the property is called ItemInSlot
notice capitalization
the field should be private. the field is called itemInSlot
I'll also note in my example above I used _itemInSlot for the field name to avoid this confusion further
yeah i had what your saying
[Header("Item inside of the slot")]
public ItemData _itemInSlot;
public ItemData ItemInSlot
{
get { return _itemInSlot; }
set { _itemInSlot = value; /*UpdateUIWithNewItem(value);*/ }
}
public ItemData _itemInSlot;
Should not be public
if you want it in the inspector use [SerializeField]
but not on what i pasted
ye ik
how would i make it Update with UI now
create and implement this function UpdateUIWithNewItem(value);
wait but first
public void AddItem(ItemData item) //Adds item into Inventory
{
for (int i = 0; i < inventorySlots.Length; i++)
{
if (inventorySlots[i].ItemInSlot == null)
{
inventorySlots[i].ItemInSlot = item;
break;
}
}
}
is this correct then?
I would even put return there instead of break. Then you can put code outside the for loop to handle the case of no empty slot being found (however you want to handle that)
maybe you have this function return a bool to indicate whether the item was successfully added or not for example
so should i make a local bool that is whether an item was added or not added
i used a return instead
does return end any loop?
it returns out of anything
oh ok
then just this
where would i put the function
is the set's value supposed to be returned or something so i can use it for this new function?
how would i use value in the
UpdateUIWithNewItem(value)
from ^
thats ur ItemInSlot being passed
It's a parameter to the function
it goes into the function the same way parameters go into any function
e.g.
void UpdateUIWithNewItem(ItemData newItem) {
// do whatever you want
}```
public void UpdateUIWithNewItem(ItemData value)
{
}
I wouldn't make it public
idk why whenever i make the code block is makes it tabbed weird
If your IDE is doing that then you probably have some incorrect indentation somewhere
📃 Large Code Blocks
Use links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/, https://paste.myst.rs/, https://hastebin.com/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
making this^
doesn't happen to me ¯_(ツ)_/¯
idk but its whatever
on discord ? i just a style of writing function
can someone explain like what this function is doing
how is it being useful?
because everytime you access it , that function runs
alright
its useful because it can trigger something to happen if slot has changed item
without explicitly writing a function and calling that function every time
the function runs itself when you make change or get the prop
for example updating the UI
with item/s
it works exactly the same as a function
so whatever you put it in runs
oh cool
im gonna need to have more experience with this to get better but this is a start for some deeper coding
well yeah regardless of what you're doing you're gonna have to learn properties some time
they're essential really
i spend my study halls reading c# coding
so ill eventually learn more
but also now
all you're doing is making your code modular and not a nesting mess
when you introduce events into that you'll start to see that
code modular?
yeah where you can more or less add to it / remove to it without breaking everything
public void UpdateUIWithNewItem(ItemData value)
{
// Slot.GetComponent<Image>().sprite = value.icon;
}
so i need the slots actual sprite to change to its items now
how would i reference the actual image?
for example if your inventory cant find a sprite or something the whole system breaks instead of just a slot breaking
yeah thats true
easier to work with
well first you want an Image component refrence already stored on each slot
although you should ideally have another script that only deals with UI elements
imgComponentReference.sprite = value.sprite
yeah
and imgComponentReference is a variable
thats equal to the slot objects image
how do i reference another component from one component
like from the Slot component (script) to Image component
[SerializeField] private Image _iconImg
and drag it in for each slot?
you're breaking down responsibilities really for the most part
just like people
you wouldnt want to give 1 person doing 20 different things
it can be done, but its overwhelming confusing and prone to mistakes
thats true
your code right now would say Hi I'm a Slot and I take care of this Slot right here #12 or w/e
so each slot does their own job
Trust me Im an infant in comparison to the big fish here
but I learned by doing trial and error basically lol
how did the better coders here learn?
realizing that 500+ lines of script is NOT the way to go
i assume c# isnt the only language they know tho
everyone has different backgrounds and experiences 🤷♂️
some have formal school training sure
you don't need to go to school for it but it helps knowing the algorithms and such at your disposable
more importantly when to use what
im only 15 so i somewhat lack that ability atm...
nobody at my school could help me at all cause i know 100 times more then them
and yet when doing it i feel so bad
i had to literally force myself through a super big learning curve to get where i am now
its usually a problem when you try to do too much at once
not that online resources for learning are bad... but they teach as if i already know a decent amount
i mean i do know, but when i first began
im probably on month 3-4
(but to be fair i go to school like everyday)
practice / repetition makes you good
just keep doing it until your brain can just do it without thinking
i mean im here everyday cause i code and work on my game daily
so i basically do that
just remember at the end of the day its not about so much the particular syntax
its the Logic you learn
learning to think differently literally
you can use any language really
all logic pretty much translates across
i started with c++
then python
now im here
because the only resources i had were for those
I started with C++ but by accident
c++ is similar
yeah but harder
I was making mod for Hl2 mods
and source engine runs on C++
so luckly it wasnt pure C++ but an engine but still ,
its poorly documented engine
so I gave up
i plan to do more with c++
switched to unity
only cause of ue5
but i heard it has a load of vis scripting
visual scripting sucks
i dont like it personally
i trieddoing a raycast with nodes and it looked atrocious
so many nodes and stuff
I can do that in 1 line of code in unity
amazing stuff
my friends keep telling me to use ue instead of unity
unreal engine
but unity feels way better
idk
ehh they're just tools, use whatever you can cook up more shit with
mostly cause they want me to make super realistic battle royale whatever games
"Realistic battle royal"
yeah i agree
I see so many games with amazing graphics (asset flips)
and the game itself sucks a fat dookie
roblox nowadays has games with amazing graphics
some of the best games have the shittiest graphics usually
but also fun
my game im attempting to make is something ive dreamed of for years
you can say Tarkov looks really good for a unity game
it does
many other
im a hypixel skyblock veteran so ive always liked mmorpg type games
but i also loved 2D top down games
such as
enter the gungeon
i play those too
but during study hall at school lol
but anyways
ive wanted to make a bullet hell 2D mmorpg
when i first began coding i attempted to make it right away and failed horribly
ALTHOUGH
even veterans around here will tell ya
multiplayer is really not a good first project lol
or second
or third
lol
i managed to get a player to move well, colliders to work, weapon swing, health, speed, critical hitting, health and enemies
i gave up when i had to make the inventory
yeah ik
after i failed i began the unity create with code
managining items, ingame economy 😵💫
yeah fr
but i got bored after 2/3 through so i just started learning other stuff
i made
a cookie clicker game that had its own card packs with rarities you could buy with cookies to eventually make more cookies
^my first taste of the most simple inventory you could have
a 3D bean shooter, where you ran around and shot capsules
so this is probbaly my 4th or 5th projec
yeah inventory is tough but if you odo it clean it can easily extend
the goal is : you will make one so good that you will make your own asset you use in different projects
one step closer
moving it is with UI script
this gets tricky because of working with Unity canvas is annoying
but doable
should i make it drag and drop right away, or just select slot then selected slot to move to
yeah...
thats up to you
well
ive never sucsesfully done either
and drag and drop sounds very complex to me
I can show an example of one of my card games and how I move them between the same slots
ok