Suggestions for streamlining/simplifying code needed

Get help using Construct 2

Post » Fri Jun 12, 2015 1:28 pm

Hello everyone,

I'm looking for some suggestions for simplifying this section of code.

I have a series of objects which can be assigned to whatever slots the player wants. Those objects then have various states which need to be reflected to the player.

At the moment I have a global variable for each slot, which I then write the objects name into.

In the level, I have to manually check which object is in which slot, which is a really ugly and unwieldy way of doing things.

At the moment it looks like this:

System|Slot A = Object A - Do This
System|Slot A = Object B - Do This
System|Slot A = Object C - Do This
System|Slot A = Object D - Do This

System|Slot B = Object A - Do This
System|Slot B = Object B - Do This
System|Slot B = Object C - Do This
System|Slot B = Object D - Do This

System|Slot C = Object A - Do This
System|Slot C = Object B - Do This
System|Slot C = Object C - Do This
System|Slot C = Object D - Do This

ect. You get the picture.

This feels like a really awful and unwieldy way of doing things; can anyone point me in the direction of the better implementation? I'm clearly missing something simple here.

Thank you very much
B
13
S
6
Posts: 130
Reputation: 1,553

Post » Fri Jun 12, 2015 3:29 pm

I can only make a suggestion to eliminate the huge pile of global variable.

Add an array. Each box of the array can represent as a systemslot.
For example Array.at(0,0) can be seen as systemslotA
Array.at(0,1) as systemslotB etc.

You can store the Objects in it when the conditions are met.

Though, you will still have the long list with different actions
Array.at(0,0)= Object A - Do This
Array.at(0,1)= Object B - Do This
(...)

gl!
It's time for me to return the favor :)
B
32
S
9
G
9
Posts: 132
Reputation: 5,370

Post » Fri Jun 12, 2015 3:45 pm

Hi Ikke,

thanks for your input - I have been considering going down the array route and it is on my list of things to do. My major concern though is the huge number of actions getting called all the time in my current system - there are over 20 potential objects and 6 slots, which equates to a huge amount of redundant/repeated code. As the 'fuel' system for each object is also tied to the slot it is in (where the fuel is displayed) I find I am running repeat actions over and over.

Does anyone have any further suggestions?

Thanks
B
13
S
6
Posts: 130
Reputation: 1,553

Post » Fri Jun 12, 2015 5:04 pm

Is the "Do This" various or everywhere the same?
If its various you cant shrink.
If its same you can do it with a few rows.
Last edited by deathangel1479 on Fri Jun 12, 2015 5:25 pm, edited 4 times in total.
Sorry for my bad english, but I really do my best! And use and check with Google Translator.
B
10
S
4
G
1
Posts: 158
Reputation: 1,303

Post » Fri Jun 12, 2015 5:05 pm

Functions?

There is almost never any reason to repeat the same code over and over again..
If a lot of the actions are basically the same you could probably call a function, maybe even one repeated 6 times for each slot..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Fri Jun 12, 2015 5:32 pm

LittleStain wrote:Functions?

There is almost never any reason to repeat the same code over and over again..
If a lot of the actions are basically the same you could probably call a function, maybe even one repeated 6 times for each slot..

You say it!
for each is the key! ;)
Sorry for my bad english, but I really do my best! And use and check with Google Translator.
B
10
S
4
G
1
Posts: 158
Reputation: 1,303

Post » Mon Jun 15, 2015 11:04 am

@LittleStain

@deathangel1479

Thanks for your input :) you are right there is never a good reason to run so much redundant code - the problem I couldn't get my head around is not all objects run the same actions.

In the end I have gone with an array, which I search for the name of each object and then extract data based on the names location in the array.

Unfortunately this system isn't perfect and only partially works. I was hoping you could answer a couple more questions:

My array looks like this

Object A | 1 | 2 | 3 | 4 | 5 |
Object B | 1 | 2 | 3 | 4 | 5 |
Object C | 1 | 2 | 3 | 4 | 5 |

I have a set of global variables which store the names of the objects in each slot. For example Global Variable SlotA = "Object A"

My code is then

Array|Contains value = SlotA - SlotAText | set text to Array.At(Array.IndexOf(SlotA)+1)

I was hoping this would look in the array for the value matching the value in the global variable Slot A, then set the text object SlotAText to the value in the cell 1 further along on the X axis.

I had expected this to set the text to "1" but this isn't the result.

Can anyone suggest why on earth this would cause the text to be set to "Object B"?

Am I adding one onto the wrong axis? Is there a way to specify which axis to add one onto?

Thanks in advance.
B
13
S
6
Posts: 130
Reputation: 1,553

Post » Mon Jun 15, 2015 12:26 pm

That gives me headache. :?
That's why I do not use that stuff.
I use plugins for that, can be much easier. :D

Of course I use my own. Tools
It allows me direct access.
But other can not cope with it, I think.
May be because my bad explain.
Sorry for my bad english, but I really do my best! And use and check with Google Translator.
B
10
S
4
G
1
Posts: 158
Reputation: 1,303


Return to How do I....?

Who is online

Users browsing this forum: Laura_D, plinkie and 20 guests