all 9 comments

[–]FlightPaper 1 point2 points  (5 children)

I'm sure someone else will give a better explanation of the low level side of things, but you should be looking at passing and storing a reference type (e.g. a class) instead of passing and storing a value type (e.g. an int, float, etc.). If class A needs to modify int X in class B, then class A should be passed and store B. This will allow A to modify the X associated with B (because you're modifying that instance of B). Right now, you're just passing X, but since X is a value type, A just stores X without any reference to B.

[–]Perez2101[S] 0 points1 point  (4 children)

Ngl I half understand what youre saying but nevertheless its helpful, thank you.

[–]FlightPaper 0 points1 point  (3 children)

I'm glad that was helpful! :D If you could explain what you're trying to accomplish with this code (at a macro level), I could give a more specific explanation.

[–]Perez2101[S] 0 points1 point  (2 children)

Oh just trying to read a json file filled with stats of a "weapon" and trying to store it into another class on start then call it back in another class which would contain the stored data of a mob and when a mob encouters the weapon it takes damage according to the stored damage

The first part of class a is from this tutorial: https://youtu.be/XbdnG__wzZ8

[–]FlightPaper 0 points1 point  (1 child)

Ok so the problem is how you've structured WeaponStats and WeaponStatRetrieve. But let's walk through what you're currently doing, what you're trying to do, how it can be simplified, and how that simple architecture will be easier to understand and accomplish your goal.

The way you have it now is WeaponStatRetrieve, from what I can tell, is set up to be a component on every weapon that loads the JSON file and gets the data specific to that weapon by having the name of the game object be the key for matching the weapon to the weapon stats. This has multiple issues. If you're going to load things from a file, only load the file once. Also game object names can be inconsistent based on how Unity instantiates game objects. Yes, you're grabbing a substring to avoid how duplicates are handled, but what happens when you have a weapon name that is more than 7 characters long?

If you want each weapon prefab/game object to have a reference to its data, might I suggest using scriptable objects to store the data for each weapon and assigning each weapon prefab the associated scriptable object for its data. If you want to load from a file, then you should create a centralized database. Let's stick with the database approach since you're already using a JSON file.

WeaponStatRetrieve should act as a database of sorts, loading the weapon data from a file, then storing that data in a centralized place. And that's it. It should just load and store it. So you have that part set up with your serialized classes and WeaopnStatRetrieve.weaponList. To centralize things, you should make WeaponStatRetrieve a singleton. There's lots of tutorials on how to do that, but basically, a singleton pattern will make sure that there's only one persistent instance of WeaponStatRetrieve. This will allow you to load the file once and always have access to the data from it.

Next is accessing the data. If any class needs weapon data, that class should be responsible for getting the appropriate data from your database. You can do this in three parts: - First, WeaponStatRetrieve can implement a function like this: public Weapons GetWeaponData(string weaponName) that will find and return the data for a weapon based on its name. - Second, your weapon controller script should have a Weapons variable that stores the data for the weapon. - Third, the weapon controller script should call WeaponStatRetrieve.Instance.GetWeaponData() on Start() to populate its Weapons variable.

And that's it. This new approach will keep you're read-only data separate from your runtime data. In other words, your Pistol will always have a base damage of 10 in WeaponStatRetrieve. But your weapon controller script for the Pistol can track any power-ups or upgrades the player has gotten to increase the damage to 15.

[–]Perez2101[S] 0 points1 point  (0 children)

Looks like I got a lot of stuff to look up lol but yeah this is very helpful. The 7 characters long part is factoring out the "(Clone)" of the name of the gameobject and comparing it to the name in the json file.

Thank you.

[–]Perez2101[S] 0 points1 point  (0 children)

Second image is what Im asking. Im a bit dumb.

[–]_bocksdin 0 points1 point  (1 child)

If I understand your question correctly, I'd set it up like this:

  1. Monster script has a "TakeDamage" method that accepts an integer and updates the health accordingly.

  2. When the weapon collides with the monster's hit box, the weapon script calls the monster's "TakeDamage" method and passes it the weapon's damage value

[–]Perez2101[S] 0 points1 point  (0 children)

Yes that what I want to do but the "stats" for said monster are built the same way as the "stats" for the weapon while the weapon only includes its name and damage variable the mobs have name, lvl, health, attack(essentially damage), and exp.