How to simplify my repeated (spaghetti) code

0 favourites
  • 7 posts
From the Asset Store
5 levels with simple coding Source-code (.c3p) + HTML5 Exported
  • So I am trying to find a way to make the following code in the picture to work without using 2000 events for 1000 screens..

    You can see below on the picture that it has a specific pattern repeating, on corresponding array value "x" does affect the corresponding different sprite "screen_x"

    Any way to do this logic without using 2000 events for 1000 screens?

  • Do you have 1000 room_x objects? This too should definitely be optmized.

    You can add all room_x sprites into a family and make a simple loop:

    For "x" from 1 to 1000
    System Pick By Evaluate: RoomFamily where RoomFamily.ObjectTypeName="Room_"&loopindex
    ...ArrayMap value at loopindex=1
    ......RoomFamily set frame to 0
    ...Else
    ......RoomFamily set frame to 1
    

    Another way to pick RoomFamily instance is by an instance variable - create a variable RoomNumber and assign a value to each room_x sprite.

  • You can also run a loop for instances:

    For Each RoomFamily
    ...ArrayMap value at (RoomFamily.RoomNumber)=1
    ......RoomFamily set frame to 0
    ...Else
    ......RoomFamily set frame to 1
    
  • Thanks dop2000 I was actually hoping that I would get an answer from you because I have seen you around in the forums giving all the best solutions and best practices to a lot of people!

    Do you have 1000 room_x objects? This too should definitely be optmized.

    I agree, 1000 objects for the rooms is not a good idea. As a solution I thought about using the same room sprite and apply an instance variable ID from 1 to 1000. But if I do it this way then I will have more animations (about 12) for each different room and therefore the loop would not be with 0 and 1.

    I don't really understand how to apply your suggested solution in C3. If it is not much to ask could you please attach a .c3p file with this code (just as an example)?

  • Update:

    dop2000 I managed to make it work like you suggested running loop instances so no need to create and attach a .c3p file :)

    I created in my test an instance variable "RoomNumber" for the same "SRoom" sprite in order to use the same sprite for each room and tried your suggestion including more events for the different animations that are needed for the display of the map.

    It works like a dream!!! The following image displays the test (where frame 7 is the transparent - not yet visited room).

    Thank you very much for your help dop2000 , I think you do a great job here at the forums by helping people with your knowledge. I appreciate it a lot :)

    P.S. Do you think that now with this practice it will need more optimization or is this practice good enough?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Glad it worked!

    To make the code even shorted you can use ternary operator

    For each Room 
    ..Room set frame to (ArrayMap.at(Room.RoomNumber)=0 ? 7 : ArrayMap.at(Room.RoomNumber))
    

    This means - if the value in ArrayMap.at(Room.RoomNumber) is 0, then set frame 7, otherwise set frame to the value in the array.

  • Thank you dop2000 !!! this ternary operator opens a whole new world for me!

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