• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Running code after creation code

S

Simulacron

Guest
I want to run some code after certain variables were set in the creation code. Whats the best way of doing this? I don't want to copy-paste any code in each creation code. Is there some sort of "After-creation-code"-event? Running an alarm set to 1 was another idea I had, but it seems very clumsy to me. Is there a better way of doing things?
 
Do you mean after the objects Create Event, or after the instances Creation Code in the Room Editor?

What is the end result you are trying to achieve?
 
Either way, is the code you want to run the same for each instance? If so, put the code in a script and call the script at the end of the creation code/create event.
 
S

Simulacron

Guest
Do you mean after the objects Create Event, or after the instances Creation Code in the Room Editor?

What is the end result you are trying to achieve?
I mean the creation code.
Either way, is the code you want to run the same for each instance? If so, put the code in a script and call the script at the end of the creation code/create event.
I thought of that too, but I don't like that I would need to past in the call for the script in every creation code. Isn't there a way to put it in the object itself?
 
Unfortunately, there isn't a more "elegant" method. You can either call a script at the end of the creation code, or use a 1 frame alarm, or a boolean toggled section of code in the step event. Beyond that, the best you're going to be able to do is have a control instance create everything programmatically, but it's a lot of work for minimum gain if all you want is a "cleaner" way of doing it.
 

Dupletor

Member
You can make creation code for objects and for instances separately... There is a "creation code" option in the room creation tool, and there is the "creation event" for the object.
You can put a script in the end of it in both cases. If it is in the creation event for object, it will happen to every instance of that object. If it is in the "creation code" option when you place an object in a room, it will only happen to that specific instance.

Both cases need to be done in the Creation event, there isn`t a "post creation event", as it would be a creation event itself if it did.
 

Electros

Member
You can also create user defined events within objects - and call them after your variables are defined.
 
You could try the Room Start Event, if this only needs to happen on room start.

From the docs:

The order that the different events will fire is:

  • Create Event followed by the Instance Creation Code of each instance - so as each instance is created, it will run first its Create event and then its Instance Creation Code before moving on to the next instance to be created

  • Game Start Event - this event is triggered once in the very first room of the game for all instances placed in the room from the room editor (note that calling game_restart() will trigger this event again.

  • Room Creation Code - this is the one-off code written in the Room Editor for when a room is first entered

  • Room Start Event of all instances - one of the "other" category of events and will fire for all instances, persistent or otherwise
EDIT: Still curious though, what is the reason you need to do this, is there no other way to restructure your initialization sequence?

It might shed some light on what you are trying to do, and someone might have more suggestions based on this.
 

Dupletor

Member
You could try the Room Start Event, if this only needs to happen on room start.
No. This is very bad habit, as if it is made before the creation of the applied instance it will break the game, or randomize behavior of pre-existent instances.
If the code is supposed to be made when an object is built, use the constructor. Doesn't matter if it is already build when code is applied.
The creation code is made for this.
 

Electros

Member
Which is pretty much identical to calling a script and still would need to be done in all cases.
I find they can be cleaner in some situations, as user defined events can be inherited and contained within the relevant object or set of objects, overidden if required etc.

That's not to say scripts can't do the same, but it makes keeping code only applicable to a set of objects, to just that set of objects (reducing potential effects of script editing unintentionally impacting something else that calls it).
 
S

Simulacron

Guest
You could try the Room Start Event, if this only needs to happen on room start.

From the docs:



EDIT: Still curious though, what is the reason you need to do this, is there no other way to restructure your initialization sequence?

It might shed some light on what you are trying to do, and someone might have more suggestions based on this.
I wanted to create an And-Gate object. For this to work it needed to know which cables that run into it are the inputs and outputs. The Output cable gets set in the creation code. The code that should run afterwards just looked for the remaining input cables.
However, I now use the script methode and everything works perfectly fine. Thanks for your help!
 
No. This is very bad habit, as if it is made before the creation of the applied instance it will break the game, or randomize behavior of pre-existent instances.
If the code is supposed to be made when an object is built, use the constructor. Doesn't matter if it is already build when code is applied.
The creation code is made for this.
Agreed. It could lead to all sorts of bugs if any instances were created in code after the Room Create Event happened.

Also, as the OP has not mentioned what problem they are trying to solve, it was kind of a last ditch suggestion. But, will take care not to recommend this kind of thing as a solution for this type of problem in future.
 

Neptune

Member
I usually do something like this in a "Begin Step"
Code:
if !ini
{
   ini = true;
   //Do stuff
}
That being said, if you want to ensure it is done before the instance appears, you could do it in a "Pre Draw" event.
 
Top