I am a fairly new programmer trying to make a pretty simple game by using MonoGame in C#. My problem is that I want to access an enum value (not sure if that's the right term) in this other class, but I don't know how. My guesses are that you could do something like: return Game1.State.EnterHighScore; or by making an object reference, but it has not worked for me, probably because I'm doing it incorrectly.
I would appreciate help!
I'm sorry I don't know how to format the code properly, but I tried to make it as clear as possible:
//File name: GameElements.cs
//...
//...
//...
//...
if (e.CheckCollision(player))
{
player.IsAlive = false;
return /*EnterHighScore*/; // I want to return the enum value EnterHighscore,
//..which is in the class Game1
}
//...
//...
//...
//...
//File name: Game1.cs
//...
public class Game1 : Game
{
enum State { PrintHighScore, EnterHighScore }; // I want to access EnterHighScore.
//...
}
//...
Keep your enum outside of any class. Then you can directly return from any method in any class in the same namespace.
eg:
public enum GameState
{
EnterHighScore,
EnterSomeOtherScore,
EnterLooserScore
};
public class SomeClass
{
public GameState CheckGame()
{
return GameState.EnterHighScore;
}
}
If you keep the enum inside any class, then the scope of enum will be only to that class.
Related
I will give you an example for understanding of my question.
transform.translate() (transform is a property and translate is a method)
But how transform can access to translate.
Example :
class ExampleClass
{
public int exampleprop
{ get; }
public void examplemethod()
{
}
}
I want to make just like that : exampleprop.examplemethod()
And is there any way to make this.
(thats why i ask this questions is there are kind of code line in unity called transfom.translate. And i can't understand it.)
In your example, exampleprop.examplemethod() is not valid because exampleprop is an int and ints do not have a method called exampleprop.
The way you would access examplemethod() is to first create a new object of type ExampleClass then access examplemethod() as normal. Here's an example:
class ExampleClass {
public void ExampleMethod() {
// code for example method goes here.
}
}
Then somewhere you need to "new up" an ExampleClass and call the ExampleMethod method.
ExampleClass foobar = new ExampleClass();
foobar.ExampleMethod();
I don't really know how to formulate my issue it's a bit complicated for me, i'll try my best to explain.
I'm making a space game, i have a base class which represent places, and i want to have different type of places like planets, space stations, asteroïds, trading ships etc. The player can click on those objects and get informations.
So my classes looks like something like this:
public class Place {
public int placeId;
public string placeName;
public string placeDescription;
/* Place constructor */
}
public class Planet : Place {
/* Specific proprieties of planet */
public PlanetType planetType;
public int planetSize;
...
// Planet constructor
public Planet(int placeId, string placeName, string placeDescription, PlanetType planetType, int planetSize) : base(placeId, placeName, placeDescription) {
this.planetType = planetType;
this.planetSize = planetSize;
...
}
}
And i have a delegate which accept a function like selectPlace with Place in parameters because i don't want to make a delegate for each type of Place i have.
In another script which is supposed to show the information of any kind of Place, i recieves the Place object that the player clicked on. I think i found a solution, however is this correct to do something like this ?
private void updateSelectedPlaceUI(object sender, EventsController.PlaceEventArgs placeArgs){
// This is just a test, i should check which type of subclass it is before
Planet planetTest = placeArgs.Place as Planet; // So now i can use planetTest.planetType
}
And placing this in a switch case so i can handle any type. I just want to be able to get the proprieties from any derived class of Place in order to display them in UI. I would like to know a better way to achieve this.
But i'm wondering if my design is ok and necessary, it has been a while since i haven't used inheritance / polymorphism, and i feel like i'm doing it the wrong way.
I would propably make the UI part of showing the properties a specific place generic to accept something like a PropertyItem, you can decide the properties yourself.
public class PropertyItem
{
public string Text { get; set; }
public object Value { get; set; }
}
And then in your select method you would just call the abstract method of your base class (make your base class abstract as well)
public abstract class Place
{
...
public abstract IEnumerable<PropertyItem> GetProperties();
}
And now you can override this in your Planet
public class Planet : Place
{
...
public override IEnumerable<PropertyItem> GetProperties()
{
yield return new PropertyItem { Text = "Size", Value = this.planetSize };
}
}
And eventually you would use the GetProperties() method to get the properties of your place and show them in a tabular or what ever format your UI knows how to handle the PropertyItem type.
private void updateSelectedPlaceUI(object sender, EventsController.PlaceEventArgs placeArgs)
{
MyUserInterfaceWidget.DisplayProperties(placeArgs.Place.GetProperties());
}
I am trying to make use of the MVC Pattern in Unity. I am a programming beginner.
Traps and moving Platforms use the same code so i created a base for them. I divide the code into "Data"-class and "Method"-class.
Both objects move to Point A, then to Point B, back to Point A and so on..
Point A and Point B got a trigger, to change the Movementdirection of the Platform/Trap.
The base class holds the data. The subclass gets the data and fills the base data. In the base class i have the object:
public virtual GameObject MovingObject { get { return null; } }
The subclass overrides the property returning null to make it return the right object. I try it this way:
[SerializeField]
private GameObject movingObject;
public override GameObject MovingObject { get { return movingObject; } }
The private variable is set in the Editor and sets the value to the property. This property gives the information to the base class. The problem is that i get null references and I do not know how to fix that. The base class does not return an object. The information get lost on their way to the base...
Is my logic wrong?
If you need to see the whole structure of these six classes you can look it up on
https://github.com/Garzec/MidnightFeast/tree/master/Assets/Scripts/MovingObjects
Sorry, i did not want to post all lines of code and unrelevant stuff :)
I looked at your code. Assuming you will never need an instance of just a "MovingObjectsController" this looks like you need an abstract class as your base class. An abstract class cannot be instantiated, but it can require a child class (subclass) to implement abstract members, removing the need to return null in the parent. For example, you would define your controller as :
public abstract class MovingObjectsController
{
protected abstract MovingObjectsData Data { get; }
}
public class PlatformController : MovingObjectsController
{
private MovingObjectsData data;
public PlatformController()
{
this.data = new MovingObjectsData(); //This being whatever data is specific to this object
}
protected override MovingObjectsData Data {
get
{
return data;
}
}
}
This way the child is required to implement whatever the parent needs, but the parent isn't required to provide a default implementation that doesn't make sense.
I want to simplify my references to an enum:
class Order
{
enum OrderStatus{open,cancelled, onHold}
}
with something equivalent to this:
class SomeOtherClass
{
// assign enum to a simpler name
enum Status = Order.OrderStatus
public void foo(){
// so the reference is simpler, rather than Order.OrderStatus.open
Console.Write(Status.open);
}
}
Any ideas on how I can do this?
You could place this statement at the top of your source code file.
using Status = Order.OrderStatus;
Beware if you have declared your Order class in a namespace called for instance ConsoleApplication, you have to prep-end this before Order.
using Status = ConsoleApplication.Order.OrderStatus;
You can use alias directives to make this a little bit shorter. This needs to be written in the same namespace that has your SomeOtherClass.
using OrderStatus = Order.OrderStatus;
I recommend to use Enum as extra and use/reuse it everywhere your need like this:
public enum OrderStatus
{
open,
cancelled,
onHold
}
class SomeOtherClass
{
private OrderStatus Status;
public void foo(){
// so the reference is simpler, rather than Order.OrderStatus.open
Console.Write(Status.open);
}
}
If the Order is part of the "SomeOtherClass" and is relevant here please forward it in the constructor and make valid for the workflow in the foo() method.
First of all, you must make the enum public, so it is visible outside the class:
class Order
{
public enum OrderStatus { open, cancelled, onHold }
}
Then you can assign it an alias for simpler access
// give an alias
using Status = Order.OrderStatus;
class SomeOtherClass
{
public void foo(){
// reference is now simpler, rather than Order.OrderStatus.open
Console.Write(Status.open);
}
}
I'm attempting to make a namespace for the first time while learning programming. I am hoping there is a way around the problem I've run into that isn't particularly messy, but essentially I have a class object that keeps two dictionaries of two nested class objects. I need to be able to pass the Dictionary of NestedClassA to NestedClassB or to allow NestedClassB to access it in some way... Here is an example of what I'm trying to do:
namespace MyFirstNamespace
{
public class BossClass
{
public Dictionary<int, NestedClassA> DictionaryA = new Dictionary<int, NestedClassA>();
public Dictionary<int, NestedClassB> DictionaryB = new Dictionary<int, NestedClassB>();
public class NestedClassA { ...arbitrary class definition... }
public class NestedClassB
{
public Dictionary<int, NestedClassA> PassedDictionary;
public NestedClassB() { }
public NestedClassB(Dictionary<int, NestedClassA> tempDic))
{
PassedDictionary = tempDic;
}
}
public BossClass() { ... arbitrary constructor ... }
...arbitrary dictionary population methods...
function void CreateAClassBInstance()
{
DictionaryB[n] = new NestedClassB(n, DictionaryA);
}
}
}
My problem seems to be that I can't typecast "NestedClassA" within "NestedClassB" because it doesn't recognize the type. Is it possible to access the "NestedClassA" type within B? Nothing I've tried has worked. Do I have to pass the instance of "BossClass" so I can reference type by "Dictionary<int, MyFirstNamespace.BossClassInstance.NestedClassA>"?
Any help would be appreciated. To be clear, I want a REFERENCE variable passed to NestedClassB of a Dictionary of all NestedClassA members so they can be manipulated by NestedClassB. It can't be a clone. I know this seems like ridiculous implementation, but it seems the most effective, if it's possible, for what I'm trying to do.
EDIT: maybe I shouldn't be nesting them at all, but it would make them much easier to serialize, which is why I really wanted to do it this way.
(EDIT - fixed typo where I forgot to insert "public" before constructors.)
There doesn't seem to be anything particularly wrong with your implementation of NestedClassB other than you need to make your constructors public if you wish to instantiate an instance of NestedClassB. By default in .Net, objects are passed by reference to function parameters, so you will have the same instance of Dictionary<int, NestedClassA> in NestedClassB.
Here is the adjusted class:
public class NestedClassB
{
private readonly Dictionary<int, NestedClassA> _PassedDictionary;
public NestedClassB() { }
public NestedClassB(Dictionary<int, NestedClassA> tempDic) {
_PassedDictionary = tempDic;
}
public Dictionary<int, NestedClassA> PassedDictionary {
get { return _PassedDictionary; }
}
}
Note that I changed PassedDictionary to a property instead of a member variable. Most serializers will ignore member variables and only serialize properties. If you need to deserialize, you'll need to remove the readonly from the private member variable and add a setter.
The function at the bottom of your code snippet doesn't look right. You'll want to make it look like:
private void CreateAClassBInstance()
{
DictionaryB[n] = new NestedClassB(DictionaryA);
}
For anyone trying to do this: passing the instance of the wrapper class is necessary for the nested class to access methods or variables of the wrapper class. This can be done with "this" keyword.