1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 Question about spawning instances of children

Discussion in 'Programming' started by Yizzard, Nov 9, 2019.

  1. Yizzard

    Yizzard Member

    Joined:
    Jul 2, 2018
    Posts:
    18
    So I have this system where all items (weapons, consumables, etc.) are children of an overarching obj_Item. It's children are obj_Weapons, obj_Consumables, etc. which in turn are the parents to obj_SteelSword, obj_BucketOGoo, etc. which are actual items. I wanted to have an item bank that basically just spawned in one of each item, read from their variables then deleted all of them. I have it now just listing out like
    Code:
    itemList[0] = instance_create_depth(x, y, 5000, obj_NoWeaponEquipped);
    itemList[1] = instance_create_depth(x, y, 5000, obj_WoodenSword);
    ...
    for every single item in the game. then at the end I iterate through itemList and delete them all. I was just wondering if there was some way to spawn in every child of the obj_Item object or maybe even the obj_Weapons and obj_Consumables objects so I don't have to add an item to this bank every single time I add an item to the game. Is there some way to iterate through all children of an object and spawn each one in or something like that? I haven't seen anything like that but I very easily could have just missed it.
    Thanks for any tips!
     
  2. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,981
    So if I understood this right, you spawn the instances because you want to read their certain values - like weapon damage, armor value, etc - to some centralized data structure?
     
  3. Yizzard

    Yizzard Member

    Joined:
    Jul 2, 2018
    Posts:
    18
    yes, exactly
     
  4. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,881
    Why not have all the data in a centralized data structure to begin with? Even a 2D array / ds_grid is good enough for most use cases (item ID on the x axis, different data on the Y axis, use macros/constants to improve readability)
    upload_2019-11-10_20-46-25.png
     
  5. Yizzard

    Yizzard Member

    Joined:
    Jul 2, 2018
    Posts:
    18
    Well I mean I could but I don't really see how that's any more efficient than what I have. It's pretty much the same thing. It still has the same issue of having to go back and add each item to the list of items every time I make a new item. I was just wondering if there was any way to do something like instance_create_all_objects_of_type(obj_Item) in order to create one of every obj_Item's children. There may not be any way of doing that automatically, but I was just hoping there would be to make it slightly easier on myself.
     

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