Nez: free, open source 2D framework

I would like to use a Xbox One Controller to handle input. What is the proper way to do this in nez? This pilfered code from the YouTube Tiled demonstration moves the ProtoTypeSprite happily.

if (Input.isKeyDown(Keys.Left)) _velocity.X = -moveSpeed;

So I authored this beauty after reading GamePadData.cs

if (GamePadData.isButtonPressed(Buttons.DPadLeft)) _velocity.X = -moveSpeed;

I am not an experienced programmer by any means but the logic seemed comparable. The impudent IDE demanded an object reference for non-static field, method or property.

After much research and failed solutions I returned, bestowing this:

GamePadData myGamePadData; 

and this:

if (myGamePadData.isButtonPressed(Buttons.DPadLeft)) _velocity.X = -moveSpeed;

That got rid of the squigglies but produced an unhandled exception, “object reference not set to an instance of an object” I know that these are simple coding errors due to my lack of understanding of C# and OOP. I feel like it’s the simplest, most basic oversight but I don’t see it. I know I’m misusing the GamePadData class and would like to see a proper implementation to learn from my mistakes if I may. I don’t ask for help carelessly, and appreciate any that’s offered.

There’s an example right in the Platformer Sample. It’s just what I was after. Great framework, very neat and concise. Almost entirely dummy proof too. :blush:

1 Like

Hi. I am trying to run Nez with DirectX. It seems to work but I got a problem on my Effect. For instance, I try to use the SepiaEffect on a single image sprite. I builded the SepiaEffect.fx with 2MGFX with the DirectX_11 Profile.

Following what I see in the samples doesn’t seem to make it. My guess was :

            var hero = myScene.createEntity("hero");
            hero.transform.position = new Vector2(200, 200);
            hero.addComponent(new Sprite(myScene.content.Load<Texture2D>("Characters/001-Fighter01")));
            hero.getComponent<Sprite>().material = Material.defaultMaterial;
            hero.getComponent<Sprite>().material.effect = content.loadNezEffect<SepiaEffect>();

But my sprite is not rendered at all. If I add a background sprite under my sprite hero, I get a sepia color all over the screen.

Do you happen to have a simple effect example on a single renderable ?

Nez only supports OpenGL. Nothing at all was ever tested for DirectX so you are on your own if you want to go that route.

Hi there. I’m struggling a bit with the UI, first problem is that text rendering isn’t perfect, this simple code:

var canvas = createEntity("ui").addComponent(new UICanvas());

var skin = Skin.createDefaultSkin();

var table = canvas.stage.addElement(new Window("window", skin)).setMovable(true).setResizable(true);
table.pad(10).setPosition(10, 10);
            
var inpt = new TextField("input", skin);
table.add(inpt).pad(10).setRow();

var button = new TextButton("click", skin);
table.add(button).size(100,20).setRow();
            
table.pack();

produces this:

Everything works, but besides the ugly rendering, a resizable and movable window has an extremely small drag area on the titlebar to move, like 1px or so, a bit up and you resize it.

Also, when you just want to pass a simple image in an Nez.UI.IDrawable typed parameter, is SubtextureDrawable the appropriate object to use?

Is there a multi-line text input, or maybe a multi-line mode for the normal text input?

Thanks in advance.

It seems to be a bug or incompatibility with FNA (17.05). Somewhere a value is off by one, I’ve printed the usual suspects from FNA but the values are consistent, at least with the window and framebuffer dimensions. Anyway, I managed to workaround that with a quick hack:

On ECS/Camera.cs, add the following lines before the inverse transform matrix calculation (around L330):

float offX = Core.graphicsDevice.Viewport.Width / (Core.graphicsDevice.Viewport.Width-1f);
float offY = Core.graphicsDevice.Viewport.Height / (Core.graphicsDevice.Viewport.Height-1f);
_transformMatrix *= Matrix2D.createScale(offX, offY);

On ECS/Scene.cs modify the following lines to read as follows (around L557):

var renderTargetWidth = screenSize.X-1;
var renderTargetHeight = screenSize.Y-1;

That’s it!

On a related note, I’m really enjoying Nez, really well-made gamedev library! The other questions are still valid though :slight_smile:

EDIT: Also, this question may sound ridiculous… but how do I enable vsync? =/

I want to make it so that an Entity’s Sprite flashes white when it gets hit. I think I need to create a shader and use that, but I am not sure how to apply a shader to a Nez Sprite. Is there any tutorial or reference describing how to do this? Am I supposed to be using a PostProcessor somehow? Thanks in advanced!

Nez includes a SpriteBlinkEffect that you can use to fully color a Sprite. All Effects are applied via a Material and all RenderableComponents can have their Material set via the setMaterial method.

1 Like

Hi!
Is Nez ok with 3.7 build ?

Cant say I have any idea what 3.7 is bringing. All you have to do is change the Reference to your 3.7 build and the compiler will let you know straight away.

Matrix.Rotation and Matrix.Scale got removed, so if Nez uses that, you should switch to Matrix.Decompose.
Other than that there are no breaking changes so far.

That should be all good. Nez uses its own 2D matrix class.

Thanks for the heads up on SpriteBlinkEffect. I have been trying to use it, but I have been running into issues. For reference, I essentially have the Nez (Portable) and Nez.PipelineImporter projects imported as shown from the youtube tutorial. Anyway, I tried using the SpriteBlinkEffect, but it caused a runtime error:

DirectoryNotFoundException: Could not find a part of the path ‘C:\MyProject\MyProject\bin\DesktopGL\AnyCPU\Debug\Content\nez\effects\SpriteBlinkEffect.mgfxo’.

I tried pasting the mgfxo file into the correct place (I got it from the Nez repo at Nez/DefaultContent), but then I got the following error:

‘This MGFX effect is for an older release of MonoGame and needs to be rebuilt.’

So instead I tried to build the files myself, but here I am not exactly sure what to do. I copied the .fx files into my Content folder (from the Nez repo in DefaultContentSource). I imported the nez/effects folder into my MonoGame Pipeline tool, and built it. When building the .fx files, I used the “Effect Importer - MonoGame” Importer and “Effect - Monogame” Processor. It built successfully, but I looked in the bin\DesktopGL\AnyCPU\Debug\Content\nez\effects\ and it has .xnb files instead of .mtfxo files. When I try to run it, I get a file not found exception again (because it is looking for SpriteBlinkEffect.mtfxo instead of SpriteBlinkEffect.xnb)

What is the correct process to create the SpriteBlinkEffect.mtfxo file in the Monogame Pipeline tool? What is the difference between .fx, .fxb, .xnb, .mtfxo files. Which should I use, and which importer and processors in the MonoGame Pipeline tool should I use to create the correct files? Thanks in advanced.

If you are using MG 3.6 you will need to rebuild all the effects. When you rebuild them they must be put in the exact location as in the repo with the same file name.

Which files should I be building? When I try to build the .fx files, the result is an .xnb. When I try to build mtfxo files, it does not work saying:“Bad token CppNet.Token”. Are the mtfxo files the result of building other files, or the files I need to build?

EDIT: I have learned more about the issue, it seems that building an .fx file with the Monogame Pipeline tool results ina .xnb file, which is more information than a .mtfxo file. I am still unsure if it is possible to use the Monogame Pipeline tool to get a .mtfxo , but I am going to try using 2MGFX for now. http://www.monogame.net/documentation/2/?page=2MGFX

2MGFX will do the trick. All the compiled effects included with Nez are compiled outside the Pipeline Tool using 2MGFX directly.

Does anyone know how to use Nez to create procedurally generated tile maps? Is this possible by leveraging the existing Nez.Tiled APIs?

There is nothing stopping you from using the Tiled API to make procedural maps. There is an example in the Samples repo that does the opposite: destroys tiles procedurally. You can achieve procedural generation by doing it just like that sample except fill in tile data rather than remove it.

Hi there, is there any batching functionality for the ‘mesh’ components?

Mesh doesn’t do any dynamic batching out of the box. You can, however, just use a single Mesh for all of your needs by manually assigning the verts/UVs.

1 Like