- Home /
Trying to change a subclasses' variable
Am I accessing the a classes' variables the wrong way? I'm trying to flip the values of a RECT and rRECT inside _item.cs's ITEM subclass from the "rotate()" function inside the guiBOX. Here's all my code on pastbine http://pastebin.com/99Lk71wf <- _item.cs http://pastebin.com/ABkeZJUP <- guiBOX.cs
and this is the specific code that i'm concerned about.
subclass of _item
public class ITEM
{
public Texture IMAGE;
public Rect RECT;
public Rect rRECT;
public Dictionary<string, string> itemStats;
public int width;
public int height;
public List<Vector2> brecs;
public ITEM(Texture I, Rect S, Dictionary<string, string> istats, int w, int h )
{
brecs = new List<Vector2>();
height = h; width = w;
itemStats = istats; // list of the "key" and the "value" of each item, every item has a NAME and TYPE, based on TYPE it has
//certain attributes so cost, description, etc.
IMAGE = I;
RECT = S;
rRECT = new Rect(S.x, S.y, S.width, S.height);
}
public void rotate(Rect R, Rect Z)
{
RECT = new Rect (R);
rRECT = new Rect (Z);
}
}
rotate func from guiBOX
public void rotate()
{
Rect R = new Rect (backpack [current].RECT);
Rect Z = new Rect (backpack [current].rRECT);
R = new Rect (R.x, R.y, R.width, R.height);
Z = new Rect (Z.x, Z.y, Z.width, Z.height);
backpack [current].rotate (R, Z);
}
Answer by RudyTheDev · Feb 24, 2014 at 11:29 AM
For one, your code doesn't actually "rotate" R
and Z
. You assign R
from RECT
, then remake R
from R
, then tell item to set RECT
as R
. So nothing changes. At the least, you should flip them around if that's what you are doing -- backpack [current].rotate (Z, R);
.
As for the entire function, you can just do:
public void rotate()
{
backpack[current].rotate(backpack[current].rRECT, backpack[current].RECT);
}
Rect
is a struct
type, so it will copy its values not the reference, you don't need to worry about messing up references/instances. In other words, you don't need to new Rect()
unless you are changing something.
But as far as I understand, you just want to swap/flip RECT
with rRECT
. You can just do this in ITEM
:
public void rotate() // or rename it SwapRects() or something more suitable
{
Rect temp = RECT;
RECT = rRECT;
rRECT = temp;
}
then your guiBOX
code is
public void rotate()
{
backpack[current].rotate();
}
It's. Just the. First step of rotate, I plan to change the texture next to a pre rotated one, but I need to swap the width with the height, which is not working for some reason.
I see you working with RECT
and rRECT
and your question says you are trying to flip their values. But you mention width/height, so which variables exactly are your width/height besides public int width, height
? Can be you a little more specific about which part is supposed to do what you want but is not working?
Rect = current frame's Rect for Textures in draw gui, rRect = last previously "snapped" Rect frame where you set it in the gui. All I'm trying to do is do is swap a rect's width with its height. and it seems to simply not work -_-
Can't you just do RECT = new Rect(RECT.x, RECT.y, RECT.height, RECT.width);
?
Oh.. wow thanks for the help but it turns out I was just retarded lol. Basically I was using the "current" variable when it was set to -1... weird that it wasn't throwing errors.
Your answer
Follow this Question
Related Questions
c# - void method from external class. 2 Answers
Instantiating objects from a class? (C#) 1 Answer
Distribute terrain in zones 3 Answers
Need idea for separate a surface of an 3D object 1 Answer
How do I use EditorGUIUtility.ShowObjectPicker()? C# 3 Answers