Problem With Weapon Switching Logic & Creating/Destroying Objects

0 favourites
  • 14 posts
From the Asset Store
Futuristics Weapons Vol 1 includes 8 different weapons, in a total of 216 sounds
  • I'm working on a simple weapon switching mechanic whereby the player is able to switch between two physical objects; a gun and a shield. The way I invisioned the logic working is that when the player hits right bumper on their controller, it destroys the current weapon and replaces it with a different one at the same position.

    I have managed to get this working in a pretty janky manner, but it's giving me some issues which have probably got something to do with the general logic and order that I'm issuing events. The major problems I have are;

    1. I can't get my events to work with the same button (i.e. Left Bumper) - I have to switch each weapon to a separate button for it to switch back and forth. I'm assuming this is something to do with the structure of my events, and there's a way of approaching this that I'm missing.
    2. When the weapon switching is working with the adjustment I mentioned above, the positioning seems glitchy. When the new weapon object is created, it reads its angle from a global variable (i.e. the left stick's last reported position) yet it seems to momentarily reset to directly in front of the player.

    Here are my events, and I've also posted the project. Thanks for any help you can provide.

    https://www.dropbox.com/s/clbq6141tbk8d8m/WeaponSwitch.c3p?dl=0

  • Just looking at the events, because they run top to bottom that's probably why it isn't working for one button because the first condition would be true then you change the variable so the second one is true. Combine those two conditions into one event with an Else and it should work. Not sure on the angle and I don't have controller to test with but have you thought of just changing the visibility of the weapons instead of create and destroy?

  • Put your set angle action in the event that the object is created and directly after the creation action. At the moment your logic is creating a new object at 0° for one tick and then the next tick it is getting set to the angle. If you put the set angle action in the same event as the creation, it will be created at that angle.

    Also change your create object position for the aim object to player.imagepointX(1), player.imagepointY(1).

  • Amazing, thank you both so much - I'll try both approaches and report back if I still have any difficulties. I hadn't considered hiding the objects for some reason!

  • I got this working perfectly using the advice that you both posted; again, thank you very much. As I suspected, it was my understanding of execution order that was tripping me up. I do have a question more for my own learning/understanding than anything else. I used the following events to get everything working, including the player cursor not skipping to 0 degrees when spawned. You can see that as suggested, I've specified the angle of creation on object spawn: -

    However, I also managed to get it working without specifying the angle on object spawn when the events were in the order below. As soon as I move the events into my Player Management group, it skips back to 0 degrees before adjusting to the angle stored in my variable. What is it about the execution order that has changed which causes that to happen?

    And just one more question; the Else event works really well for switching back and first between two weapons/variables, but could it also be adapted to having 3 or more weapons/variables (i.e. hitting RB on the controller cycles between 0, 1, 2)? Or am I looking at a completely different approach for that?

    Thanks for your help!

  • Not sure about your first question without seeing all of your events.

    For the second question you wouldn't use an else condition by itself, rather create a variable called WeaponSelect then use the RB press to add to the variable and set the weapon type based on the variable. Personally I'd use a function, as you might want to switch weapons based on other conditions in your game, but there are many ways you can do it.

    You can also store your weapon sprites in a single sprite object using different animation frames, or you could add destroy and create events as you liked.

    e.g. of function method

  • Amazing, thank you so much - I got this mostly working using functions (and now I understand a bit more about use cased for functions, so thank you for that too!).

    The only thing I'm running into is that the function doesn't cycle back to 0 - once initiated, it adds one every time, but seems that when WeaponSelect gets to 3, it doesn't set itself back to 0. This seems like it should work, but am I missing something about the logic?

    One final question - because of the globally accessible nature of functions, is it always necessary to use globally scoped variables (i.e. like WeaponSelect in your example)? Previously, I had been using a local variable attached to the player - I expect that using this in other parts of the game would lead to issues?

  • When you say it seems to not be working, how are you determining that, are you checking the variable in debug mode?

    Yes you can use an instance variable from the player as a parameter instead.

  • I’m outputting the value of WeaponSelect to a text box; it starts on 0 because that’s the initial value, but one the function is initiated, it never loops back to 0 as per the last event. I also have some events based on the value being 0 that aren’t activating.

  • Add 1 to variable comes after the function so you should see it as 1 when it loops but never 0.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Amazing, thank you! Switched that around and now it works perfectly.

  • I don't think it would work perfectly as it would skip 0 if you want that to run the first time, it would make the variable 1 then run the function. Depends what you want to happen in the game. You could of course change the conditions to 1, 2 and 3 instead of 0, 1 and 2 then it would run them all.

  • Hmm, perhaps I'm not understanding the logic - shouldn't the last part of the function return the WeaponSelect variable to 0? Or is that not possible, because as soon as the button is pressed, it's triggering the event which adds 1 and activates the function again (i.e. after the first use, there are currently no conditions which return it to 0 without the same button press adding 1)?

    The intention is to have the first weapon (i.e. the handgun which = WeaponSelect 0) spawned in at the beginning of the game, though obviously this can't use the Right Button event so I'm using a separate condition to do this separate from the Function.

    I see an easy fix in just assigning the weapons to 1, 2, 3 etc without using 0 as you suggested and then just change my other conditions - but I can't currently figure out a way to do it more elegantly using the weapon switch function or use 0.

    I've uploaded a current version of the project here in case anyone can suggest a way to do this: -

    https://www.dropbox.com/s/clbq6141tbk8d8m/WeaponSwitch.c3p?dl=0

    Again, thanks for your help with this, it's helping me to get my head around a bunch of concepts.

  • It looks ok except you need to press twice to get to the next weapon the first time, you need to set weaponselect to 1 on start of layout when you spawn the first weapon.

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