1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

object with variables from script?

Discussion in 'Programming' started by corwin22, Jun 16, 2018.

  1. corwin22

    corwin22 Member

    Joined:
    Nov 18, 2017
    Posts:
    73
    I have a script that is supposed to create an object with a few variables but it doesn't work I have tried using 'with' but it won't work?
    //scr_orb(Health,color,speed)
    // using this will create an entire row at the top!

    orb = (instance_create_layer(32,32,"Enemy",obj_orb))
    with (orb) {
    Health = argument0
    Color = argument1
    spd = room_speed * argument2
    }
     
  2. Carl Nylander

    Carl Nylander Member

    Joined:
    Jul 21, 2016
    Posts:
    212
    Health is a global variable, make your own instead like hp (for health pointsl
     
  3. corwin22

    corwin22 Member

    Joined:
    Nov 18, 2017
    Posts:
    73
    I changed the variable but it still doesn't get the 'spd' variable from the script.
     
  4. corwin22

    corwin22 Member

    Joined:
    Nov 18, 2017
    Posts:
    73
    ___________________________________________
    ############################################################################################
    FATAL ERROR in
    action number 1
    of Create Event
    for object obj_orb:

    Variable obj_orb.spd(100021, -2147483648) not set before reading it.
    at gml_Object_obj_orb_Create_0 (line 2) - alarm[0] = spd
    ############################################################################################
    --------------------------------------------------------------------------------------------
    stack frame is
    gml_Object_obj_orb_Create_0 (line 2)
    called from - gml_Script_scr_orb (line 4) - orb = (instance_create_layer(32,32,"Enemy",obj_orb))
    called from - gml_Object_obj_playGui_Create_0 (line 1) - scr_orb(4,4,4)
     
  5. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,327
    Create events run at the point of the call to instance_create_layer(), the code in your with block won't make it in on time.

    Create a blank object called xobj_egg, create an instance of it and load it with variables, then change it to the actual object type once it's ready:
    Code:
    with (instance_create_layer(32, 32, "Enemy", xobj_egg)) {
      Health = argument0;
      Color = argument1;
      spd = room_speed * argument2;
      instance_change(obj_orb, true);
    }
    
    This is a standard pattern demonstrated in this article.
     
  6. corwin22

    corwin22 Member

    Joined:
    Nov 18, 2017
    Posts:
    73
    thank you it worked
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice