I’m making a tycoon game. I need help calculating the correct offline earnings for each car and removing the ones that expired while the player was away.
Each car earns money for a limited time, so they all have their own timers.
Variables:
I haven’t really got my head around what you’re up to yet but i noticed that you’re using a text “Zovota Paris” instead of a number for an index when you save
then you’ve not specified the index to remove Taxi Bz Worker. Keith’s the man to sort these things out in detail. But where you’ve got “Zovota Paris” use a number variable…or a variable that refers to a number variable
The Zovota Paris text is the name i gave it for the storage name. Idk if i must give it a number, since the Taxi[“Zovota Paris”] value in the save is the already the number. I’m new to calculating array variable type and i haven’t really grasped it much however i could be way off than the one with the exprience. If Keith has an idea to what i’m trying to do, i would love to see it
Firstly, there is a date and time extension and its is better to use that than pure js
The problem possibly comes from your foreach. You’re not looping through the variable you want but a number. You need to loop through the array/structure (I don’t know you’re specific goals though).
The variable _1A would be each of the children. For your Taxi_Bz_DETAILs that is each of the details such as BoughtAT, CarName, ExpiresAt and Rate while for Taxi_Bz_Worker it would be each of the details structure ever stored in the array on after the other.
The subevents containing AAA aren’t they supposed to be under the foreach so that would happen for each of the Taxi_Bz_Details.
You’re events are a bit confusing so before I continue let me clarify something. You want:
a. Player buy a car, the car and the details are set in Taxi_Bz_Details.
b. Taxi_Bz_Details are added to Taxi_Bz_Worker and Taxi_Bz_Worker
c. When the player comes back, the Taxi_Bz_Worker is retrieved and we do a foreach on it. Checking each of the Taxi_Bz_Details has expired and then removing the expired ones.
My goal is to create a timer system. When a player buys a car, it starts a timer (for example, 2 hours). The player earns 4200 per hour, so after 2 hours they get 8400. Each car lasts for 24 hours, and when that time is over, the car is removed and stops generating rewards.
Each car works independently, so it doesn’t affect other cars. For example, if a player buys one car, leaves for 2 hours, then comes back and buys another car, each car will have its own timer and its own reward system.
Plus the codes i showed for the “Car timer” it was just example i literally have no idea what to do there. So if it made no sense, so did it with me. If you can show me a better way then let me know cuz i’m new to array and calculating it.
Arrays are simply numbered structures. They are like autonumbering from 0. So when you want to access an array in gd you use myArra[0] or the number you want to access. For your structure use case you can use myArray[0].BoughtAt because the array contains structures. So your accessing a structure at 0 and then BoughtAt in that structure. The number used to access and array is called an index.
You need to setup few things for this idea to work:
When a player buys a car. We prefill the Taxi_Bz_DETAILS with what we want and the current time inside ExpireAt (ps. we store the current UTC value). We add the variable to Taxi_BZ_WORKER and save Taxi_BZ_WORKER to local storage.
When player reopens the game or when you need to check the data. We need to go through the array. We would use that same forEach you used before, The Array being looped is Taxi_BZ_Woker (as we are going through this one), The child to store it in should be Temp_Taxi_Details (you can rename),The child name Temp_Taxi_Details_No (this is because we need this to know the current index of the loop later).
While we’re looping. Every action, conditions and subevents relating to that block contains Temp_Taxi_Details and Temp_Taxi_Details_No which are currently a particular Taxi_Bz_DETAILS we saved before and its index
So we then in a subevent:
a. Get the current time passed. You can do this by simply getting the UTC of now and subtracting Temp_Taxi_Details.BoughtAt (remember that Temp_Taxi_Details is a Taxi_Details saved before) from it and convert it to hours - the difference between two UTC timestamps is in seconds - which is divided by 3600 (use divided by 60 for minutes). Probaly store it in a local variable, lets call it TimePassed (You should floor or round up this value to avoid crazy decimals)
b. Check if the TimePassed is lower than 24 and then give the player PRICE_PER_HOUR * TimePassed.
c. If TimePassed is equal or greater than 24 we give the player 24 * PRICE_PER_HOUR. This is because the player did earn up until the 24th hour so we give him all his rewards. Then we remove the car details. To do that we use Temp_Taxi_Details_No (which is the index). We use the action remove from array with index. Some we say remove child from Array Taxi_Bz_WORKER with index Temp_Taxi_Details_No
So this is a simplified implementation. Though at the ending I noticed a limitation with this idea. We always give the player what he got from the time he bought every time instead of what we owe him. So I recommend adding a new variable to Taxi_Bz_Details called Has_Given (which is default 0) and every time we remove this value from the money, we’re giving the player and save the full value of the money in this variable so we would always know the amount the player has received.
Okay I did some digging and I figured out that deleting an array child inside a loop might create shift because it is auto renumbered and thus where GDevelop uses an old variables index because of the gaps we created.
At the beginning of the For each you create a local variable array called ToBeDeleted
When we reach the block we want to delete we add it to the array instead. To be clear we add Temp_Taxi_Details_No to ToBeDeleted.
Here we need Array Tools Extension. We sort ToBeDeleted (lowest to highest) and then reverse it (highest to lowest). This is so we start deleting from the biggest to the smallest so we don’t create a gap. When an array value is deleted, it is re-auto-numbered and then during the next loop we would be accesing the wrong index because everything has been renumbered.
We then go through ToBeDeleted from start to end. For each of the children (lets say the child is IndexToBeDeleted) in ToBeDeleted we remove a child from Taxi_BZ_Worker with index IndexToBeDeleted
Hey, thanks i have good results however i had some questions and legitly i’m a bit lost. I’ll use more screenshots easier it’s easier for me to point things out while explaining.
I load the save like this then put it to the rest of the structures n arrays.
However i came into this issue, when i buy a car it doesn’t put the details into to variable, take a look.
Here’s all the variables i’m using to test this feature (ignore the Taxi_Bz_Original)
This part is where i was pretty confused and lost, since we didn’t make a specific carname variable, i pretty much just said the vehicle name. And honestly, you used local variables which even confused me more, due to where is where i should put. Also the variable that gives money here, keep giving the reward non stop.