How to make the architecture of the character doll?

Hello everyone.
I came up with two options. In both options, I have the same problem.
First option. I have a character “RogueSp”, it has an “equipment” script. In this script there is a public variable game object “right hand”. This variable is assigned a prebuff named “sword 1”. This prebuff has a script named “sword”. The “sword” script stores the characteristics of the weapon. These characteristics are made with “struct”. I mean, it’s a copy.

Second option. There is a character “RogueSp”, it has a child object “weapon”. In the “weapon” child object, there is a “sword 1” child object, and so on. The fact that in the first version the prebuff is not displayed on the stage is understandable. Maybe I’ll combine the two.

The problem is that I can’t extract information from the “sword” script. There will be a lot of weapons in the game. Therefore, there will be many such prebuffs with different names and with different names of scripts on them. I learned how to recognize the name of the prebuff in both options. But how to find out the name of the script (component) in the prebuff? That is, how can a script named “sword” pass a reference about itself to the “equipment” script? Or, like the “equipment” script, will it refer to another script without knowing its name?

Not really sure what this question is, but you can use GetComponent() to get things off a given GameObject.

As always, whatever you’re contemplating probably already exists in tutorial form on Youtube if you search with the right terms.

You can also interfaces, which lets you have lots of different things that all express the same interface in different ways.

Using Interfaces in Unity3D:

Check Youtube for other tutorials about interfaces and working in Unity3D. It’s a pretty powerful combination.

I do not know the terms, so the search engine does not give out what is needed. There are only two games on the unity that are remotely similar to my project. They were made by the same studio.Many things there are made easier.

I will read about it, but most likely it will be the same. But it’s quite difficult.

Not everything is so simple. GetComponent<?>() T - unknown. I’m trying to do what I did before in squad management.

I came up with a solution, just list everything. If ( GameObject.name==“sword_1” ) var zxc= GetComponent(). But maybe there is another way. How to pass a link to yourself? Or find out the name T.

The point about interfaces is you don’t need to know the exact T… and you don’t care.

A sword could implement the IEquippable interface.

So could a glove. Or a hat. Or a staff. Or a shield… anything that can be … wait for it … equipped!

If you found some item you could call GetComponent() on it, and a non-null result would tell you that you can use the IEquippable interface.

An example IEquippable interface might implement functions such as

void Equip();
void Unequip();
float GetWeight();
int HowManyHandsDoesThisRequireToEquip();
// etc.

I read about interfaces, I had a few questions. Than “class” from “structure” differs I approximately know. I now try to apply the interface to “structure”. They write what is better to apply to the “class”. What are the features?

I created a simple interface.

 interface IWeapon
    {
        string name();
        bool not_armed();
        bool meele();
        int damag();
        int rang();
        int crit();
        string type_attack_ecipirovan();
    }
    public  struct Long_sword : IWeapon
    {
     
        public string name()
        {
            return "Long Sword";
        }
        public bool not_armed()
        {
            return true;
        }
     
        public bool meele()
        {
            return true;
        }
        public int damag()
        {
            return 8;
        }
   
        public int rang()
        {
            return 0;
        }
                 
        public int crit()
        {
            return 2;
        }
     
        public string type_attack_ecipirovan()
        {
            return "Slashing";
        }
    }
 public struct Dagger : IWeapon
    {
        public string name()
        {
            return "Dagger";
        }
        public bool not_armed()
        {
            return true;
        }
        public bool meele()
        {
            return true;
        }
        public int damag()
        {
            return 4;
        }
        public int rang()
        {
            return 0;
        }   
        public int crit()
        {
            return 2;
        }
        public string type_attack_ecipirovan()
        {
            return "Slashing";
        }
    }

The problem is still the same. When a character picks up a sword, how do you retrieve information about the sword and not the dagger?

You have an object (dagger or sword or any other item) that the player is holding.

You would use :

var weapon = heldItem.GetComponent<IWeapon>();
if (weapon == null)
{
  Debug.Log( "This is NOT a weapon at all!");
}
else
{
  // call weapon.XXX to do anything to the weapon (dagger, sword, does not matter, same code).
}

struct vs class are completely different: Value Types vs Reference Types:

https://discussions.unity.com/t/826396/4

1 Like

Thanks, I’ll try. It turned out that I edited the message after your post, now I will make the original. :slight_smile:

I meant this. “Struct boxing with interfaces?” This is a common question on the Internet. But if you haven’t written about it, then I will use “structure”.

@Kurt-Dekker I made some experiments. I learned how to extract data in this way. But in the original there was such a mechanism, to improve weapons and transfer improved weapons to another. I couldn’t figure out how to do it with just “interface.” The scene can also have clones or copies of this prebuff. How can variables be changed in the script of one copy or clone?
Can all this be done with “struct”? Using this example, I could not combine “interface” and “struct”.