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

Get help using Construct 2

Post » Wed Apr 01, 2015 3:16 am

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:

Image
(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.
B
7
S
1
Posts: 18
Reputation: 361

Post » Wed Apr 01, 2015 7:09 am

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.
B
14
S
5
G
1
Posts: 189
Reputation: 1,536

Post » Wed Apr 01, 2015 1:40 pm

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)

Image
(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:

Image

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.
B
7
S
1
Posts: 18
Reputation: 361

Post » Mon Apr 06, 2015 2:33 am

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.
B
7
S
1
Posts: 18
Reputation: 361

Post » Mon Apr 06, 2015 6:38 am

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.
B
40
S
8
G
3
Posts: 546
Reputation: 5,085

Post » Thu Apr 09, 2015 7:07 am

Message: Mattrix can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
Hi @chrisinfinger, 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: https://dl.dropboxusercontent.com/u/14838442/2_Gun_Swap.capx
B
7
S
1
Posts: 18
Reputation: 361

Post » Fri Apr 10, 2015 5:32 am

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.
Last edited by chrisinfinger on Mon Sep 05, 2016 10:28 pm, edited 2 times in total.
B
40
S
8
G
3
Posts: 546
Reputation: 5,085

Post » Sat Apr 11, 2015 4:54 am

I like your way of thinking on this @chrisinfinger 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.
B
7
S
1
Posts: 18
Reputation: 361

Post » Sat Apr 11, 2015 9:27 am

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...
B
40
S
8
G
3
Posts: 546
Reputation: 5,085

Post » Sat Apr 11, 2015 3:29 pm

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.
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
53
S
16
G
8
Posts: 6,157
Reputation: 19,744

Next

Return to How do I....?

Who is online

Users browsing this forum: Aphelleon, Greaver, LaroTaio and 2 guests