Weird forest generation [SOLVED]

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

  1. SaltiestOfAll

    SaltiestOfAll Member

    Joined:
    Jun 11, 2019
    Posts:
    5
    Hey everyone I'm new here!
    I recently came up with a bit of code that didn't seem to work
    After a bit of twitching I cannot seem to find where my mistake..
    So what I'm basically trying to do, is generating a bunch of trees on a large map without any of them being created on a river that I generated beforehand.
    I've put the piece of code after the "river generation" part.
    Thanks in advance!

    Here's the code :
    Code:
    ///Trees
    repeat(2500){
        var xspawn = round(random_range(42, room_width-42));
        var yspawn = round(random_range(165, room_height-18));
        if (!place_meeting(xspawn,yspawn,obj_river)){
            instance_create(xspawn,yspawn,obj_pine);
        }
    }
    instance_destroy(self);
    
    And here's the result :
    (Don't worry about the depths issues, I'm perfectly aware of it)
    [​IMG]
     
  2. Gamerev147

    Gamerev147 Member

    Joined:
    Sep 12, 2016
    Posts:
    444
    Try checking if the position is free instead of meeting another object. This solution would be more universal as well, not having to check to see if the tree is colliding with every object.
    Also, a For Loop may be more efficient than a Repeat.
    Example:
    Code:
    for (i = 0; i < 2500; i ++) {
    
       var xx = random_range(42, room_width - 42);
       var yy = random_range(42, room_height - 42);
    
       if (place_free(xx, yy)) {
          instance_create(xx, yy, obj_pine);
       }
    
    }
    
    instance_destroy();
    Try that code out and let me know what happens.
     
  3. curato

    curato Member

    Joined:
    Jun 30, 2016
    Posts:
    371
    might want to check the collision boxes. the objects do have an odd shape. If there are multiple versions of the object, make sure the inheritance is set correctly.
     
  4. SaltiestOfAll

    SaltiestOfAll Member

    Joined:
    Jun 11, 2019
    Posts:
    5
    I pasted the code and made the collision boxes much bigger but it doesn't seem to change much :(

    [​IMG]
     
  5. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    223
    in the create event for the trees, couldnt you check for a collision with the river and destroy the tree ?

    in my chicken toss game, i have a spawner object that makes sure there are always a set number of chickens in the room... sometimes they would spawn overlaying on my farmhouse and get stuck in the collision wall... so in the create event of the checken i made this...

    Code:
    /// destroy self if spawn on house
    
    if point_in_rectangle(x,y,1056,864,1440,1024)
    {
    instance_destroy()
    }
    
    

    something similar might work for you?

    create event tree
    Code:
    
    if (place_meeting(x,y,obj_river))
    {
           instance_destroy();
    }
    
    
     
  6. SaltiestOfAll

    SaltiestOfAll Member

    Joined:
    Jun 11, 2019
    Posts:
    5
    Hey it actually works ! Thanks everyone !

    [​IMG]
     
  7. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    223
    highfive!!
     
  8. SaltiestOfAll

    SaltiestOfAll Member

    Joined:
    Jun 11, 2019
    Posts:
    5
    highfive!
     
  9. Liam Jacobs

    Liam Jacobs Member

    Joined:
    Jul 1, 2016
    Posts:
    157
    Here's some advice to achieve this a bit more efficiently, at a scalable level.

    Make a parent object and apply it to the tree, house and river objects.

    Then use something like this:

    Code:
    ///Trees
    var xspawn, yspawn;
    
    repeat(2500)
    {
        xspawn = round(random_range(42, room_width-42));
        yspawn = round(random_range(165, room_height-18));
    
        while (position_meeting(xspawn, yspawn, PARENT_OBJECT_HERE)))
        {
            xspawn = round(random_range(42, room_width-42));
            yspawn = round(random_range(165, room_height-18));
        }
       
        instance_create(xspawn, yspawn, obj_pine);
    }
    instance_destroy(self);
    This code is untested, but I'm pretty sure it'd work. Basically, you keep choosing a random position for each tree, until you find an empty space.
    Then we create the tree. This ensures that 2500 trees are created (I think this number may need to be toned down a little now, as you'll find that there may not be space when actually placing 2500 into the room).

    The parent object ensures that the trees cannot be placed on top of each other, the house, or the river, without having to do multiple checks for each of them.

    If you want some variation on the amount of trees, use something like "repeat(irandom_range(1800, 2500))" instead of a set number. I'd suggest changing your 'random_range' to 'irandom_range' globally, as you may run into some issues where trees are placed at locations such as "207.26, 501.07."


    Good luck! I like the art style of this project :).
     
    woods likes this.
  10. SaltiestOfAll

    SaltiestOfAll Member

    Joined:
    Jun 11, 2019
    Posts:
    5
    I've added your code to the game it works perfectly!
    Thanks for the tips such as the irandom_range() function, i'll keep the tricks in mind!
     

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