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

Drag And Drop Create Instance

Discussion in 'Programming' started by FlatulenzaFiamm, Sep 7, 2018.

  1. FlatulenzaFiamm

    FlatulenzaFiamm Member

    Joined:
    Jul 11, 2018
    Posts:
    115
    Hi everyone! Id like to create an object via the Create_Instance, but only one.
    Because using the Step Event, the object created is more than one.

    There is a way I could create just one?

    Thanks again! :D
     
  2. Lazzeking

    Lazzeking Member

    Joined:
    May 3, 2018
    Posts:
    29
    It all depends on when you want this instance to be created.
     
  3. FlatulenzaFiamm

    FlatulenzaFiamm Member

    Joined:
    Jul 11, 2018
    Posts:
    115
    When the player reaches 2 of health, the object I want should spawn.
     
  4. Lazzeking

    Lazzeking Member

    Joined:
    May 3, 2018
    Posts:
    29
    Oh man Im terribly sorry as i didnt saw the question was about drag and drop , i though it was gml. My bad, you should wait for someone who knows drag and drop better than I do then.
     
  5. Tony Brice

    Tony Brice Member

    Joined:
    Feb 4, 2017
    Posts:
    69
    Hmm, don't know about drag and drop but in simple code form you would set a variable to false in the create event. Not sure if it's going to help putting in code form but:-
    Create:
    Trigger = false;

    Step:
    if (Trigger == false) {
    if (Health == 2) {
    Trigger = true;
    Create_instance (whatever);
    }
    }

    So, even if the health stays at 2, the creation of your new instance wont happen more than once even running in the step event.
     
    Toque likes this.
  6. FlatulenzaFiamm

    FlatulenzaFiamm Member

    Joined:
    Jul 11, 2018
    Posts:
    115
    So I just need to use a Script_Instance and copy paste what you wrote?
     
  7. Tony Brice

    Tony Brice Member

    Joined:
    Feb 4, 2017
    Posts:
    69
    I don't know as I've never used drag & drop. You'd have to change the create instance line to whatever yours is called, for starters. Additionally one of those lines goes in the create event of wherever the code is running from, not the step event. The code really runs from an actual object itself, not a script.
     
  8. FlatulenzaFiamm

    FlatulenzaFiamm Member

    Joined:
    Jul 11, 2018
    Posts:
    115
    Sorry I meant, code not script. Anyway, it says that "Create_instance" is not a script/code. What do I do?
     
  9. Toque

    Toque Member

    Joined:
    May 25, 2017
    Posts:
    1,071
    You can do this with drag and drop. You are creating an instance every step. You want to only create one.



    watch this around the 10 minute forward to see how he does it. He limits the creation to ever 3rd step but you can make it only create one.

    You need to learn about variables.
     
  10. FlatulenzaFiamm

    FlatulenzaFiamm Member

    Joined:
    Jul 11, 2018
    Posts:
    115
    The video is not avaible.
     
  11. Toque

    Toque Member

    Joined:
    May 25, 2017
    Posts:
    1,071
    Google game maker drag and drop tutorial. It’s the official tuts. 2nd one ten minutes through. Wouldn’t hurt to just watch them. If you can’t figure it out let me know.

    It’s really what Brice said but in drag and drop.

    It’s an important concept. Step will trigger 60 times a second while condition is true. So will instance create 60 times.

    In non GML terms.
    ...................

    Declare variable triggeronce = true


    If (health is 2 and triggeronce is true)

    Creat instance.
    Change triggeronce to false.
    ............


    So when health is 2 it will create one instance. Then it will change triggeronce to false.

    The code will be read again but since triggeronce is no longer true it will not create instance again.
     
  12. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    996
    I've never used drag and drop before, as it seemed limited, but just for fun I went and did what you were looking for. This is how I would do it.

    I just set a scenario where when I click the mouse button the players hp drops by 1. Each time this happens it also sets a variable called create_enemy. Then i run a check to see if hp is 2 and the variable create_enemy is true, it creates an enemy at a random x,y. Importantly it then sets create_enemy to false so it doesn't keep running.

    You'll find when making games this kind of setup is essential to lots of scenarios, so you'll use something like this many times over.

    EDIT: Toque has explained it above and essentially it's the exact same method.
     

    Attached Files:

    Toque likes this.
  13. FlatulenzaFiamm

    FlatulenzaFiamm Member

    Joined:
    Jul 11, 2018
    Posts:
    115
    Is there a way I could modify triggeronce with drag and drop?
    Or do i need code?
     
  14. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    996
    Look at my example. I modify create_enemy with d&d.
     
  15. Toque

    Toque Member

    Joined:
    May 25, 2017
    Posts:
    1,071
    Yeah thesly has done it. I’m on my phone and can’t do a video for you.
     
  16. Toque

    Toque Member

    Joined:
    May 25, 2017
    Posts:
    1,071

    What did you think of the d and d ?? It’s not as limited as most people believe.
     
  17. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    996
    After a few minutes of playing with it, it was surprisingly capable. In the picture, you can see I created a section that has "if variable" then "if expression", that I would of liked to create as "if variable" and "if expression" instead, but couldn't see how to do that. I assume it's possible, as you can add multiple checks to an "if expression" but just couldn't find how to do that for the "if variable". If that makes sense.
     
  18. Toque

    Toque Member

    Joined:
    May 25, 2017
    Posts:
    1,071
    I think I know what you mean. You have to move the box it to a certain place (middle right ) and it makes it “and”.

    It’s visual so hard to explain as you noticed. Haha.

    I came from 4 years with a visual scripting engine. So I started with the d and d. But I couldn’t easily follow and read the GM drag and drop. I found it very powerful but with that over complexity. I actually found the GML easier to read. And quickly realized since only beginners used it, help was sparse.

    But the drag and drop is capable.

    Your d and d code example will work great. I am on a phone so thanks for doing that.
     
    Last edited: Sep 9, 2018
  19. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    996
    If you look at the "if expression" and "if variable" help,
    "if expression" has this:
    These actions will now be run if the "if" evaluates to true, while any actions dropped elsewhere will be performed after the "if" block. Note that you can "stack" expressions in the same action by clicking the plus icon beside the action, and giving another expression to check. This will check if all the expressions given return true in one check and only perform the added action code if they do

    while "if variable" has this:
    These actions will now be run if the "if" evaluates to true, while any actions dropped elsewhere will be performed after the "if" block.

    That bold line is missing, indicating there is no way to have an 'and' check with an 'if variable'. I'd be happy to be proven wrong though. I was not able to replicate any middle right drops, as that wasn't an option.
     
    Last edited: Sep 9, 2018
  20. Toque

    Toque Member

    Joined:
    May 25, 2017
    Posts:
    1,071
    No worries. Your example is great.

    I looked again and yours is exactly right.
    Thanks.
     
    Last edited: Sep 9, 2018
  21. FlatulenzaFiamm

    FlatulenzaFiamm Member

    Joined:
    Jul 11, 2018
    Posts:
    115
    Sorry guys but I don't understand how to do so...
    my version of GM is older and different from yours and I can't find any of those variabilities options...
     
  22. Toque

    Toque Member

    Joined:
    May 25, 2017
    Posts:
    1,071
    Okay. I think Im getting the picture here. Ive never used old versions of GM. The logic is the same.

    Here is a tutorial that might help with the old GM. I hope this link works.



    PM sent.
     
  23. FlatulenzaFiamm

    FlatulenzaFiamm Member

    Joined:
    Jul 11, 2018
    Posts:
    115
    I kinda get what you posted, thanks to Toque's video.
    But it's still a bit confusing because of the names appearing in the variabilities.

    I'm mean, could you just explain me that using the names of my objects? And leave the variabilities that I need to chage?

    The object I want to crate is called: Ascia_Pickup

    This is what I've done, and gives me a fatal error...

    Screenshot (1).png Screenshot (2).png Screenshot (3).png
     

    Attached Files:

  24. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    996
    It looks like you almost have it working. The error says that instance_create is an unknown variable. Meaning it doesn't know what that variable is, because as my image shows, even though it's in GMS2, you need to create the variable in the create event first. You also need a condition where you set create_instance to true somewhere else when you want the create to occur.

    I've made it again in GMS 1.4 for you. My condition is when I click the mouse on the player hp goes down by 1, and I set instance_create to true then.
     

    Attached Files:

    Toque likes this.
  25. FlatulenzaFiamm

    FlatulenzaFiamm Member

    Joined:
    Jul 11, 2018
    Posts:
    115
    Nothing works... it still says unknown variable create_instance and hp...
     
  26. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    996
    If you are using a variable, it's best to first declare the variable. Meaning you have to set it to a value first in the create event. hp and create_instance need to be declared in the create event of the object that is using them.

    Like my first image shows, did you add "Set variable create_instance to false" in the create event of the "Fatica" object? You would also need to declare hp as well, if that is producing an error too.
     
    Last edited: Sep 19, 2018
  27. Swegm

    Swegm Member

    Joined:
    Apr 23, 2017
    Posts:
    106
    Create event
    executable code
    create = 1

    Step event
    executable code
    If create = 1
    {
    instance_create(x,y,whateverobject)
    create = 0
    }
     
  28. FlatulenzaFiamm

    FlatulenzaFiamm Member

    Joined:
    Jul 11, 2018
    Posts:
    115
    I've done it multiple times but it always crashes...

    Screenshot (9).png Screenshot (10).png Screenshot (11).png Screenshot (8).png
     
  29. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    996
    Well that's rather strange. Could you open the actions and screenshot them. These ones:
    "Set variable hp to 6"
    "if hp is equal to 2"
    "Set variable hp to -1"

    Also I'm assuming action number 30 is the "if hp is equal to 2" action?
     
  30. FlatulenzaFiamm

    FlatulenzaFiamm Member

    Joined:
    Jul 11, 2018
    Posts:
    115
    Yes, the line 30 is the hp set -2.

    Screenshot (12).png Screenshot (13).png Screenshot (14).png
     
  31. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    996
    I'm totally at a loss as to why it can't find your hp variable. Hopefully someone else can see something I'm missing, but all I can think of is it's bugged out. Gamemaker 1.x and above have the "Clean" button to flush the system, but I don't know if 8 had that option.

    Maybe restart the program, create a new project and just set up the one object with the same setup. Also you could get the trial of GMS2 and set it up too. At least then we would confirm it's being set up right, but the program is not doing what it should.
     
  32. FlatulenzaFiamm

    FlatulenzaFiamm Member

    Joined:
    Jul 11, 2018
    Posts:
    115
    How can I add your code?
     

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