#my stupid reload function problem

1 messages · Page 1 of 1 (latest)

elder cradle
#

@weak widget

hasty crown
#

go to the enum and rename it to AmmoType

elder cradle
#

my bad it was ammoType

#

still, same error

hasty crown
#

is the AmmoType enum public?

elder cradle
#

it says this

#

yeah but it's in a different script

hasty crown
#

okay so first of all, change it to AmmoType. That's how enums are usually capitalized

hasty crown
elder cradle
#

yeah

hasty crown
#

if yes, you need to reference it with MyClass.AmmoType.lowcalAmmo etc

#

or move it outside of the class, it can be in the same file

elder cradle
#

someone told me to put the dictionary in the place where the ammo variables are declared

#

mao did

#

but where the ammo variables are being declared, in the playerAmmo script, is not where that enum is

hasty crown
hasty crown
elder cradle
#

this particular one is in my weaponController script

hasty crown
#

then in other scripts you have to access it like this:
weaponController.AmmoType

elder cradle
#

it as in the enum?

#

private Dictionary<weaponController.AmmoType, 1> ammoDict = new(); like this?

hasty crown
#

yeah looks right

elder cradle
#

alright

#

the rest is still showing up red

weak widget
#

your 4 there should be the variable type of your value

hasty crown
weak widget
#

oh wait actually if it's a value type it may need to be boxed

#

uhh

hasty crown
#

or just move the public enum AmmoType outside of the weaponController class

elder cradle
#

it needs to be there for how i want the weapons to be editable in the inspector

hasty crown
elder cradle
#

wait hold on, here's another question

hasty crown
elder cradle
#

if i'm putting weaponController.AmmoType in the dictionary, I'll only be able to use this dictionary for the weaponController script right?

#

I thought i'd be able to access this script from anywhere and compare it to an AmmoType

hasty crown
#

you should put types inside the < >, not values like 4 or 1

elder cradle
#

yeah I replaced the floats with int

#

no errors there

weak widget
#

I think you may need to wrap the primitive though. It won't return a reference to the variable.

hasty crown
weak widget
#

doing it this way, unless you want to use pointers ;)

elder cradle
#

I thought that means it'll only be comparing this dictionary to weaponController's AmmoType variable

hasty crown
#

the only reason you add weaponController. is because your AmmoType enum is declared inside weaponController class.

#

you can freely move it outside of the class

elder cradle
#

and If I want to use the same AmmoType enum in a different script as a dropdown in the inspector it'll be fine?

elder cradle
#

ok good

#

now that's out of the way, hopefully my last question is, how do I access the ammo type from the weaponController script now?

#

in the same way I was doing it with the switch statements

hasty crown
elder cradle
#

not sure how to word what I mean

#

cause im basically changing playerAmmoScript.lowcalAmmo in the reload function of weaponController

hasty crown
#

I dont know what any of this means. Is it the ammo count?

elder cradle
#

is what the ammo count

hasty crown
#

playerAmmoScript.lowcalAmmo etc.

elder cradle
#

yeah, it's the reserve ammo count

hasty crown
#

in your PlayerAmmo script, add a method that returns int

elder cradle
#

I feel like I'm being annoying for asking, but how exactly would one do that

#

I was asking in beginner for a reason

hasty crown
#

something like

public int GetAmmo(AmmoType ammoType)
{
  return ammoDict[ammoType];
}
#

@weak widget is this what the original idea was?

weak widget
#

In the switch statement we had the ammo type, so just need to use the dict to get the reference.

elder cradle
#

yeah

hasty crown
#

oh that works too.

#

The dict was just private so i thought you meant to have a method

elder cradle
#

the simplest way to explain what I wanted is to do what the switch statement is already doing, but not having to do it for each new ammo type I add

weak widget
#

Problem though is I am like 99% certain we'd want to box the value type cause it will just return a value from the dictionary and not a reference.

#

and since this isnt c++ it's kinda a pain lmao

#

but if it comes to it, you may need to make like a AmmoType class with a single variable called value in it.

elder cradle
#

how would that help?

hasty crown
#

so that you could access your ammo with a reference to it, instead of a value (like int)

#

if I understood correctly

#

you would access a class that holds the int

weak widget
#

yeah, cause currently how it's set up, you will receive a 'copy' of the value, so you can't really write back to it.

elder cradle
#

i'm not sure how different that is from the PlayerAmmo script and it's own variables though

elder cradle
weak widget
#

so usually what you do in c# is to box your value types for stuff like this unfortunately.

#

yeah, but this techniques involves linking references, but what we're doing is maping a enum to a value type.

#

still very useful to remember though, since this would be the exact scenario to use it otherwise

elder cradle
#

still not sure what I should do, most of this conversation is going over my head still

elder cradle
hasty crown
#

I would personally probably just do this

    Dictionary<AmmoType, int> ammoDict = new Dictionary<AmmoType, int>();
    public int GetAmmo(AmmoType ammoType)
    {
        return ammoDict[ammoType];
    }
    public void SetAmmo(AmmoType ammoType, int ammoCount)
    {
        ammoDict[ammoType] = ammoCount;
    }
weak widget
#

Yeah, that's pretty cool idea

#

I second that

hasty crown
#

@elder cradle these would go in PlayerAmmo script

elder cradle
#

just on it's own?

hasty crown
#

then you could for example access the low cal ammo from other script like this:
int currentAmmo = playerAmmoScript.GetAmmo(AmmoType.lowcalAmmo)

hasty crown
#

next to Start method for example

hasty crown
#

if its still inside wpncontroller class.

elder cradle
#

sorry I am just not following any of this I think I need to take a break from coding or something

hasty crown
#

nah youre almost there

hasty crown
elder cradle
#

more than 4 times given the fact I'll more than likely add more ammo types

hasty crown
#

because this allows that

elder cradle
#

okay hold on

hasty crown
#

just use a for loop or something

elder cradle
#

idk what a for loop is 😭

#

look right here, in the bottom, in the FinishReloading function

hasty crown
#

just say the line number lol

#

i see tho

elder cradle
#

144-185

#

I made a switch statement with tons of references to playerAmmoScript, and then the ammo type for each specific case

#

this works, yes

#

for me to make it work with the other 3 ammo types though, I'd have to copy line 147-168 into each case and then change each instance of lowcalAmmo to highcalAmmo etc...

#

not only does this give me a whole lot of work to do every time I add another ammo type, it's also a whole lot of repeating code which I've heard is bad

weak widget
#

i dont think you'd need the switch statement anymore once you get this working

#

you just enter the key into the dict and you will know what you're working with right away

elder cradle
#

after we get this working I wont have to touch the weaponscript when I add another ammo type either will I?

#

cause that's mostly part of the problem im having

#

I would love to just have it dynamically function with just me having to add a new ammo type into PlayerAmmo and not have to modify the reload function each time

hasty crown
#

this is dynamic, you can just use typeOfAmmo as the ammotype with functions like what I showed

elder cradle
#

okay that's what i've been imagining in my head, which is good

#

now the only problem is trying to get my snail brain to understand what you're saying

hasty crown
#

so you would replace your whole current switch statement with stuff like

  // Get ammo count
  int ammo = playerAmmoScript.GetAmmo(typeOfAmmo);
  
  /* Change, check ammo count etc. */
  if(ammo > 0)
  {
    ammo--;
  }
  // Etc

  // Update ammo count
  playerAmmoScript.SetAmmo(typeOfAmmo, ammo);
elder cradle
#

oh yeah I just realized what a headache this'll be to understand later

hasty crown
#

yeah I can't really explain this better right now

#

try some stuff out later

elder cradle
#

It's alright im sure you're explaining this fine I just have no idea how to code things like a normal person

weak widget
#

dont worry too much about the optimization of your code if you're still learning. Just get your stuff in a working state and revise your code later.