Assigning data from one object's variable onto another using "with"

Discussion in 'Programming' started by peceecar, Jun 11, 2019.

  1. peceecar

    peceecar Member

    Joined:
    Jun 11, 2019
    Posts:
    3
    Hi guys, I'm making a space trading and top down simulation game similar along the lines of the Patrician series and somewhere along the code I've run into an issue I'm quite unable to solve by myself. Most of code structure is established by now and all it's left is to assign resource transfer between two objects.

    Long story short: I have two objects, the miner ship (obj_miner) and the planet (obj_planet). The miner ship goes out from the planet to mine asteroids by itself (the AI completely handles this) and returns to the planet where it destroys itself as an object. Now I want when the miner object to transfer the value for the "ore" variable onto the planets variable (ore_amount) but I've run into a logic issue and I can't exactly figure out how to make this, I'm not exactly an expert programmer so I thought I'd ask here. Here is the piece of code of what happens when the miner object reaches it's end destination, the planets x,y coordinates:

    if status = "mining"
    {
    ore += 0.1
    speed = target_asteroid.speed
    direction = target_asteroid.direction
    };

    if ore > 3 or !place_meeting(target_asteroid.x,target_asteroid.y,obj_asteroid) or fuel < 1
    {
    status = "returning"
    };

    if status = "returning" && fuel > 0
    {
    move_towards_point(start_planet.x,start_planet.y,2)
    };

    if point_distance(x,y,start_planet.x,start_planet.y) < 32
    && status = "returning"
    {
    image_xscale -= 0.05
    image_yscale -= 0.05
    };
    if image_xscale < 0.06
    {
    instance_destroy()
    };

    The green highlighted part is what happens when the miner is getting near towards the planets center and starts shrinking and eventually dissapearing from the game. Everything functions well and I have to use this code structure because it's connected to a lot of other objects I just have an issue figuring out how to make this work. Now what I want is when this happens for it to transfer the "ore" amount into the planet's "ore_amount" variable. For example:


    if point_distance(x,y,start_planet.x,start_planet.y) < 32
    && status = "returning"
    {
    image_xscale -= 0.05
    image_yscale -= 0.05
    };
    if image_xscale < 0.06
    {
    with start_planet

    {ore_amount += MY ORE AMMOUNT };
    instance_destroy()
    };


    This is the issue as I don't have the skills to figure out how this actually works. Sorry for the long text, I'm pretty sure there's a simple way to do this that I just can't figure out. If this has been already solved in another thread please drop a link, I wasn't even sure exactly how to even title this thread. And sorry for my bad English, I'm not a native speaker. Thank you in advance :)
     
  2. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    536
    Code:
    if image_xscale < 0.06
    {var is_ore = MYOREAMMOUNT
    with start_planet
    {ore_amount += is_ore };
    instance_destroy()
    };
    When you have another instance being accessed using WITH in an event, then it can access local variables from the calling instance during the event. So where I have put var is_ore etc it can be read later on in the code block.

    The alternative would be to do:

    Code:
    with start_planet
    {ore_amount += other.MYOREAMMOUNT };
    But it shouldn't be necessary in this case.
     
    Last edited: Jun 12, 2019
    peceecar likes this.
  3. peceecar

    peceecar Member

    Joined:
    Jun 11, 2019
    Posts:
    3
    Alright will give this a try right now.
     
  4. peceecar

    peceecar Member

    Joined:
    Jun 11, 2019
    Posts:
    3
    Thank you so much that worked, I knew there was a much simpler way of doing this, glad I asked here before creating objects for every single piece of ore to colide with the planet so the amount grows :D
     
  5. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    536
    You're welcome :) It took me a long while to get to grips with how local variables can be accessed in scripts / events. I was creating permanent variables for everything, because I thought that was the only way they can be read by another instance. This way saves a lot of hassle.
     

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