Dynamically populate array for farming type game

0 favourites
  • 4 posts
From the Asset Store
Change delay, create new lines, "backspace" the text
  • Hello,

    I have been enjoying Construct on a dozen of small games and I wanted to get to the next level but find myself in front of a wall that no tutorial so far was able to remove.

    I am working on an UI base management game where you can build production units (a farm for example) and then produce something (vegetables for example) using this production unit. It is possible in my game to have several instance of the same production unit available therefore same production (vegetable) from 2 or more similar unit can run at the same time. Since my game is 100% UI based I call the same layout for all similar production units.

    Producing through a unit takes time and also yield a result (in that case vegetables). Not being too familiar with data management I looked around and dynamically populating an array seemed like the best solution. Sadly I was unable so far to populate it the way I want (Production name, time to completion, yield) a bit like that:

    (note that since my game is time based, you have X in game days to complete it, the time to completion is a calculation of 2 var (remaining days - vegetable production time) that return a date compared to the current date)

    Vegetables 350 15000

    Vegetables 350 15000

    Wheat 346 6000

    Inserting data in the array with this format is where I am currently blocked. I will also need later to check the date, transfer the yield (with its type) into inventory and then destroy the finished line in the array.

    If anyone could help me understand a bit better how to populate an array dynamically without a set size and using the At() expression (since it s dynamic) I ll be more than grateful.

    Thanks!

    Tagged:

  • Not sure if array is totally required for this although it could act as a way to view all data. In the end you will be pulling variable data into the array and those variables would be good enough. So a farm would have production type which is a string variable, number of stored or created products, production time/speed as number variables etc.

    If I was to put all this data in an array, what you can do is give each building an identifier which is the position or row in the array. Then with logic you link up the building's values to the array, so type = array.at(building.arrayposition,0), production time = array.at(building.arrayposition,1). This would allow all buildings to put their data into the array with very little code.

    Also I would advise putting all buildings into a buildings family for this, so you do not have duplicate code referencing different buildings, farm.arrayposition,0 and mill.arrayposition,0 become building.arrayposition,0 if you use families and family instance variables to group them.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hello Plinkie,

    Thank you for your answer. This was somewhat the way I started working on it but I still had the problem of different instance of the same production unit producing on a different time since in this case there is only one instance of the type "farm" in the array.

    For example I start producing vegetables on day X in farm A and a day later I produce vegetables in farm B. I will need to track both production time as separate entries in the array and return the yield on each finishing dates.

    I also want to try to have a day log of current and finished production on current day so an array seemed like the best solution.

    Thanks!

  • It's fine to use an array for tracking but my point is if you use instance A and instance B of farm, you will still be using variables so the array is just for holding all the data. The idea is on creation of the building you set the array identifier to 1, then increase it in general by 1 (this would be a global variable to compare against), so when you create the next farm the identifier is 2. The identifier links to a row in the array so you can have all buildings in there. As for a log of the days activities you could total up a column of units produced etc

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