Loup&Snoop
Loup&Snoop
CC#
Created by Loup&Snoop on 1/11/2024 in #help
✅ Refactoring A Class With Too Many Responsibilities
Right now, I am trying to refactor a level editor class, which currently has way too many responsibilities, and I want to fix that. My core issue comes from trying to restrict access to certain methods between my level editor and other scripts. For example, here are methods for drawing a tile in a level (in my LevelEditor class): -private DrawCommand (what to do when user issues a command to draw at the target position, including validation logic. Calls DrawWithLog) -private DrawWithLog (try to draw a tile while logging changes to undo history, calls DrawAtRegion) -public SmartDrawIngame (force draw, but also funnels logic through some singletons that may need to update internals, Calls ForceDrawIngame) -private ForceDrawIngame (outside level edit UI, other scripts can request drawing at a specific spot using other private methods. Calls DrawAtRegion) -private DrawAtRegion (actually erases anything that conflicts in different tilemaps, and draws. Calls SetTile) -private SetTile (draw the tile and associated metadata) There are several other features like this, and I want to split up responsibilities across a couple of classes (so I can also add other features). Any recommendation on how to split this without exposing everything publicly?
9 replies
CC#
Created by Loup&Snoop on 10/9/2023 in #help
✅ Refering to Generic Base Class
struct MyStruct1 : IStruct struct MyStruct2 : IStruct abstract class MyBaseClass<TStruct> where TStruct : IStruct { TStruct storedVar; public void Foo(TStruct x) { storedVar = x; } [many methods to be inherited. None have TStruct arguments. Some use storedVar.] } class MyClass1 : MyBaseClass<MyStruct1> { 1-3 unique methods specifically needing MyStruct1 } class MyClass2 : MyBaseClass<MyStruct2> { 1-3 unique methods specifically needing MyStruct2 } ——- I want to: 1) Avoid boxing with storedVar 2) I want to be able to make collections that can hold a mixture of the derived types, and call the abstract BaseClass methods without downcasting. So far, I have been using an interface to tack on to MyBaseClass, but it seems like a very hacky solution, and forces some property getters to be public (that I would rather be protected). Any suggestions on how to do this? I think an abstract nongeneric base class (for generic base class to derive from) is the answer, but I don’t know how to avoid boxing storedVar as an Istruct if I do that.
5 replies
CC#
Created by Loup&Snoop on 8/15/2023 in #help
✅ Immutable object with mutable clone
I have a class EntityData which is full of fields that need to be read only (everything public get, private set). I want to be able to make a different object that is a mutable clone, which behaves like EntityData in every way except every property is public. During runtime, I will have multiple instances of a class (EntityDataHolder) which will get fed an immutable original EntityData, and then make a mutable clone so that this particular instance of EntityDataHolder can modify it freely. I will later add more fields to EntityData, and I don’t want its definition and its clone’s definition to get messed up/misaligned (ie I want to avoid writing every field 4 times to make one on the original, clone, and clone-copying class all match up). What is a clean way to do this?
21 replies
CC#
Created by Loup&Snoop on 7/26/2023 in #help
✅ Class as a field?
Suppose I have Class1, Class2, and Class3. Class2 is just a container of information. At runtime I want Class1 to look into the contents of Class2, find Class3, and instantiate an instance of Class3. For this, I DO NOT WANT class2 to have an instance of class3, and I do not want class2 to have any code/knowledge of what class3 is going to do. In theory, I want Class2 to have a field which gives a type that implements a specified interface that allows Class1 to call it. Is there a way to do this?
97 replies
CC#
Created by Loup&Snoop on 7/24/2023 in #help
❔ Offset property for array
Suppose I have an array: public int[,] paddedArray = new int[width + 6, height + 6]; I want to make a property that accesses values offset, to get something like unpaddedArray[x,y]: get gives paddedArray[x+3,y+3], and set will set paddedArray[x+3,y+3]; Would anyone know how to define this?
7 replies
CC#
Created by Loup&Snoop on 7/21/2023 in #help
❔ Best data structure for graph?
I need to represent a directed graph, and I’m going to need to very rapidly do various operations when something happens at one node/vertex. I could represent my graph with Dictionaries, but is there a better way? Q: What data structures are most appropriate to work with graphs?
13 replies
CC#
Created by Loup&Snoop on 7/19/2023 in #help
❔ Is my Inherittance Scheme dumb?
Base class BuildingObjectBase defines a tile that is used in a level editor. It has data to describe rules of rendering layer, tooltip strings, ID for files, sprite, etc. TileAssembly : BuildingObjectBase describes a single object that spans a rectangular array of tiles. TileAssembly needs ALL of BuildingObjectBase’s fields and properties. The main difference is TileAssembly has an underlying dictionary/ints/int[,] field to populate arrays of tile sprites. Almost every property of TileAssembly is an override, where BuildingObjectBase has a virtual property returning default output. (eg TotalCells() {return 1;}). Everything has been going great, but now if I want to make a class that has a separate sprite for a certain trigger (eg hitting switch changes tile sprite), it either needs to be a TileAssembly (if I want any of them to have multiple tiles), or I need to add a field to BuildingObjectBase (which most instances of the class won’t use). Q: Is there a smarter way?
8 replies
CC#
Created by Loup&Snoop on 7/17/2023 in #help
❔ static class
Hey. I'm trying to define a static class. My static class's methods are almost entirely all functions of variables not in my class. However, I do want to initialize a reference to one thing, to an instance of a singleton, so I don't need to call GetInstance() (and all the baggage associated with calling it) every time I run a function from my static class. Q: Is there a way to store a reference to an instance as a variable in a static class at runtime?
14 replies