GML Scripts and instance creation

Hi.

I am making a script that takes an argument how many times an instance will be created based on a time interval.

The code goes like this:

Code:
InstancesToCreate=argument0;
for(i=0;i<timeInterval*InstancesToCreate;i++)
{
 if (i mod timeInterval == 0) {
  instance_create
 }
}
However, instead of creating one instance during the time interval all instances are created when the for cycle ends at the same time.

Am I doing something wrong here?
 
B

Bgamemaker

Guest
A theme worth understanding with programming is timing. At first glance, it may appear you are offseting the timing of instance creation - and you are - just not how you expect.
Issue : The block of code provided will be executed completely before game maker can move on to something else. Meaning that, in this step of the game all of this code will be executed. In order to achieve a delayed object creation, you need to focus on creating objects during different steps. One way to do this is with alarms. Good luck
 
Hm, I see. So what you are saying is that all of this happens in a single step.

However I found a weird thing, if you set an audio to play before the instance creation the audio plays as intended, during the set time interval.

I'll try to find a workaround for this as I try to avoid using alarms as much as possible. I'll use them as a last resort.
 

samspade

Member
Hm, I see. So what you are saying is that all of this happens in a single step.

However I found a weird thing, if you set an audio to play before the instance creation the audio plays as intended, during the set time interval.

I'll try to find a workaround for this as I try to avoid using alarms as much as possible. I'll use them as a last resort.
Why are you trying to avoid alarms? They're a critical part of controlling when things happen in your program. If you don't like GM's built in alarms you can always make your own.

GameMaker: Simplest possible custom alarms

Audio runs separately from the rest of your program's code. So in general it will just start and continue until the audio ends regardless of what else is happening.
 
With using alarms I would create a script to create only one instance while using an object's alarms as time intervals. I considered using custom alarms but I was trying to use multiple instance creation based on script execution and not object steps so that the script wouldn't be dependant on the object's alarms.
 
T

TinyGamesLab

Guest
You can still do it using scripts.
You'd have to pass a variable name (not the value) to the script and have the script check of it greater than 0. If it is, spawn a new object and reduce 1 from such variable.
 

FrostyCat

Redemption Seeker
What you want can still start with a script, but the part that runs chronically must be mediated by an object. There's no other way around it.

instance_create_multiple:
Code:
///instance_create_multiple(x, y, obj, n, interval)
with (instance_create(argument0, argument1, worker_instance_create_multiple)) {
  obj = argument2;
  n = argument3;
  interval = argument4;
  alarm[0] = interval;
}
worker_instance_create_multiple Alarm 0:
Code:
instance_create(x, y, obj);
if (--n == 0) {
  instance_destroy();
} else {
  alarm[0] = interval;
}
Also see: What's the Difference: Loop Structures vs. Step Checks and Alarms
 
T

TinyGamesLab

Guest
Yes I did but you haven't read mine.
My post was meant for TinyGamesLab.
My post should work as long as your script uses the variable name, as a string, and you use
variable_instance_get and
variable_instance_set functions.
Something like this (sorry but I'm posting from my phone):

Code:
Create event:
Instances_to_spawn = 5;

Step event:
Script_create("Instances_to_spawn");

Script Script_create:
Var n = variable_instance_get(self.id, argument[0]);
If (n > 0)
   {
   //instance creation code
   variable_instance_set(self.id, n - 1)
   }
This will run once per step and will create 5 instances before stopping.
I agree that it may not be the best code, since it'll be evaluating every step only to create something for 5 steps, but it should work as a approach not to use alarms.
 
Top