How can I make my code more efficient?

0 favourites
  • 6 posts
From the Asset Store
Global plugin that allow for relatively easy and efficient implementation of an in-game inventory system
  • Hi there!

    I'm running into a problem - which I've come across multiple times before - but still am unsure how to 'solve'.

    I was working on a basic economy game where you can buy up to 10 flats, and charge rent for the number of floors, there are 5 floors. I also implemented a mechanic where renters would occasionally, randomly leave an apartment/floor and you'd have to click a button to rent it out again to get rent.

    There are also repairs involved, which start adding up when you purchase the building. Repairing costs money. If you don't repair, the renters start paying less money every x seconds until a minimum rent is reached. Finally, you can collect the rent. If you click on a building you get a menu with all the buttons and rent height, repair height and total rent height displayed, with a close button to close it for that building.

    There are also visual signs for each building to warn you when can collect rent, repairs are overdue etc.

    So you have building 1, 2, 3 etc. up until 10 (this image is just to give you an idea, didn't want to put the whole artwork here)

    Problem: The code works perfectly for one building. But the challenge lies in efficiently reproducing the code for all the 10 buildings which all do the same thing. Earlier I thought to myself; why not copy the code and buttons for each building? But that turned out to be a hellish amount of mindkilling work, duplicating each button, global variable, and replacing everything in the code.

    I figured it could be done more efficiently. So I started using instance variables for buttons. That reduced the amount of work largely, but I still found myself replacing and setting code for each specific building and its floors. My god.. and all kinds of quirky undesireable stuff is currently happening. In other words - it's not working and I was about to throw in the towel.

    I'm not so sure what I am doing anymore, where to look for in the code to fix it, and I'm confident there's a way more efficient way of coding this. Here are some examples of how it looks now:

    It would be awesome if someone could point me in the right way of how to handle this. Currently I've got about a 165 global variables, well, let's just say I firmly believe it can be done more efficiently, just don't know how.

    I thank you much in advance!

  • The global variables should be an instance variable on the buildings then you have 1 instead of 165. You would link the text object to the building with a condition such as is overlapping the building.

    The second section, if you put the building objects into a Family you can use one event with the Family, so for the first bit I mentioned use Family instance variables from the beginning on the buildings to enable easy use of them.

  • Hey, thanks.

    I significantly reduced the number of variables.

    I believe it is also possible to 'link' the objects if they are in the same container, correct?

  • Not subbed right now but as I recall you can't put a Family in the container with the Text so it would not be useful here but you can try it. You can link in other ways with overlap or pick nearest condition, or you could have variables on the Family and text and when you create them both together give them the same value/ID.

  • Thanks, I think it works though as I put the objects seperately in the container. And they are linked. I replaced and cut down a lot of lines of code, but still a lot of quirky things are happening. Not sure how to fix them. But on the upside, I learned a lot of new stuff like how to code more efficiently. I can't imagine ever doing that treadmill work again which I did a week ago, lol, so inefficient and time consuming.

    P.S. Maybe a bit of a personal question, you mentioned you are currently not subbed, are you not (interested in) making games anymore? Or are you perhaps using another editor, like Unity etc.? Sometimes I take a long break myself, but I always seem to come back, when thing work out it can be a lot of fun to make games.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • At the end of 2021 I wasn't involved in any game development so yet to resubscribe but I plan to at some point, I don't use any other engines, I tried and tested them but I only use Construct for making games. If I am away I am taking a break and not using something else.

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