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?
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.
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:
Thanks, I’ll try. It turned out that I edited the message after your post, now I will make the original.
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”.