#my stupid reload function problem
1 messages · Page 1 of 1 (latest)
is your enum AmmoType? it was ammoType before
go to the enum and rename it to AmmoType
is the AmmoType enum public?
okay so first of all, change it to AmmoType. That's how enums are usually capitalized
is this enum inside another class?
yeah
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
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
then where is this enum declared?
like where is this from
this particular one is in my weaponController script
https://hatebin.com/gnrqenybvf weapon controller
https://hatebin.com/vzugqahhhp player ammo
okay so see how you declared it inside the weaponController class?
then in other scripts you have to access it like this:
weaponController.AmmoType
it as in the enum?
private Dictionary<weaponController.AmmoType, 1> ammoDict = new(); like this?
yeah looks right
your 4 there should be the variable type of your value
you have to do the same thing for those
or just move the public enum AmmoType outside of the weaponController class
it needs to be there for how i want the weapons to be editable in the inspector
i think you need to replace 1 with int
wait hold on, here's another question
then you have to use weaponController.AmmoType to access it 🤷♂️
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
compare yours to Mao's example:
#💻┃code-beginner message
you should put types inside the < >, not values like 4 or 1
I think you may need to wrap the primitive though. It won't return a reference to the variable.
no, you can use it in other scripts too
doing it this way, unless you want to use pointers ;)
I thought that means it'll only be comparing this dictionary to weaponController's AmmoType variable
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
and If I want to use the same AmmoType enum in a different script as a dropdown in the inspector it'll be fine?
yeah
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
what do you mean by access the ammo type, like the class or a variable that has type AmmoType?
the same way I'm "accessing" it at the bottom of here, with playerAmmoScript.lowcalAmmo
not sure how to word what I mean
cause im basically changing playerAmmoScript.lowcalAmmo in the reload function of weaponController
I dont know what any of this means. Is it the ammo count?
is what the ammo count
playerAmmoScript.lowcalAmmo etc.
yeah, it's the reserve ammo count
in your PlayerAmmo script, add a method that returns int
I feel like I'm being annoying for asking, but how exactly would one do that
I was asking in beginner for a reason
something like
public int GetAmmo(AmmoType ammoType)
{
return ammoDict[ammoType];
}
@weak widget is this what the original idea was?
In the switch statement we had the ammo type, so just need to use the dict to get the reference.
yeah
oh that works too.
The dict was just private so i thought you meant to have a method
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
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.
how would that help?
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
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.
i'm not sure how different that is from the PlayerAmmo script and it's own variables though
how I was doing it with switch statements it let me write back to the variable
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
still not sure what I should do, most of this conversation is going over my head still
this didn't exactly work and I'm not sure what it would've done for me if it had
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;
}
idk how you used it
@elder cradle these would go in PlayerAmmo script
just on it's own?
then you could for example access the low cal ammo from other script like this:
int currentAmmo = playerAmmoScript.GetAmmo(AmmoType.lowcalAmmo)
in the same place you had that first line (ammoDict declaration)
next to Start method for example
you probably need weaponController.AmmoType
if its still inside wpncontroller class.
sorry I am just not following any of this I think I need to take a break from coding or something
nah youre almost there
paste full script
this doesn't make sense to me though because the fact that you're still writing playerAmmoScript . . . lowcalAmmo makes me feel like I still have to write it 3 more times in the reload function for high cal, shotgun ammo, and bugblast
more than 4 times given the fact I'll more than likely add more ammo types
so do you want to change all ammo types at once or something?
because this allows that
okay hold on
just use a for loop or something
idk what a for loop is 😭
look right here, in the bottom, in the FinishReloading function
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
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
yeah this is my point
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
this is dynamic, you can just use typeOfAmmo as the ammotype with functions like what I showed
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
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);
oh yeah I just realized what a headache this'll be to understand later
It's alright im sure you're explaining this fine I just have no idea how to code things like a normal person
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.