class that is initialized becomes null when outside of function

Flaze07 christianseiji.cs at gmail.com
Mon Jul 9 09:18:50 UTC 2018


I am using DSFML
module game;

import core.time;

import std.stdio: writeln;

import dsfml.graphics;

import dsfgui.button;

import apple;
import snake;

class Game
{
private:
     enum State { menu, playing }
     State state;
     Font font;
     Button playBtn;
     Snake snake;
     Apple apple;
     RenderWindow win;
     int size;
protected:
     this(){}
public:
     void init()
     {
         import std.exception : enforce;

         size = 10;

         auto win = new RenderWindow( VideoMode( 600, 600 ), 
"snake" );

         font = new Font;
         enforce( font.loadFromFile( "media/arial.ttf" ), "failed 
to load font arial" );

         playBtn = new Button( "play", font );
         playBtn.position = Vector2f( 300, 300 );

         snake = new Snake( size, 3, Vector2f( win.size.x, 
win.size.y ) );
         apple = new Apple( size / 2, Vector2f( win.size.x, 
win.size.y ) );
     }
     void run()
     {
         auto clock = new Clock;
         writeln( win is null );
         while( win.isOpen() )
         {
             writeln("lol" );
         }
     }
}

the problem here is, during init(), the writeln will output 
false, which means yes win is initialized, however during run(), 
suddenly win is null becomes true...

and here is my main()
void main( string[] args )
{
     myGame.init();
     myGame.run();
}

P.S I tested this with int* and it don't become null when 
function ends


More information about the Digitalmars-d-learn mailing list