Adam's Lair Forum

game development and casual madness
It is currently 2018/10/16, 17:19

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: 2018/01/04, 01:04 
Newbie
Newbie

Joined: 2018/01/03, 02:53
Posts: 7
Role: Gamer
Hello everyone, I've only recently downloaded Duality and I'm really struggling with it.
I've read the introduction, tutorials, basic topics, etc. I also completed the space shooter example.

Now my game takes place in a building (much like sim tower, if you had the pleasure).
So I want to dinamically create floors and then add rooms to the floors. Needless to say, these rooms are not created at the beginning of the game, but are created by the player as the game progresses.
So I create a GameObject "room" and I add a Transform Component to set it's position on the screen. But I'm stuck adding the SpriteRenderer to it. And how do I set the pixmap which is already loaded in the Project View into the renderer?
Can anyone point me in the right direction? Is even a sprite renderer what I need? I'm only going for it because that is what's created when I create a game object from a pixmap in the editor...

I have used Irrlicht in the past, but I wanted to switch to something in C#, game-oriented and 2D specific. But this "your code does not use the engine, the engine uses your code" has me really puzzled.

Also, are there some sample projects where I figure out this basic stuff? Where?


Top
 Profile  
 
PostPosted: 2018/01/04, 11:53 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 880
Location: Italy
Role: Hobbyist
kiel814 wrote:
Hello everyone, I've only recently downloaded Duality and I'm really struggling with it.

Hello and welcome! :mrgreen:

kiel814 wrote:
I've read the introduction, tutorials, basic topics, etc. I also completed the space shooter example.

:+1:

kiel814 wrote:
Now my game takes place in a building (much like sim tower, if you had the pleasure).

Yes, yes I had :D
Now that the pleasantries are out of the way, let's get on the juicy bits

kiel814 wrote:
So I want to dinamically create floors and then add rooms to the floors. Needless to say, these rooms are not created at the beginning of the game, but are created by the player as the game progresses.
So I create a GameObject "room" and I add a Transform Component to set it's position on the screen. But I'm stuck adding the SpriteRenderer to it. And how do I set the pixmap which is already loaded in the Project View into the renderer?
Can anyone point me in the right direction? Is even a sprite renderer what I need? I'm only going for it because that is what's created when I create a game object from a pixmap in the editor...

You are right on track:
you need to create a GameObject, fill it with all the required Components (Transform and SpriteRenderer for example) and then, remember to add it to the Scene. Something like this, assuming that this code is part of a Component running inside your game Scene (this is my style.. don't take it as immutable)

Code:
{
    ...
    TowerRoom room = ...;
    this.GameObj.ParentScene.AddObject(CreateNewRoom(room))
}

private GameObject CreateNewRoom(TowerRoom room)
{
    GameObject go = new GameObject(room.Name);
    go.AddComponent<Transform>();
    go.AddComponent<SpriteRenderer>();
    go.GetComponent<Transform>().Pos = new Vector3(room.Position, 0);
    // actual size depends on your sprite of course
    go.GetComponent<SpriteRenderer>().Rect = Rect.Align(Alignment.Center, 0, 0, 256, 256);
    // also here you will want to load a different material depending on the room type
    go.GetComponent<SpriteRenderer>().SharedMaterial = ContentProvider.RequestContent<Material>(@"Data\Path\To\My\Material.Material.res").Res;

    return go;
}

Otherwise, you could explore the wonderful world of Prefabs (see below about where you can find some info in general)

kiel814 wrote:
I have used Irrlicht in the past, but I wanted to switch to something in C#, game-oriented and 2D specific. But this "your code does not use the engine, the engine uses your code" has me really puzzled.

That's an inspirational quote that basically means that, unlike other game engines where you provide the program's entry point and reference the engine, in this case it's actually the engine that references your classes to play the game :D

kiel814 wrote:
Also, are there some sample projects where I figure out this basic stuff? Where?

You can check the tutorial section on the forum (though they might be old, they should still work or require a minimum amount of tinkering to make them work with the newer versions of Duality), the samples in the Package Manager of Duality itself, and feel free to join the chat on Discord if you need pseudo-real-time help :mrgreen:

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


Top
 Profile  
 
PostPosted: 2018/01/04, 16:55 
Newbie
Newbie

Joined: 2018/01/03, 02:53
Posts: 7
Role: Gamer
Thanks for your quick and detailed response!
It certainly worked as expected.

Now I do have a few additional questions...

So in order to test adding a few rooms, I created an empty "Map" GameObject, then I created a "Map" Component, which also inherits from ICmpInitializable.
On the OnInit method, I created some rooms as you suggested.
So when I add the Component to the GameObject, all the rooms get added in editing time.
I realize I have not filtered the InitContext, so I filter for "Loaded", manually remove all the components from Map in the editor, and re-add the Map component.
So far so good, the rooms are not yet added but they are generated when I run the game.
The problem is, they are still there when I close the game.
First questions: Why does the editor remember what happens in runtime? What if I accidentally delete something from the scene in runtime? I don't want to lose it in the editor. Specially if I have a large, populated scene, I may not even notice I am missing a GameObject.
Ignoring that, I tried to dispose of the dynamically generated content on the OnShutdown method:
Code:
for(int i = GameObj.Children.Count() - 1; i >= 0; i++)
{
   GameObj.ChildAtIndex(i).Dispose();
}

This is causing plenty of errors when I exit the game.
So what is the proper way to dispose of my dynamic content?
Note: Also tried DisposeLater, similar results.


Top
 Profile  
 
PostPosted: 2018/01/04, 17:32 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 880
Location: Italy
Role: Hobbyist
kiel814 wrote:
First questions: Why does the editor remember what happens in runtime? What if I accidentally delete something from the scene in runtime? I don't want to lose it in the editor. Specially if I have a large, populated scene, I may not even notice I am missing a GameObject.

In theory nothing should happen to your Scene unless you actually SAVE it while it's still running, in which case the current state of the Scene will be saved.
If everything goes properly, nothing should happen and you would have no need to clean up the generated GameObjects.

kiel814 wrote:
So what is the proper way to dispose of my dynamic content?

As you correctly guessed, there is something to do with the InitContext; usually you would want to use the following pair
Code:
void ICmpInitializable.OnInit(Component.InitContext context)
{
    if(context == InitContext.Activate)
    {
         //create your stuff here, wire up events, ...
    }
}

void ICmpInitializable.OnShutdown(Component.ShutdownContext context)
{
    if(context == ShutdownContext.Deactivate)
    {
        //cleanup events, anything you don't want to keep, i.e. while switching scenes
    }
}


The errors you were facing were probably also due to the fact that you were deleting GameObjects while the Scene was saving (that's a specific ShutdownContext as well), effectively modifying the list while it was being iterated on.

Try with this for now, and if you still have issues let me know :mrgreen:

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


Top
 Profile  
 
PostPosted: 2018/01/04, 20:41 
Newbie
Newbie

Joined: 2018/01/03, 02:53
Posts: 7
Role: Gamer
I used Loaded context instead of Activate because with Activate, everything gets added the moment I add the Component to the GameObject in the editor.

Is this how it's supposed to be?
I mean, I right click on the empty game object -> New -> [ProjectName] -> Map.
And all the stuff that is added during OnInit(Activate) is stuffed into the GameObject together with the Map Component.


Top
 Profile  
 
PostPosted: 2018/01/04, 21:08 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 880
Location: Italy
Role: Hobbyist
Well, you should check the lifecycle of the GameObject (or better, of your Component):

https://github.com/AdamsLair/duality/wi ... itcontexts

As you can see, Loaded is called only once, whenever the object is actually loaded from disk (so that if you rely on it to create your subobjects in the editor, and then save the Scene, when you actually start the game from the Launcher it will create a new copy of them, as your Component is being Loaded again); with Activate/Deactivate you can actually control everytime your Component is de/activated (duh.. ^^), avoiding the issue described above; you can still see the effects on the Scene by running the sandbox from the editor.

(this stuff is always a bit hazy for me as well.. hope I didn't throw you off course with this)

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


Top
 Profile  
 
PostPosted: 2018/01/04, 21:49 
Newbie
Newbie

Joined: 2018/01/03, 02:53
Posts: 7
Role: Gamer
Yeah... I think I'll just try to avoid creating GameObjects and Components in the OnInit method and try to find a more appropiate way to do this.

Thanks for your help!


Top
 Profile  
 
PostPosted: 2018/01/04, 22:11 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2063
Location: Germany
Role: Professional
Hey there!

Just skimmed the conversation, but the missing puzzle piece might be that the init and shutdown methods are called regardless of whether the application is currently in game mode (sandbox or launcher) or editor mode (scene loaded or reloaded after sandbox stop). It's safe to use Activate for creating objects dynamically if you clean them up in Deactivate, but for use cases that alter the scene, you usually want to check whether DualityApp.ExecContext is Game.

If you already discussed that and I missed it, please ignore ^^

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2018/01/05, 14:29 
Newbie
Newbie

Joined: 2018/01/03, 02:53
Posts: 7
Role: Gamer
Awesome! That was the missing puzzle piece indeed.
Thanks!


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 8 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