- Home /
Structs in C# Question
I am trying to use structs in unity for an inventory system. The struct has all of the variables that will be used for various types of information. I am trying to create a class that copies the struct in order to create a database for each item, so that other scripts can access this database to obtain information for each item. (if there is actually better logic for this please help me out, i have only just started doing anything like an inventory) here's my code. Please bear with my. I've just started structs:
The Struct:
using System;
public struct ItemObject {
private string itemName;
private string itemDescription;
private int itemID;
private string weaponType;
private int itemDamage;
private int itemAccuracy;
private int itemSpeed;
public ItemObject () {
itemName = "name";
itemDescription = "description";
itemID = 0;
weaponType = "melee";
itemDamage = 2;
itemAccuracy = 4;
itemSpeed = 2;
}
public string Name {
get { return itemName; }
set { itemName = value; }
}
public string Description {
get { return itemDescription; }
set { itemDescription = value; }
}
public int ID {
get { return itemID; }
set { itemID = value; }
}
public string Type {
get { return weaponType; }
set { weaponType = value; }
}
public int Damage {
get { return itemDamage; }
set { itemDamage = value; }
}
public int Accuracy {
get { return itemAccuracy; }
set { itemAccuracy = value; }
}
public int Speed {
get { return itemSpeed; }
set { itemSpeed = value; }
}
}
And the "database": (barely anything, my problem is that when i try and do this unity says "Unexpected symbol "='"...
using System;
public class ItemDatabase {
ItemObject item0;
item0.Name = "name";
}
And also, needless to say and not to be rude, id prefer not to be treated like a noob. I have a clue to what im doing. I understand a lot of people on these forums see this kind of question and respond in a way such as "well, i think your too inexperienced and should start with something a little simpler." I am inexperienced in this subject of coding. But I prefer a sophisticated response. Ill know what your talking about.
Thank you in advance for any help!
-myjean17
A stuct is very helpful when you need something that cannot have a null value, or where passing by value is preferable. I believe that they are also not processed through the normal garbage collector system because they are not objects but rather similar to ints and booleans. Not being actual objects make them ideal for when you need to create lots of short lived instances.
I not treat him as a noob. I asked the question about the struct to him. Because to use a class could be the better method, depending on the scenario. If he want to seriliaze the objects, it might be easier with structs. But propably he could use a class ins$$anonymous$$d of the struct and include the save/restore methods within that class.
Answer by swisscoder · Jan 29, 2012 at 06:54 PM
you can only assign the item0.Name within the constructor or a method of the class!
to better explain what I mean:
using System;
public class ItemDatabase {
ItemObject item0;
item0.Name = "name"; //not possible!
public ItemDatabase(){
item0.Name = "name"; //possible! (inside ctor)
}
public void init()
{
item0.Name = "name"; //possible! (inside method)
}
}
um.... ItemObject.Name
is a C# property (it can be used as a normal variable but it acts as getter/setter methods).
So, you can't really 'assign' item0.Name
.
Also, this is kind of what he meant when he said that he does not want to be treated like a noob. Your answer is not all that sophisticated nor does it seem correct (from what I understand).
Ah.. feeling like a noob. I've been messing around with the code and forgot to change it to 'item1.Name' before I posted it on the forums. Edited my post, but anyways, I used the microsoft C# reference to accomplish some of this, so obviously it's implemented a bit differently in unity.
WTF! I help and write why it doesn't work. I made a exclamation mark, because it's the actual reason that myjean17 still is getting the error! In no line I offended myjean17!
@rabbitfang: even you can not assign a property of a localy defined variable outside a method or the constructor!
I agree with swisscoder. This corrects the problem with the code in the question.
Ah yes this corrects the problem. Thanks for the help swisscoder!
Answer by rabbitfang · Jan 29, 2012 at 06:38 PM
It would seem that item0.itemName
is a private member. Try using item0.Name
instead.
Also, when you ask noobish questions, you are then treated like a noob. However, I would not call this a noobish question, especially because you seem to understand the concept of constructors and C# properties. This is just a simple mistake that can happen often, much like a simple spelling error in code happens more often than people would like to admit.
$$anonymous$$y mistake- i did mean to change it back to 'item0.Name' before I posted it. Now, when i do that, everything seems to work except for that one line, which causes the error i mentioned in my above post, 'Unexpected symbol '=''.
Answer by IfItsPeeLetItBe · Jun 11, 2014 at 08:53 PM
You can't make an assignment like this inside of a class.
using System;
public class ItemDatabase {
ItemObject item0;
item0.Name = "name";
}
You need to place it inside of a Method like this.
using System;
public class ItemDatabase {
ItemObject item0;
void MethodForAssigningStuff()
{
item0.Name = "name";
}
}
Then you will see that the issue goes away. I've been coding for about 9 months but stuff like this slips my mind all the time... Let my know if this helps... Or if something else goes wrong :|
Also struct does not allow for a constructor with no parameters.