How do I Pick Up one Item and Drop Another on Key Press

0 favourites
  • 13 posts
From the Asset Store
320 high-quality cinematic sound effects for trailers and game scenes.
  • Hello again all, I'm having problems doing something I though would be simple.

    Imagine the character is holding an Item (gun, sword, shield whatever), and they run across a different Item on the ground. The character can only hold one item at a time, so on Key Press the character needs to drop the active Item and pick up the new one. Like this:

    (Blue box is the character holding the purple item. When overlapping the green item and pressing a button, character drops the purple item and picks up the green item).

    Basically think of the way Master Chief changes weapons in the Halo games. (I eventually want a little box to popup and show you Item Details too, but that is for a later time).

    I have each item as a separate Sprite with Pin and Platform Behaviours. The Platform Behaviour has Input set to off, and it is there make the Item drop to the ground when it is Unpinned from the Character. (I have some VectorX/Y set so it does a little arc away from the character).

    So far I can make it so the character can pick up an item and then drop it again, but when I try to make it swap Items with what's on the ground, stuff goes haywire.

    My thinking on how it should work:

    • Character has Item1 Pinned

    - Character Overlaps Item2

    - Key Pressed

    - Item1 -> Unpin (Item1 falls to ground)

    - Item2 -> Pins to Character | Position to Image Point

    I will eventually have a range of Items, so I'm guessing I'll need to use loops or functions or arrays or something, but I'm quite new to all this, and a lot of that is over my head for now.

    Any help would be appreciated and thanks in advance.

  • Your logic seems sound. To see why it's going "haywire" we need to see exactly what is happening, as well as the exact events driving it.

  • Hey bladedpenguin, thanks for the reply.

    I had tried quite a few things lately and hadn't saved the semi-working code, so I had to do some code cleanup to get back to how it was.

    I decided to just scrap what I had and start again, and I managed to get it working! (kind of)

    (When you press Space while colliding with a Gun Sprite, it drops the one you have and picks up the one you are touching)

    Now, this does what I want, but the code is long winded and as far from optimised as you can get:

    As you can see, it works fine with only 2 gun types, but what if I had 5 guns? Or 10? As you can see, it would quickly become unwieldy.

    What would be the best way to streamline this process? I'm guessing families would probably help out a lot, but I'm still on the free version of Construct 2, as I can't afford the Private version yet, so they aren't really an option for me.

    Loops might be the way to go, but as I mentioned before, I don't really know how they work (despite reading the manual and numerous tutorials and forum posts).

    Any thoughts are appreciated.

    Cheers.

  • Well, it's been 5 days with no response, so I guess no one can help me on this. Shame. I guess I'll keep plugging away. If I ever find a solution I'll post it up here for others to find.

  • I think the reason you haven't gotten as much help as other posts is there are several ways to do what you ask but they depend on your vision of the game you're making. Also the only example is from a 3d FPS.. No one wants to waste their time when we aren't even sure exactly what you want. Add a link to your capx posted on a free dload host will make it easier for people to realize what your trying to do and "fix" it like you envision.

    That said, I would approach this problem with the grounded weapon as the trigger. Weapons on the ground would wait for overlap with player, then fire off gui asking if player wants to swap out (ie. You found a Smack10, swap it for your Guzzi?") if the player chooses yes the weapon would attach itself to player and turn off its "can be picked up variable" a function would be called that dumps the current hand weapon. I would have an every 0.05 second event constantly running that set all weapons internal "can be picked up" variable to yes if not overlapping player.

  • Hi , thanks for your input. Sorry for the late reply, but work has been quite hectic of late.

    I used the Halo example because I thought it would be something a lot of people could relate to, and understand what I was trying to say. (I've also been watching a lot of early RvB lately)

    The basic idea is this:

    • Character has a weapon.
    • Character comes across another weapon on the ground.
    • Press a button to pick up new weapon off the ground and drop the original weapon.

    You can download a .capx of what I managed to get working here: It works well for what it is, but it only works for 2 weapons. I've been trying to get it to work with a 3rd, but so far no luck. It just gets very convoluted, very quickly.

    As for your suggestion of using a "CanBePickedUp" variable, I was already using a "GunAttached" variable, but I haven't been able to get your suggestion to work yet either.

    Back to grind-stone now. I might try something with Arrays.

    Cheers

    Edit: Apparently I'm 26 Rep short of being able to post URLs, and it's not giving me the option to upload an attachment. I'll post the .capx once I've sorted some more Rep.

    Edit2: Hooray, Rep. Let's try this: dl.dropboxusercontent.com/u/14838442/2_Gun_Swap.capx

  • I was envisioning something similar to this: https://drive.google.com/open?id=0Bx-ufu2WID53Ry1zSWNjbnFsZ00 (click the download arrow in the upper right corner of screen)

    Its crude and probably buggy but it does what it says

    Use platform controls (arrow keys) and s key to swap when overlapping "gun".

    I have it set to be empty handed at start and grab the first gun it encounters.

  • I like your way of thinking on this and, while still a bit buggy, I can see how it could potentially do just what I need it to.

    I was using Booleans for the Instance variables, but using Numbers as you are here could be the thing.

    Unfortunately as I am still on the free version the use of Families is beyond me. I had a feeling that I would come up against that limitation soon enough and it appears I have. I'll hopefully be able to purchase the full version soon, but I may have to put this aside for now. There is still quite a lot I can do in the mean-time (art assets and what-not) so I won't be idle.

    Thank you for all your help chrisinfinger.

    If I ever get this fully figured out I'll post back on this question for others to find.

  • I used families for simplicity sake. You can easily do it without.. but if you have lots of pickups you would hit 100 event cap pretty quick anyways...

  • Just thinking out loud..

    You are talking about what if 5 guns or ten, but in actuality there are still only two objects.

    An object lying on the ground and an object that the character is holding.

    Visually these might be different all the time, but the actions are very much the same.

    Wouldn't using a function to create the switch between the two be enough?

    If the guns have different ways of working, which I assume they do, calling a function with the parameter for the type of gun should do the trick when for example firing.

  • - Yeah, I think it's going to have to wait until I can afford the full version, but I have a pretty good idea of how I'm going to go about it. Cheers for everything mate, it's been a big help.

    LittleStain - Thank you for your input, but I'm trying to wrap my head around what you are saying (functions are a bit of a new thing to me). Without using families, wouldn't you still need to make a different Event for each Object? etc:

    Player - Overlap - Gun1 --> drop current weapon/pickup Gun1

    Player - Overlap - Gun2 --> drop current weapon/pickup Gun2

    Player - Overlap - Gun3 --> drop current weapon/pickup Gun3

    ... I have a feeling I just made no sense. The boss gave me 2 weeks to do 2 months worth of work, so I'm pulling late hours and I'm not really with it at the moment.

    My deadline approaches so I won't have a chance to work on this game for a while. It might give me a chance to clear my head.

    Cheers to all

  • I was thinking along the lines of making all guns different animations of the same gun-sprite.

    That way an instance variable would be used to determine which gun to display and how the gun works.

    gun if instance variable = bazooka

    fire Very big bullet

    You could also use a boolean variable InUse set to true when the player is holding it and to false when it's on the floor.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Hmm interesting idea I didn't think about making all the guns the same sprite and then just using Instance Variables to set frame/animation.

    It's something that I'll definitely have a look into, but it's unfortunately going to have to go on hold for the moment, as I have too much real-world work taking up my time. Hopefully I'll get back into it in the next week or two.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)