C
C#2y ago
Solace

❔ ✅ abstract-Parent and Child static overrides?

How do I redeclare an objects element which needs be from a static context in a child class? I have an abstract parent class:
public abstract class ResizableGatePrefab : PrefabVariantInfo {
//more things above
private static Color24 color_t = new Color24(0,0,0);
public Block prefabBlock = new Block { RawColor = color_t };
//more things below
}
public abstract class ResizableGatePrefab : PrefabVariantInfo {
//more things above
private static Color24 color_t = new Color24(0,0,0);
public Block prefabBlock = new Block { RawColor = color_t };
//more things below
}
and some child classes (here's an example):
public class ResizableAndGatePrefab : ResizableGatePrefab {
public static Color24 color_t = new Color24(127,127,127);
public Block prefabBlock = new Block { RawColor = color_t };
//more things below
}
public class ResizableAndGatePrefab : ResizableGatePrefab {
public static Color24 color_t = new Color24(127,127,127);
public Block prefabBlock = new Block { RawColor = color_t };
//more things below
}
where in I'm trying to override prefabBlock and want to give it a color RawColor; RawColor requires it's Color24 be static. I have been struggling with this massively.
28 Replies
Solace
SolaceOP2y ago
Note: the childs prefabBlock is not overriding the parents, so the parents is the one getting through to the base methods of the child.
Kouhai
Kouhai2y ago
Is what you're trying to do is ResizableGatePrefab.color_t = Color24(0,0,0) ResizableAndGatePrefab.color_ = new Color24(127,127,127) ? You can't mark static methods and properties as abstract or virtual, your best option would be to hide the parent.s color_t with new new public static Color24 color_t = new Color24(127,127,127);
Solace
SolaceOP2y ago
I've tried Child:
public static Color24 color = new Color24(127,127,127);
new public Block prefabBlock = new Block { RawColor = color };
public static Color24 color = new Color24(127,127,127);
new public Block prefabBlock = new Block { RawColor = color };
and the color is still (0,0,0) for the child objects
Kouhai
Kouhai2y ago
Can you show how you're using it?
Solace
SolaceOP2y ago
What do you mean?
Kouhai
Kouhai2y ago
Like how are you testing that the color is still (0.0.0)
Solace
SolaceOP2y ago
In unity, these two objects have a prefab which starts in their child classes. These are two child classes of ResizableGatePrefab which have colors other than (0,0,0) (or close) defined; however, both of this still use their parents default color (0,0,0) When I don't declare the default object I struggle with declaring it in the child because passing static objects in C# is- more complicated than I expected (or impossible?)
Kouhai
Kouhai2y ago
I struggle to see why you have color as a static. Block has a RawColor property right?
Solace
SolaceOP2y ago
I wish I knew why color needed to be static too. the prefabBlock struct requires that RawColor be a static Color24.. for whatever reason. I don't like it either.
Kouhai
Kouhai2y ago
How does it require it to be static? I'm not that well versed in unity but how would they enforce that?
Solace
SolaceOP2y ago
I can change it and show you the compile error, one sec
2023-03-27T17:32:15Z | ERROR | [Random Debug Collection] Failed to compile mod
2023-03-27T17:32:15Z | ERROR | [Random Debug Collection] Error: (14,59): error CS0236: A field initializer cannot reference the non-static field, method, or property 'ResizableGatePrefab.color_t'
2023-03-27T17:32:15Z | INFO | [Random Debug Collection] Skipped 36 'mscorelib' warnings.
2023-03-27T17:32:15Z | ERROR | [Random Debug Collection] Error: (14,59): error CS0236: A field initializer cannot reference the non-static field, method, or property 'ResizableGatePrefab.color_t'
2023-03-27T17:32:15Z | ERROR | [Random Debug Collection] Failed to compile mod
2023-03-27T17:32:15Z | ERROR | [Random Debug Collection] Error: (14,59): error CS0236: A field initializer cannot reference the non-static field, method, or property 'ResizableGatePrefab.color_t'
2023-03-27T17:32:15Z | INFO | [Random Debug Collection] Skipped 36 'mscorelib' warnings.
2023-03-27T17:32:15Z | ERROR | [Random Debug Collection] Error: (14,59): error CS0236: A field initializer cannot reference the non-static field, method, or property 'ResizableGatePrefab.color_t'
"A field initializer cannot reference the non-static field" :c sad When I make it static, it works.. but every instance takes on the parent one (0,0,0)
Kouhai
Kouhai2y ago
Yes, field initializers can't reference other fields. Properties on the other hand can reference them So
public Color24 color = new Color24(127,127,127);
public Block prefabBlock => new Block { RawColor = color }
public Color24 color = new Color24(127,127,127);
public Block prefabBlock => new Block { RawColor = color }
Works
Solace
SolaceOP2y ago
They're still (0,0,0); I tried it with and without new's for hiding. I'll keep testing. no matter which way I change the child to have new hidings and => expressions, it doesn't change the color.
Kouhai
Kouhai2y ago
Okay, how are you using RawColor in Block? If possible, please show the code for Block as well ThumbsUpSmile
Solace
SolaceOP2y ago
Child:
public class ResizableAndGatePrefab : ResizableGatePrefab {
new public static Color24 color_t => new Color24(127,127,127);
new public Block prefabBlock => new Block { RawColor = color_t };
//more things after (not changing prefabBlock
}
public class ResizableAndGatePrefab : ResizableGatePrefab {
new public static Color24 color_t => new Color24(127,127,127);
new public Block prefabBlock => new Block { RawColor = color_t };
//more things after (not changing prefabBlock
}
Block:
public class Block : IRenderData {
public static Block Standard {
get { return new Block(); }
}

public Vector3 Position { get; set; } = Vector3.zero;

[SaveThis(SaveAs = "rotation")]
public Vector3 Rotation { get; set; } = Vector3.zero;

[SaveThis(SaveAs = "scale")]
public Vector3 Scale { get; set; } = Vector3.one;
public GpuColor Color {
get { return this.RawColor.ToGpuColor(); }
}

[SaveThis(SaveAs = "color")]
public Color24 RawColor { get; set; } = Colors.DefaultBlockColor;

[SaveThis(SaveAs = "material")]
public MaterialType Material { get; set; }

[SaveThis(SaveAs = "colliderData")]
public ColliderData ColliderData { get; set; } = ColliderData.Standard;

[DontSaveThis]
public Mesh Mesh {
get {
if (this._Mesh == null) {
this._Mesh = Meshes.FromString(this.MeshName);
}
return this._Mesh;
} set {
this._Mesh = value;
}
}

[SaveThis(SaveAs = "mesh")]
public string MeshName = "BetterCube";

private Mesh _Mesh;
}
public class Block : IRenderData {
public static Block Standard {
get { return new Block(); }
}

public Vector3 Position { get; set; } = Vector3.zero;

[SaveThis(SaveAs = "rotation")]
public Vector3 Rotation { get; set; } = Vector3.zero;

[SaveThis(SaveAs = "scale")]
public Vector3 Scale { get; set; } = Vector3.one;
public GpuColor Color {
get { return this.RawColor.ToGpuColor(); }
}

[SaveThis(SaveAs = "color")]
public Color24 RawColor { get; set; } = Colors.DefaultBlockColor;

[SaveThis(SaveAs = "material")]
public MaterialType Material { get; set; }

[SaveThis(SaveAs = "colliderData")]
public ColliderData ColliderData { get; set; } = ColliderData.Standard;

[DontSaveThis]
public Mesh Mesh {
get {
if (this._Mesh == null) {
this._Mesh = Meshes.FromString(this.MeshName);
}
return this._Mesh;
} set {
this._Mesh = value;
}
}

[SaveThis(SaveAs = "mesh")]
public string MeshName = "BetterCube";

private Mesh _Mesh;
}
This was under some random dll in the build Lemme find IRenderData too- That one was easier to find:
public interface IRenderData {
Vector3 Position { get; }
Vector3 Rotation { get; }
Vector3 Scale { get; }
GpuColor Color { get; }
MaterialType Material { get; }
ColliderData ColliderData { get; }
Mesh Mesh { get; }
}
public interface IRenderData {
Vector3 Position { get; }
Vector3 Rotation { get; }
Vector3 Scale { get; }
GpuColor Color { get; }
MaterialType Material { get; }
ColliderData ColliderData { get; }
Mesh Mesh { get; }
}
Note: the only code shown I wrote are in ResizableGatePrefab and ResizableAndGatePrefab
Kouhai
Kouhai2y ago
public class ResizableAndGatePrefab : ResizableGatePrefab {
public Color24 Color_t => new Color24(127,127,127);
public Block prefabBlock => new Block { RawColor = color_t };
}
public class ResizableAndGatePrefab : ResizableGatePrefab {
public Color24 Color_t => new Color24(127,127,127);
public Block prefabBlock => new Block { RawColor = color_t };
}
But, I still can't really tell how the RawColor is getting used, I can't imagine it being checked on every Update() call
Solace
SolaceOP2y ago
Here's Color24 too, just in case: https://paste.mod.gg/odsfsmwgwths/0
BlazeBin - odsfsmwgwths
A tool for sharing your source code with the world!
Kouhai
Kouhai2y ago
Color24 seems okay
Solace
SolaceOP2y ago
ClydeBot won't let me send the rest.
Kouhai
Kouhai2y ago
😅 $paste
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Solace
SolaceOP2y ago
BlazeBin - odsfsmwgwths
A tool for sharing your source code with the world!
Solace
SolaceOP2y ago
I should state, I believe much of this code I'm looking at is owned by some company
Kouhai
Kouhai2y ago
Yeah The question is, how is RawColor accessed? Like if you set RawColor in an update call, does the color change?
Solace
SolaceOP2y ago
Still (0,0,0) :c heck (well I already tried that so I'm not really surprised, just sad) Update() as the unity thing?
Kouhai
Kouhai2y ago
Yeah, set it in Update
Solace
SolaceOP2y ago
I found a solution !close
Accord
Accord2y ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server