GML resizing imported sprites vs. GM sprites?

Discussion in 'Programming' started by jwrose, Mar 14, 2019 at 2:51 AM.

Tags:
  1. jwrose

    jwrose Member

    Joined:
    Aug 26, 2018
    Posts:
    39
    Hello!
    I've run into something that's causing me some problems and I'm curious if this is related to GM treating imported sprites (PNG files) differently than sprites drawn in GMS2. Below is a sample of some code I'm using to resize all enemies on the layer "enemy2". What's throwing me is that my placeholder enemies that were drawn in gamemaker are resizing just fine. My other enemies, which are imported png files, are not resizing. Is there something I need to do differently for imported sprites?

    elements = layer_get_all_elements("enemy2");
    for (var i = 0; i < array_length_1d(elements); i++)
    {
    if (layer_get_element_type(elements) == layerelementtype_instance)
    {
    var inst = layer_instance_get_instance(elements);
    show_debug_message(inst);
    inst.image_xscale = 0.5;
    inst.image_yscale = 0.5;
    }
    }

    Thanks!
     
  2. Danei

    Danei Member

    Joined:
    Mar 23, 2018
    Posts:
    205
    I'm unaware of any such difference between sprites.

    Since elements is an array, you probably need to be specifying the indices inside the loop:

    Code:
    elements = layer_get_all_elements("enemy2");
    for (var i = 0; i < array_length_1d(elements); i++)
    {
    if (layer_get_element_type(elements[i]) == layerelementtype_instance)
    {
    var inst = layer_instance_get_instance(elements[i]);
    show_debug_message(inst);
    inst.image_xscale = 0.5;
    inst.image_yscale = 0.5;
    }
    }
     
    jwrose likes this.
  3. jwrose

    jwrose Member

    Joined:
    Aug 26, 2018
    Posts:
    39
    OK, odd thing here. I did already have that in my code and I can't figure out why it wasn't in my copy/paste. Below is another copy/paste, looks like it brought it over this time. I also removed the unnecessary debug message line.


    Code:
    elements = layer_get_all_elements("enemy2");
    for (var i = 0; i < array_length_1d(elements); i++)
        {
         if (layer_get_element_type(elements[i]) == layerelementtype_instance)
            {
            var inst = layer_instance_get_instance(elements[i]);
             inst.image_xscale = 0.5;       
             inst.image_yscale = 0.5;
            }
        
        }
    
    Any other thoughts on why imported sprite based characters wouldn't be affected? I have confirmed they are all on the same layer.
     
  4. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    549
    When is this code run?

    I was thinking if it is in the create event and the the order that the instances are created could matter.
     
    jwrose likes this.
  5. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,618
    Because when not within a code block, your missing symbols are a control for turning on italics. If you look at your original code posting, it was WAI. :)
     
    jwrose likes this.
  6. jwrose

    jwrose Member

    Joined:
    Aug 26, 2018
    Posts:
    39
    That's a good point. I am doing this in the create event, but planning on altering them more in step events. Let me play around with it and see.
    Thanks!
     
  7. jwrose

    jwrose Member

    Joined:
    Aug 26, 2018
    Posts:
    39
    OK, I did some tests tonight and I'm even more confused about how its processing this.

    1) I tried placing the code into many other create events for other objects that are always present = same result, it doesn't work (I'm trying to keep it in create events so it runs once)
    2) place it in a step event for various objects = WORKS ... but its running this code every frame, which isn't necessary and is using resources
    3) did an experiment by setting a variable on the create event (makeBig = 1), used that as an if statement in the step event where it worked previously (if makeBig =1, then run the code, and added makeBig = 0 at the end. (example below) I was hoping this would trigger it on one frame, make the change, and not run the code again. Well, the code ran once, but DIDN'T affect the xscale of the problem enemies. I placed it at the start of the step event, in the End Step event, no dice. I REALLY thought this would do it since it would essentially be combining #1 and #2.

    I'm trying to think of any other way I can load this. I could check if the instances exist, but doing that on create event leaves me with little else i can do if it DOESN'T exist since that runs a check once and I don't see what I can do if its false (other than having false run a script that tries a 2nd time, but I'd hate to build a redundancy like that). If I do it in a step event, I don't see how it would process things any differently than what I'm already trying with the makeBigger variable. I'm still stumped as to why it works just fine with some enemies and not another- consistency.

    Anyway, open to any other theories or suggestions.

    Code:
    if (makeBigger == 1)
    {
        elements = layer_get_all_elements("Enemy2");
        for (var i = 0; i < array_length_1d(elements); i++)
            {
             if (layer_get_element_type(elements[i]) == layerelementtype_instance)
                {
                var inst = layer_instance_get_instance(elements[i]);
                 inst.image_xscale = 2.5;       
                 inst.image_yscale = 2.5;
                }
            }
         makeBigger = 0;
    }        
    
    
     
  8. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    985
    The simplest fix is to just set an alarm to 1 on Create and then run the code in that alarm. That way everything will be initialised before the code runs. You won't need to wait more than 1 step unless you're specifically delaying creating the instances for some reason.
     
    jwrose likes this.
  9. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    549
    Since it works, but only if you run the code every step, the more likely issue is you set image_xscale somewhere else, which is overriding the change you hoped you were making.

    Even if you doubt this is true, do a search for image_xscale from the edit menu and go through all search results to consider if this is happening.
     
    jwrose likes this.
  10. jwrose

    jwrose Member

    Joined:
    Aug 26, 2018
    Posts:
    39
    Ha! Believe me, I don't doubt you at all. That's a VERY good point. I'm going through my results now and checking if that's set somewhere for that object.
     
  11. jwrose

    jwrose Member

    Joined:
    Aug 26, 2018
    Posts:
    39
    Yep, that was it! I forgot that long ago I made some code to control the direction those characters were facing- which also controlled their size. I'm going to rework that code. THANK YOU for the suggestion. As soon as I read it, it made too much sense to not be the solution. I guess it reminded a lot me of a common issue with website CSS classes & styles that I run into with my day job :)
     

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