Running the Main Loop inside New()

BlitzMax Forums/BlitzMax Programming/Running the Main Loop inside New()

beanage(Posted 2010) [#1]
Hey,

I'd like to know whether it is a bad choice architecture-wise to run the main loop inside the "constructor" of a type.

On my part, I just found it cool to do this:
Global game:MyGame = New MyGame

.. whereas the rest happens in MyGame::New

Thanks for your replies!

Last edited 2010


Leon Drake(Posted 2010) [#2]
I see nothing wrong with that if that is how you want to do it.


ProfJake(Posted 2010) [#3]
But when the rest happens in New, why do you assign it to a Global?

My opinion: It's not that clean to my mind, since when I read this code I would not expect the game to be run already.


Oddball(Posted 2010) [#4]
I would create a separate method or function within the type for running your singleton. I do the following in one of my projects.
New app.Run
Where app is the singleton type and Run is a method of that type with the main app loop inside. In fact that line above is the only code in the program that isn't a type. Doing it this way means that your object gets fully created before then running the main method/function.


beanage(Posted 2010) [#5]
In fact that line above is the only code in the program that isn't a type.


Yea, that was exactly my aim too. I tried moving the global into type scope, but there is an issue with using reflection inside the constructor of an object assigned to a global declared in type scope, that made me doing otherwise.

New app.Run

Awesome idea, Thanks!

Last edited 2010


Czar Flavius(Posted 2010) [#6]
Why do you need to use reflection on your game type???

I find this works well enough.

Local game:TGame = New TGame
game.Load()
game.Run()
End


I personally would not put main game code inside the New method, as the method is "meant" for start-up code to initialize an object. From the computer's point of view, I don't think there is any problem (though you might want to make your type Final).

Last edited 2010


Gabriel(Posted 2010) [#7]
I suspect this falls into the category of what C++ calls "undefined behaviour". Roughly translated into Blitz-speak, it means "I can't think of any bad consequences right now, but Mark has every right to change or add something which will cause your application to explode hideously, and you won't have any comeback because you did something you weren't technically supposed to do."

In practice, I believe Mark does everything a constructor needs to do internally before he calls your New method, and it's highly unlikely that he would change that behaviour because it's in his interest to make this a user-friendly language which lets you do things you shouldn't do. Still, I would (and did) use the same technique that Oddball demonstrates.


beanage(Posted 2010) [#8]
Why do you need to use reflection on your game type???

Calm down man.. alright: Just one example: In my game type, I mark certain methods as callable from the console by appending metadata to them. I then reflect on the game type and iterate through the methods, to find and register the "public" ones. Just one example.

But back on topic, I am Oddbals' solutions' biggest fan, I must admit :}


Czar Flavius(Posted 2010) [#9]
That's one way to make a menu I guess!

Have you considered Lua?