Adam's Lair Forum

game development and casual madness
It is currently 2020/01/28, 18:16

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: How to create singleton
PostPosted: 2015/01/11, 21:38 
Newbie
Newbie

Joined: 2015/01/11, 21:07
Posts: 4
Role: Hobbyist
Hello!

I am a novice in Duality, and don't really understand how to do a Singleton object. In fact I didn't find out how to get access to one gameObject's public parameter from another GameObject. So give me a few code examples please.

_________________
Sorry for my english...


Top
 Profile  
 
PostPosted: 2015/01/11, 22:08 
Veteran Member
Veteran Member
User avatar

Joined: 2015/01/07, 09:58
Posts: 102
Location: Sweden
Role: Hobbyist
Hello,

Do you mean a singleton object in C#? Or like a globally accessible entity?


Top
 Profile  
 
PostPosted: 2015/01/11, 22:29 
Newbie
Newbie

Joined: 2015/01/11, 21:07
Posts: 4
Role: Hobbyist
skoggy wrote:
Hello,

Do you mean a singleton object in C#? Or like a globally accessible entity?

The latter I think. The problem is to create the global object that I can access from GameObject's code in any time.

_________________
Sorry for my english...


Top
 Profile  
 
PostPosted: 2015/01/12, 00:52 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2073
Location: Germany
Role: Professional
Hi Urias,

a regular C# singleton looks like this:

Code:
public class YourSingleton
{
   private static YourSingleton instance = new YourSingleton();
   public static YourSingleton Instance
   {
      get { return instance; }
   }
   
   // The rest of your code
}


Which can be accessed using YourSingleton.Instance. The singleton will be available across all Scenes, but note that it is not a Component, or related to Duality in any way. It is just a globally available class instance. This means, you won't be able to select the object in the editor, nor will its state be serialized with a Scene.

There are ways to make "Component Singletons", but there is no "one definite" way to do this, but rather a broad concept that can be applied differently, depending on the actual use case and requirements. In some cases, what you really want is not a Singleton, but rather an automated way of locating an existing object of a certain Type in the current Scene.

What exactly do you want to do with your Singleton? What's your use case?

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2015/01/12, 12:04 
Newbie
Newbie

Joined: 2015/01/11, 21:07
Posts: 4
Role: Hobbyist
In fact I've understand that I can store all global variables in the protagonist's class, and have no problems:)

But I still don't understand how gameObjects communicate with each other whithout any mutual context such as some Collision Event e.g..
For example, there are two objects on a scene - Player and Plant e.g. - and in random time one object should get the health variable (that declared in code) of another object. How can he do that?

_________________
Sorry for my english...


Top
 Profile  
 
PostPosted: 2015/01/12, 16:42 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 883
Location: Italy
Role: Hobbyist
Hi Urias,

for your question, there are a number of ways to do it.. I will describe the first two that come to mind, assuming that both Player and Plant are Components:

You can make a direct link between the Player and the Plant (which can then be set in the editor, or by code)
Code:
class Player : Component
{
  public Plant MyPlant { get; set; }
  ...
 
  public void GetPlantHP()
  {
    if (MyPlant != null)
    {
      int plantHP = MyPlant.HitPoints;
    }
  }
}


or you can look in the Scene for the Plant by name
Code:
class Player : Component
{
  public Plant MyPlant { get; set; }
  ...
 
  public void GetPlantHP()
  {
    string plantName = "myPlant"; //or it can come from a parameter, for example
    GameObject plantObj = this.GameObj.ParentScene.FindGameObject(plantName);
   
    if(plantObj != null)
    {
      Plant p = plantObj.GetComponent<Plant>();
      if(p != null)
      {
        int plantHP = p.HitPoints;
      }
    }
  }
}


Then it's just a matter of deciding how and where you should call GetPlantHP (OnUpdate? on a collision? keypress?)

Good luck :)

EDIT: typo

_________________
Come on Duality's Discord channel. We have cookies! :mrgreen:


Last edited by SirePi on 2015/01/12, 17:02, edited 1 time in total.

Top
 Profile  
 
PostPosted: 2015/01/12, 16:48 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2073
Location: Germany
Role: Professional
I was about to post here, but SirePi was faster :D What he said. Plus, you can also do the following:

  1. If you know object B only by Type or name, you can ask the current Scene for one or all of them:
    Code:
    Scene.Current.FindComponent<Plant>();
    Scene.Current.FindComponents<Plant>();
  2. If you access a certain object a lot of times, you can use one of the other options, combined with clever caching to get something that feels a little like a singleton:
    Code:
    private static GameObject importantObject = null;
    public static GameObject ImportantObject
    {
        get
        {
            if (importantObject == null || importantObject.Disposed || importantObject.ParentScene != Scene.Current)
            {
                importantObject = Scene.Current.FindGameObject("VeryImportant");
            }
            return importantObject;
        }
    }

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2015/01/12, 17:46 
Newbie
Newbie

Joined: 2015/01/11, 21:07
Posts: 4
Role: Hobbyist
SirePi, Adam, thanks a lot! Now it's became much more clear for me :)

_________________
Sorry for my english...


Top
 Profile  
 
PostPosted: 2015/01/13, 22:03 
Veteran Member
Veteran Member
User avatar

Joined: 2015/01/07, 09:58
Posts: 102
Location: Sweden
Role: Hobbyist
Adam wrote:
Code:
private static GameObject importantObject = null;
public static GameObject ImportantObject
{
    get
    {
        if (importantObject == null || importantObject.Disposed || importantObject.ParentScene != Scene.Current)
        {
            importantObject = Scene.Current.FindGameObject("VeryImportant");
        }
        return importantObject;
    }
}


Now this was very handy :) Just came across a situation where I needed this as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group