Suggestions for streamlining/simplifying code needed

  • 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

  • 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!

  • 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

  • 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.

  • 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..

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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!

  • 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.

  • That gives me headache.

    That's why I do not use that stuff.

    I use plugins for that, can be much easier.

    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.

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