GM:S 1.4 [SOLVED] It's possible to leave a variable with no initial value?

Discussion in 'Programming' started by Bhreno, Oct 9, 2019.

  1. Bhreno

    Bhreno Member

    Joined:
    Oct 5, 2019
    Posts:
    26
    Is it possible to leave a worthless variable until a certain point in the game, even if its value grants a sprite_index a room-specific object? Or at least leave it with a default value that can later be changed by other values still in room_start?

    Well, whenever I start the game, I leave this var with a empty value and the game crashes because it doesn't recognize any value assigned to a variable.

    The following is:
    My obj_circle in question have a var sprite_color that can grant 5 different sprites right away in create event. Follow values and results for var sprite_color:

    If sprite_color == 1 {sprite_index = spr_circle}; (default value).
    If sprite_color == 2 {sprite_index = spr_circle_green};
    If sprite_color == 3 {sprite_index = spr_circle_blue};
    If sprite_color == 4 {sprite_index = spr_circle_yellow};
    If sprite_color == 5 {sprite_index = spr_circle_orange};

    If either obj_circle does not exit with a set value, it would be considered as sprite_color = 1 (spr_circle), or the default value. If you set this default value in room_start, game_start, or create event of the obj_circle, i will not be able to change the code or sprite_index later, the values of the sprite_color lost the effect because a value has already been used before.

    My goal is to place multiple obj_circle in the room with different colors, without having to create an object for each color. Some created by an obj_circle_creator randomly by the map, others I manually set to specific positions and have already set a default value for a sprite color.

    Example: If I manually add 3 obj_circle to the room and give them these values:
    First obj_circle: sprite_color = 3;
    Second obj_circle: sprite_color = 1;
    Third obj_circle: sprite_color = 4;

    When you choose all cores with different cores, following the values of var sprite_color, they all end up with the default value (sprite_color = 1) set in obj_circle events. So I was wondering if you can create a variable and leave it with no value to play the value after the game starts, or some other way to change the value of sprite_color even though var sprite_color has a default value in room_start.
     
  2. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,742
    Why not make the sprite white, and then set image_blend to change colors? Then you only need a single sprite as well. (white blending means no change, and it's the default value of image_blend. Having the sprite be grayscale means you can blend it with any color and get exactly that color as the result)
     
    ParodyKnaveBob and Bhreno like this.
  3. Bhreno

    Bhreno Member

    Joined:
    Oct 5, 2019
    Posts:
    26
    Note: Randomly created obj_circle work perfectly by following a code in step event for sprite change. The problem is manually add obj_circles in room and try to change the value of var sprite_color with a simple code (sprite_color = 5, for example).
     
  4. Bhreno

    Bhreno Member

    Joined:
    Oct 5, 2019
    Posts:
    26

    I'm not experienced in GMS issues and still not quite sure how to use a image_blend. Also, i created all the sprites needed to make the color changes. :(
     
  5. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,742
    To make something red:
    image_blend = c_red;

    To make something blue:
    image_blend = c_blue;

    There's a whole lot of color constants, they're good enough for most common cases. You can also use make_color_rbg(red,green,blue) and make_color_hsv(hue,saturation,luminosity) to make colors using RGB and HSV values (to create any color you want).
     
    Bhreno likes this.
  6. Bhreno

    Bhreno Member

    Joined:
    Oct 5, 2019
    Posts:
    26
    And how would I use these functions without falling into the same problem of setting a default color in create event in the obj_circle that won't let me change to another color later (in room_start, for example)?
     
  7. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    537
    Howdy, Bhreno, and welcome to the GMC.

    Have you read the manual on what order the create/start events occur? You should absolutely be able to set a default value in the Create Event, then set the value you really want in the Room Start Event -- or Instance Creation Code for that matter.

    (On two other notes: 1. I agree with Yal that image_blend might be easier than all your sprite finagling. 2. Instead of the 5 if() statements, you could surely make that 1 switch() statement. Don't know how to use switch()? Great! That means "learning opportunity." Pull out the manual, read up on it, and -- just like image_blend -- experiment with it.)

    I hope this helps!
     
    Bhreno likes this.
  8. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,742
    image_blend defaults to c_white, there's no need to manually set a default value. And you can change it as many times as you want, too!
     
    Bhreno and ParodyKnaveBob like this.
  9. Bhreno

    Bhreno Member

    Joined:
    Oct 5, 2019
    Posts:
    26
    Hello! Thanks for the greeting! haha And yes, I've taken a quick look at the switches and it looks like they'll come in handy. Still, for some reason, i cannot apply a value in create event of obj_circle and change it later in start_room. A sprite_color variable is local, and in this situation is considered unrecognized or default (sprite_color = 1) to any value other than 1.
     
  10. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,742
    image_blend is a built-in variable, it will exist even if you don't set it to anything. Your custom variables won't (but you could solve that by setting them to something in the create event - changing them via Instance Creation Code happens after their creation event, so that's an option).
     
    ParodyKnaveBob and Bhreno like this.
  11. Bhreno

    Bhreno Member

    Joined:
    Oct 5, 2019
    Posts:
    26
    I will follow the tips and try to use image_blend to see what it would look like.
     
  12. Bhreno

    Bhreno Member

    Joined:
    Oct 5, 2019
    Posts:
    26
    But isn't instance creation meant to mess with more than one object? Because my obj_circle is only one and the magic happens only via sprite.
     
  13. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    537
    Instance Creation Code only messes with the one specific instance in the Room Editor.

    ~

    If you have problems with a variable being local-scoped instead of instance-scoped, it could be because you're declaring the variable with the var keyword. Using that keyword makes the variable local only to that script or event. Not using that keyword (in an instance) keeps it scoped to that one instance; i.e., if your object runs the code, and there are five instances of that object, then each of the five instances will have its own variable.

    Btw, I started to give a big ol' example, including using enums and a switch() statement, but then I saw you'd be better off using either an array to link your sprite_color values or really just image_blend anyway.

    Still, Instance Creation Code should be how you'd handle the image_blend built-in instance variable. ~shrug~ Then, the randomized circle creator could just set the new instances as they're created.
     
    Bhreno likes this.
  14. Bhreno

    Bhreno Member

    Joined:
    Oct 5, 2019
    Posts:
    26
    That was it! I was decoding sprite_color with "var" and this prevents a room_start () from recognizing the variable later! I remove "var" and it worked perfectly as I wanted! Thank you very much to both of you! I'm already reading about switches because I intend to use a lot in my project! kkk
     
    ParodyKnaveBob likes this.

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