Creating object in the room Creation event?

T

TypicalHog

Guest
Is it a bad practise to create objects in the room creation code?
Should I just put them in the room manually?
 

jo-thijs

Member
It's not bad practice at itself.
It might become bad if you purposely try to make it a bad thing.
If you're concidering doing it, I suppose there must be a reason for it.
In that case, go ahead.
 

Phil Strahl

Member
For me, the biggest problem is that after a few weeks of coding, I forget about putting code in the room. I had some weird bugs because of some old code that was in there since the prototyping days.

To have all my code easily accessible it to have objects calling scripts in each even. For example, my practice is to have an invisible setup-object in an empty room at the very start of my game. The object calls all the scripts which I use to do the initialization and jumps to the first "real" room when finished, so I never need to actually put code in some room :)
 

RangerX

Member
You can do whatever you want in the creation code of a room. But good practice in my opinion would be to be organised and coding clean. Put code where it makes sense to put it. Comment your code. No wait, comment your code even more!
In the creation code of a room, it would make sense to put the setting for that room. Let's say your game is having background settings for each room, music settings, spawn points settings, etc. Anything that feels like "room settings" for you should be in there. If you put something that is odd, you might forget about it like Phil up there.
 

ZeDuval

Member
It's not bad practice at all. But there's one thing you need to consider! The room creation code doesn't get called before everything else. If there are objects manually placed within the room, their Create Event will come before the room creation code. If one of this objects would in some way rely on some initialization-code within the room creation code, for example a global variable, the variable might not be defined yet. Oh the trouble. This is how I do it:
I don't place any objects manually onto the map. At the end of the room creation code, I create my unique meta object and the reference to it in form of the constant(macro) 'META' like this:
Code:
META = instance_create(0,0,obj_meta);
Every initialization that should happen before everything else just comes before this.
The constant 'META' equals a global variable like 'global.__meta_obj'. This object then creates and manages all the other "meta objects", for example obj_ui, which manages everything related to the user interface. If you create it within the Create Event of 'META', like this...
Code:
UI = instance_create(0,0,obj_ui);
...you get a nice reference like 'META.UI', which you can access from everywhere since META correlates to the global var. Objects like this create and manage all other objetcs from there on in a tree-like-hierarchy.

SD
 

MudbuG

Member
If it works for you it is fine.

I like to place this sort of code in a "controller" or "spawner" object in the "Room Start" event. I have found it too easy to lose track of code that I put in room creation, so I tend to avoid it.
 
Top