HELP. Advanced instance variable help needed. [Solved]

0 favourites
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • In the game I'm making the player will get many different weapons. The easiest way to manage a lot of weapons with minimal coding is to put something to use as an object that will not be deleted in a family and give the family instance variables like; damage, clip, ammo, fire rate, reloading, active, etc.

    What happens is when you select a weapon it deletes all the weapon sprites and spawns in the selected sprite and sets the "data holding objects" active variable to 1 and all others to 0. Then I can just use 1 template for all the weapons but it needs to detect which weapons is active and use its stats.

    I have done this before but I deleted it, now I need it again and I can't work it out.

    Shortened: test for which weapon is active and use its variables. But it needs to select one from multiple objects with the same instance variables by seeing which one is 'active'.

    Thank you to anyone who can help me, or anyone who can figure out an easier way.

  • You can do instance variables on individual sprites too.

    I would look at using JSON to layout all of your weapons ahead of time and load it into a rex_hash then just pull values to populate your sprite instance variables. (presuming that the stats don't change dynamically).

  • Thanks but the instance variables on the weapons which I think your talking about wont work because of two reasons, instance variables reset when their object is deleted and the stats will change throughout the game.

  • At risk of sounding a bit off of what your actually asking for, have you thought of having one sprite with multiple frames, setting each frame to a variable number (or name for that matter) for that spite and simply using that as all of the possible weapons (Or is that what you're referring to now)? You could use that in tangent with an array to keep track of ammo etc.

    Alternatively, I imagine you could also set the weapons variables to the player sprite and use those to spawn the correct weapon sprites or delete them (or change frames as mentioned above).

  • The problem with that is that I want to have about 100 weapons and this method would be very time consuming my way of having all the variables in a family and setting one weapon active and only choosing the stats from the active weapon would be the easiest and smallest way, only if I could figure out how to use the stats from the active object.

  • The problem with that is that I want to have about 100 weapons and this method would be very time consuming my way of having all the variables in a family and setting one weapon active and only choosing the stats from the active weapon would be the easiest and smallest way, only if I could figure out how to use the stats from the active object.

    if you have to make 100 weapons and you don'w want to use one sprite for all of theme (considering 2 animation per weapon) you are right, will be very confusing.

    if you use a similar weapons, you can choose different way to achieve that, one for example is just make an invisible sprite with all variables that spawn just another sprite with the design of the weapons.

    you can also divide the weapons for typology. like make 5 sprite for all weapons... for the variables, you can put into the family and make there.

    it depends how you make the weapons... so I suggest you to try without any graphics, just make a square with different frame of fire/reload and try the best method for you

  • Why not just save the active UID to some global variable (let's call it activeWeapon)? Now whenever you need to read or save data from/to instance variables you simply pick by UID and do whatever is needed.

  • The problem with that though is that I want the weapons to have clips, ammo and be able to be reloaded, these weapons don't just spawn in and get deleted they need their info saved and altered, so that's why I can't transfer the info into a 'template', I need to take it directly from the original data holder. Like every time you empty a clip it subtracts that amount from the overall ammo and stuff like that. So this is why I need to figure out how to only use variables from an 'object'. I might eventually put up a simple capx that better describes this later.

  • If you pick from weapons family by UID you grab te particular weapon, and you are able to manipulate instance variables for this particular weapon.

  • I've made a quick example.

    1. currentlySelectedWeapon holds the UID of the weapon object

    2. weapons family contains all weapons

    3. once you pick family member by UID all actions made on family objects affecting that particular object with UID you provided so you can add/remove ammo etc.

    Note that instance variables must be family instance variables.

  • This looks like it could work but how do you change UIDs and will changing them effect the game?

  • You cannot change UID, that's the point. You can also pick instances by "instanceName" as I described here:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for the help, but I'm not understanding how this will work, I've made a little demo capx with just 4 weapons but it will have all the proper features. I'll post it later and it will contain all the info needed for you to understand my side.

  • drive.google.com/file/d/0B6FgXCrZ1bnZZ2k5NFdNcHJLekk/view?usp=sharing

    That is the capx for my project, it has enough info on it, if anyone can solve it please tell me or better, repost it solved.

    just put the http:// infront of the thing, because of my limited reputation i cant technically post them.

  • Here you go https://drive.google.com/file/d/0BwT_0l ... sp=sharing

    I fixed it according to your project, but imo it needs some refactoring. First of all you are using arrays but not arrays functionality, you just use them as holders for instance variables... so you could move those instance variables to guns family and use UIDs which are faster then "codes".

    Anyway I've made it here based on "codes".

    You also could do it differently in the way you have it but I'm not sure if you realize that you set gun_data "active" property, but you never read it... so engine had no idea which gun data you want to use. You could loop the family and check for "active" property... but picking by UID is surely better idea, and much faster.

    Anyway it works, hope you understand the changes, so you could implement it in the rest of the project.

    Good Luck

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