How do I change a local number in a different group?

Get help using Construct 2

Post » Sat Sep 27, 2014 1:18 pm

EDIT: THE ISSUE IN THIS POST HAS BEEN SOLVED. SEE A COUPLE POSTS DOWN FOR THE NEW ONE I'M HAVING. (didn't want to make a whole new thread)

I'm making a game where you can upgrade the various statistics of your weapons, and to organize it better and make things work consistently I have organized the weapons' stats and firing patterns into groups:

Image
(ignore the names and stuff, this is just from my early dev file)

Inside each group are a bunch of variables that pertain to the stats of each weapon as well as the math that determines what they do:

Image
(the player will be able to upgrade or change each of these numbers)

If I place a button inside the group I can change the number, but all of my upgrade buttons and my pause menu are in a separate group because they have to be in order to be usable. So, is it possible to change a global number that is inside a different group, or will I have to do some reorganizing? If it isn't possible, how would you recommend I go about fixing/reorganizing it?
Last edited by CJacobsSA on Sat Sep 27, 2014 7:54 pm, edited 1 time in total.
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Sat Sep 27, 2014 1:37 pm

Global variables are not linked to a specific group, in that case they will turn into local variables, and are only available to that group.
I don't think there are any logical or good way to do it. Due to several reason, it would be very inconvenient to work with variables that way, and it will complicate things for you unnecessarily.

Want i would suggest and a way that i personally think is a good way to organise things, as im not a huge fan of global variables in general, as you tend to end up with a very long list and after a while its very hard to maintain an overview of what variables does what and where they are used, so i use them only for very few things. Instead i would suggest you to make a small sprite or a series of them, what you prefer and depending on how many weapons you have, but just a size of 16x16 and place it outside the layout and you use this/these for storing your weapon settings.

So if you give it a name like "Weapon settings" whenever you need to get a value or setting for a weapon you know that these are found in that sprite. And you don't have to worry about loosing control of them, as these are only used for weapons. So when you look through your "Add action/Add event" things will be a lot more organized. And if you ever need to change any settings you can just update the values in that sprite.

I normally use several of these types of sprites, for "Names", "Weapons", "System settings", "Enemy settings" etc. and since they are so small in size they will have near to no impact on performance and they allow you to use Booleans. And if you need them to be global you just set the sprite global value to true.

However just be sure that whenever you need a value that you get it from the sprite, and not just make a "Set weapon damage = 2" etc as that would give you wrong data at some point. However that also means that its very easy to update your whole project in one go, if you decide at some point that a certain weapon should do more damage.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Sat Sep 27, 2014 1:56 pm

Ahh, that does seem like a clever way of doing things. I'll try that. Thank you for the quick reply! :)
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Sat Sep 27, 2014 7:45 pm

Update: Now that I've made the changeover and re-written the code, other things have broken! Previously the shotgun fired straight out of the barrel at the angle I set:

Image
(old code method)

But now when I fire, this happens:

Image
(new code method)

I can't tell at a glance what has changed or what I did wrong in the crossover, so I'll post both the new and old shotgun fire code and let people more experienced than me take a look at it (you'll probably have to open them in a new tab to view them in full, they're big and I don't have enough posts to use URLs yet).

Image
(old code, note the variables being part of the group)


Image
(new code, variables are a part of a sprite called 'codebox_shotgun_stats')
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Sat Sep 27, 2014 8:02 pm

Based on post from nimos, I assumed you implemented it, LocalVariable always reset to it's default value, while StaticVariable does not, so you need to reset those variable in the For loop action to make it behave similarly to your old version.
B
28
S
8
G
4
Posts: 553
Reputation: 4,924

Post » Sat Sep 27, 2014 8:06 pm

If you made more than one "codebox_shotgun_stats" you need to pick the correct one first. Note, you don't need the "Group is active" test as that code can only be reach IF the group is active!
ImageImageImage
B
71
S
21
G
232
Posts: 3,695
Reputation: 129,866

Post » Sat Sep 27, 2014 8:12 pm

DuckfaceNinja wrote:Based on post from nimos, I assumed you implemented it, LocalVariable always reset to it's default value, while StaticVariable does not, so you need to reset those variable in the For loop action to make it behave similarly to your old version.


This does sound like it would work, but I'm not sure I understand what I need to do to reset the variable to what it was before and still have the gun be upgrade-able. If you could go a bit more in-depth I'd appreciate it. :oops:

blackhornet wrote:If you made more than one "codebox_shotgun_stats" you need to pick the correct one first. Note, you don't need the "Group is active" test as that code can only be reach IF the group is active!


Thanks for the tip, redundancy removed!
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Sat Sep 27, 2014 11:24 pm

After doing some bug testing it appears that a_shotgun_x is the culprit. When this line is enabled in the code:

Image

the shotgun does the angle thing it was doing above because:

Image

a_shotgun_x does not reset to 0. If I set it to reset to 0 in the action where the gun fires, it will only shoot 1 pellet for god knows what reason. I've officially given up on trying to explain why this is in words so here, I'll just upload my dev .capx and let you guys play around with it if anyone's interested in finding out what's wrong and/or want to explain how to fix it.

After doing some bug testing it appears that a_shotgun_x is the culprit. When this line is enabled in the code:

Image

the shotgun does the angle thing it was doing above because:

Image

a_shotgun_x does not reset to 0. If I set it to reset to 0 in the action where the gun fires, it will only shoot 1 pellet for god knows what reason. I've officially given up on trying to explain why this is in words so here, I'll just upload my dev .capx and let you guys play around with it if anyone's interested in finding out what's wrong and/or want to explain how to fix it.
You do not have the required permissions to view the files attached to this post.
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Sun Sep 28, 2014 4:57 am

Line32, set a_shotgun_1 to 0.
B
28
S
8
G
4
Posts: 553
Reputation: 4,924

Post » Sun Sep 28, 2014 5:20 pm

Haha, wow, don't I feel stupid. I was putting it in the wrong place. Thanks a bunch!
B
18
S
9
Posts: 103
Reputation: 1,817


Return to How do I....?

Who is online

Users browsing this forum: ChoosyZerk and 72 guests