- Home /
How would one use a pointer variable to reference an array element?
Suppose I have an array of a custom class that takes a string and two ints:
public Statistic[] stats = new Statistic[]
{new Statistic("Health", 100, 100),
new Statistic("Stamina", 50, 50),
new Statistic("Attack", 5, 5),
new Statistic("Defend", 4, 4),
new Statistic("Speed", 3, 3),
new Statistic("Luck", 3, 3)};
I would love it if I could reference these elements through some kind of shorthand. For instance,
//referencing
stats.hp
//would point to the same data as
stats[0]
Is this a thing? My apologies if this has been asked before, I did some searching but I found it difficult to come up with a concise query. It's really a readability thing that would be really nice to have. Thanks awfully for the help! -Luke
I don't really get your problem. "stats[0].hp" should work if your Statistic class has public/internal variable/property named hp.
This is really a basic C# question. What I mean is, you can get a much better explanation in a regular C# guide.
All class-variables in C# are really just pointers, and any class-variable can not use new, and ins$$anonymous$$d just point to any existing item. A common example is how GameObject player=GameObject.Find("player");
uses player
as a pointer (C# calls them references.)
You can't point to a item in any old array (like an array of ints.) Array is just a red-herring. You can point to any class, whether the class is in an array or not.
Answer by LRG · May 15, 2015 at 04:30 PM
Yes, quite easily:
public Statistic[] stats = new Statistic[]
{new Statistic("Health", 100, 100),
new Statistic("Stamina", 50, 50),
new Statistic("Attack", 5, 5),
new Statistic("Defend", 4, 4),
new Statistic("Speed", 3, 3),
new Statistic("Luck", 3, 3)};
var stat = stats[2];
stat.hp = 200;
Well, that doesn't quite solve the problem, but it helped me figure it out so I'm marking it right.
In the first place, I neglected to tell you that stats is initialized outside of the game loop- like before Start or Update in the class.
so that means that the pointer variable has to be initialized inside start or awake.
stat.hp is a typo, I think you meant stats.hp- and that wouldn't work as hp is not a field of the class Statistic. That's what you got wrong.
var stat is javascript (not your fault, I didn't specify.), so that would be
Statistic hp = stats[0];
and then I can change the value of hp and it will also change stats[0];
Glad it helped, to an extent!
stat.hp was not exactly a typo, but indeed, if hp is not an attribute of the Statistics class, it won't work. stats.hp would also fail but for a different reason; it wouldn't be trying to access the hp field within the Statistic class, but the hp field within a Statistic[] array. This is not possible because an array wouldn't have such a field (you could, however, access, for instance, stats.length).
var stat = stats[2] is both C# and JavaScript code, though the var keyword is probably indeed more popular in JavaScript. They have different meanings, however. In JavaScript var is used to declare a local variable. In C# var is used as a shortcut for the type, which is statically inferred. Basically, it's just syntactic sugar to type less. You can use it within a method scope but not within a class scope, so depending on your case you may not be able to use it anyway.
Your answer
Follow this Question
Related Questions
array is pointer and float variable is not - why not? C# 1 Answer
Pointer or Copied Value? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Store reference to array as variable 2 Answers