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

How to access variables from another script WITHIN an object?

Discussion in 'Programming' started by gacl, Jun 29, 2016.

  1. gacl

    gacl Guest

    I have an object that uses ammo, obviously I cannot define the variable in the same step script as the bullet firing etc, as the ammo would be reset every time it looped. So, within an object how can I access a script in the create event from a script in the step event?

    Something like:

    object.create_script.variable += 3;

    Stuff like that.

    How do I access the variable though? I've already tried out just typing out the script...
     
  2. FredFredrickson

    FredFredrickson Member GMC Elder

    Joined:
    Jun 21, 2016
    Posts:
    118
    Maybe I don't understand what you're trying to do, but why don't you just define the variable in the object's Create event and use the scripts to manipulate it?
     
  3. gacl

    gacl Guest

    Tha's what I want to do... But, how do I do this?
     
  4. RangerX

    RangerX Member

    Joined:
    Jun 20, 2016
    Posts:
    2,596
    In the create event of the object you declare the variable:

    name = value;

    In the scripts, you manipulate that variable like any other. If however you call that script in an object that doesn't have this variable, it will crash.
     
  5. chorrus

    chorrus Member

    Joined:
    Jun 21, 2016
    Posts:
    111
    But he can also modify that variable even if he called it from another object, use something like this.

    with(obj_abc){//The object with the variable you want to access
    var -= 1//The avriable and whatever you want to do with it
    }
     
  6. anomalous

    anomalous Member

    Joined:
    Jun 29, 2016
    Posts:
    464
    obj_player = player instance that has ammo variable
    ammo_count = 5; // in obj_player create event

    From any other object you can access ammo_count using :
    obj_player.ammo_count

    Or if you want to work "inside" of that object use :
    with obj_player
    {
    // do stuff here as though you were in obj_player, have access to all obj_player variables, etc.
    }

    If using with obj_player, and you want to access a variable back in the original instance you are working in, use other.<variable>
     
  7. gacl

    gacl Guest

    Thank you, how can I compare variables like this?

    E.G.

    if (with obj_player {variable > 5})
    {
    do stuff
    }

    would this work, how would I go about doing so if not?
     
  8. gacl

    gacl Guest

    [
    I want to access the variable from within the same object, just from a different script within the object. I tried doing obj_player.variable however I got the same runner error of variable is not defined / does not exist. Am I doing something wrong?
     
  9. FredFredrickson

    FredFredrickson Member GMC Elder

    Joined:
    Jun 21, 2016
    Posts:
    118
    Any variables you declare in the Create event are always with the object after that.

    So in Create, you say:
    Code:
    ammo_count = 10;
    Then, in the Draw event, you could do:
    Code:
    draw_text(10,10,string(ammo_count));
    ...which will draw the value of ammo_count in the player's draw event.

    Is this along the lines of what you're trying to do?
     
  10. gacl

    gacl Guest

    Holy **** thanks man, the problem was in the create event I declared variables prefixed with the var keyword:

    var beans = 5; //example

    however if I remove the var it works. Thanks!
     
  11. chorrus

    chorrus Member

    Joined:
    Jun 21, 2016
    Posts:
    111
    That wouldn't work, it would be this:
    with(obj_player){
    if(variable>5){
    //do stuff
    }
    }

    Everything inside with(obj_player) works like if you had it in obj_player
     
  12. FredFredrickson

    FredFredrickson Member GMC Elder

    Joined:
    Jun 21, 2016
    Posts:
    118
    You're welcome.

    You only use var when you want the variable to be temporary.

    Don't forget that you can look up this sort of thing in the GM manual (click the little circle with the ? inside) if you're confused about what a function does.
     
  13. RangerX

    RangerX Member

    Joined:
    Jun 20, 2016
    Posts:
    2,596
    So basically you didn't know enough about variable's scope.
    Have a read here and I think this will round up your necessary knowledge on the subject:

    http://www.yoyogames.com/blog/33

    :)
     

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