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

GM:S 1.4 There is a problem in this piece of code that I am unable to debug!

Discussion in 'Programming' started by prithvidiamond, Jun 26, 2016.

  1. prithvidiamond

    prithvidiamond Member

    Joined:
    Jun 21, 2016
    Posts:
    39
    Hello everyone,

    According to the GML compiler, There is an error in this piece of code:

    if (mouse_check_button_pressed(mb_left) and fruit = fruitguess)
    {
    points += 1;
    repeat (1) if fruit = 0
    {
    draw_self();
    draw_sprite(spr_fr_wm,0,320,150);
    }

    can someone please help me in where I have gone wrong,

    ALL answers are appreciated,

    prithvidiamond
     
  2. Jordan Robinson

    Jordan Robinson Member

    Joined:
    Jun 22, 2016
    Posts:
    86
    Could you post the error message?
     
    ParodyKnaveBob likes this.
  3. prithvidiamond

    prithvidiamond Member

    Joined:
    Jun 21, 2016
    Posts:
    39
    Below is a picture of the error meessage:
     

    Attached Files:

  4. prithvidiamond

    prithvidiamond Member

    Joined:
    Jun 21, 2016
    Posts:
    39
    Here is a the error when I run the game (both error messages seem to be the same).
     

    Attached Files:

  5. Jordan Robinson

    Jordan Robinson Member

    Joined:
    Jun 22, 2016
    Posts:
    86
    Well, the error message is saying that somewhere you are using a variable called i which hasn't been set. Is there more to the step event code than what you posted? Sometimes it doesn't give the position of the error exactly right so you have to do a bit of looking around.
     
  6. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,872
    The error tells you that you haven't set your variable "i" you are using as array pointer in "fruitguess"
     
  7. prithvidiamond

    prithvidiamond Member

    Joined:
    Jun 21, 2016
    Posts:
    39
    I fixed the variable part by setting it as globalvar but now this came up:
     

    Attached Files:

  8. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,872
    The value of i is now larger than there are entries in your array. Check your code where the value is set and ensure it remains within valid range. Also, you can copypaste error text from GM compile view, so you don't need the extra step of cutting screenshots.
     
  9. zendraw

    zendraw Member

    Joined:
    Jun 20, 2016
    Posts:
    1,330
    now it says that you are out of the array your using`s range so if your max array is var[5] and you call var[6] or var[-1] you get that error. arrays start from 0 cant go below.
     
  10. hippyman

    hippyman Member

    Joined:
    Jun 20, 2016
    Posts:
    565
    Please stop posting screenshots of your error messages. LOL!

    You know you can copy and paste the message right? Much quicker and easier I might add :)

    you can use a custom wrap function or your can use clamp to keep it within range after you change the value also.
     
  11. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,327
    Here is a habit that you need break ASAP: Blindly declaring every variable in the global scope with globalvar. I've seen this in your other topics and nobody called you out on it, but you've got to smell the coffee now.

    The object that you are trying to debug clearly has the potential to manifest as many instances at the same time. If you declare its variables with globalvar, all of them will share the exact same value. This is not wanted behaviour, but you made it that way.

    Read the Manual entries on scope:

    As a rule of thumb:
    • If this variable has no relevance beyond the current piece of code, declare it as local.
    • If this variable needs to persist beyond the current event and requires an independent copy per instance, declare it as instance.
    • If this variable needs to persist beyond the current event and is singular throughout the game world, declare it as global without using globalvar.

    With its deprecated status and an entourage of cross-platform inconsistencies, using globalvar is itself already a bad habit. Abusing it like this is many times worse.
     
    ParodyKnaveBob likes this.
  12. mwgriffin

    mwgriffin Guest

    I am getting the same error message as his first error message, i have
    -------
    a = 5;
    show_message(string(obj_thisobjectsthathasadifferentname.a));
    ------
    It works fine until I add a second room to the project . . . doesn't matter if show_message is right after delcaration, or called in another object - the error
     
  13. ParodyKnaveBob

    ParodyKnaveBob theUndiscovered

    Joined:
    Jun 23, 2016
    Posts:
    440
    Howdy, prithvidiamond,

    My answer -- hopefully still appreciated like you said $;^ J -- won't really address your issue directly. It's intended to help clarify things for yourself and others. In short, the big thing I want to say is that you would do well to read the manual on all the functions and keywords your use:

    Code:
    if (mouse_check_button_pressed(mb_left) and fruit = fruitguess) // using = for a comparison instead of == is a confusing habit ... it's supported for legacy stuff, but let's move onto new stuff that reads more clearly! $:^ ]
        {
          points += 1; // just fwiw, you can make this shorter and simpler via ++points;
          repeat (1) if fruit = 0 // what .. *do* you intend with this repeat loop here? it's only going to run once, unconditionally, meaning it'll do the same if no repeat(1) were here at all .. and then again = vs == $:^ ]
        {
            draw_self(); // is this code in the Draw Event? .. do you mean for this instance to draw itself for only the frame you click?
            draw_sprite(spr_fr_wm,0,320,150); // I take it this is a separate sprite, but do you mean for this also to only be drawn for the one frame in which you click?
        }
    }
    Also, I can't really even read your indenting. I'm not trying to pick at you. I'm just trying to help you. $:^ ] Personally, I like to leave my opening brackets on the end of my opening lines, but it appears you want the style of dropping one line, thus I'll go with that. Perhaps you want the brackets in the same column as the opening lines' starts?
    Code:
    if (mouse_check_button_pressed(mb_left)  and  fruit == fruitguess)
    {
        ++points;
        if (fruit == 0)
        {
            draw_self();
            draw_sprite(spr_fr_wm, 0, 320, 150);
        }
    }
    Something I had to learn the hard way as a teenager and beyond is that whitespace is our friend! $:^ J But now, that cleaned up some, the questions remain as to what you intend to do. Others have already addressed your variable issue, including the "duct tape fix" of globalvar not being a true solution, thus I don't have much to say there -- other than, just because a specific error stops popping up, that doesn't mean you've actually solved a problem.

    I do hope you can solve all this eventually, ~nod~ $:^ J
    Bob
     
    prithvidiamond likes this.
  14. prithvidiamond

    prithvidiamond Member

    Joined:
    Jun 21, 2016
    Posts:
    39
    All your answers are very much appreciated, You all inspire me! Will try all answers given :)
     
  15. prithvidiamond

    prithvidiamond Member

    Joined:
    Jun 21, 2016
    Posts:
    39
    [UPDATE] :

    I have learnt use scripts and have made my coding more dynamic and easy to code.

    Thanks for all you inspiration especially Bob and FrostyCat (I appreciate others too!)

    The problem although still persists but I think I am close to solving it!

    prithvidiamond
     
    ParodyKnaveBob likes this.
  16. mwgriffin

    mwgriffin Guest

    found my issue . . . it was a globalvar error from marketplace code. if anybody else has this issue, make sure you use global.whatevervariables
     
    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