Odain
Odain

Reputation: 11

Why my variable is illegal undefined in GML space rocks tutorial?

I'm new to GML and I've just started yesterday. I don't know why "repeat(num1)" in the "spawn_off_camera" didn't work at all and I rechecked it many times but there seems nothing wrong. Can you help me to fix it?

My error:

############################################################################################
ERROR in
action number 1
of Create Event
for object <undefined>:

DoConv :1: illegal undefined/null use
 at gml_GlobalScript_spawn_off_camera (line 9) - repeat(num1){
############################################################################################
gml_GlobalScript_spawn_off_camera (line 9)

Spawn Off Camera:

var obj = argument0;
var num1 = argument1;
var xx,yy;


var paddding = 64;

repeat(num1){
    xx = random_range(0,room_width);
    yy = random_range(0,room_height);

    while(point_in_rectangle(xx,yy,global.cameraX- paddding,global.cameraY- paddding, global.cameraX+global.cameraWidth-paddding, global.cameraY+global.camHeight- paddding)){
        xx = random_range(0,room_width);
        yy = random_range(0,room_height);

    }
    instance_create_layer(xx,yy,"Instances",obj);
}

obj_game room start:

if(room == rm_game){
    spawn_off_camera(obj_asteroid,40);
    
    if(audio_is_playing(msc_song)){
        audio_stop_sound(msc_song);
    }
    audio_play_sound(msc_song, 2, true);
    
    alarm[0] = 60;
}

obj_game Alarm 0:

if(room != rm_game){
    exit;
}
spawn_off_camera(obj_asteroid,1);

alarm[0] = 1*room_speed;

Upvotes: 0

Views: 573

Answers (1)

YellowAfterlife
YellowAfterlife

Reputation: 3192

If you are using GameMaker Studio 2.3 or newer, to use older scripts/tutorials you will need to wrap the script contents in function <name>() {<code>}.

So spawn_off_camera could be

function spawn_off_camera() {
var obj = argument0;
var num1 = argument1;
var xx,yy;


var paddding = 64;

repeat(num1){
    xx = random_range(0,room_width);
    yy = random_range(0,room_height);

    while(point_in_rectangle(xx,yy,global.cameraX- paddding,global.cameraY- paddding, global.cameraX+global.cameraWidth-paddding, global.cameraY+global.camHeight- paddding)){
        xx = random_range(0,room_width);
        yy = random_range(0,room_height);

    }
    instance_create_layer(xx,yy,"Instances",obj);
}
}

Upvotes: 1

Related Questions