So basically I recently worked on a placement system. I already did the placement system but now its the datastore part. Games such as Restaurant Tycoon 2, Miner’s Haven have different plots. What I thought at first was to have a string value and save the position of the part and then when the player loads back in the game, I would just clone the parts to the saved position. But I recently came out with an issue. Plots. There would be different plots in the game so the game could fill more than 7 players. But obviously saving the positions wouldn’t work for this since the player would then have the be on the same plot. So, how do games do this?
I’m not looking for any scripts, just looking for ways I could attempt to work this out.
For my city builder which is based on a grid, I save the grid coordinates of the front left corner, and save the orientation snapped to the nearest 90 degrees (1=90,2=180,3=270)
Edit; more details.
Orientation is calculated relative to the grid orientation, using toObjectSpace as described by other repliers.
Using the grid coordinates and relative rotation, for any given grid in any given orientation and position, we can work out exactly where to place the building and the orientation to give it to reproduce the area.
Thanks a lot! I never needed to make a saving system like this but I also always had the idea of saving the part’s cframe, but never knew how to save it relative to the plot or whatever
This will be made much easier if you have a table that indexes all of the items in your system, i.e. instead of storing your items as “One item”, “Another item”, “Big item” you have something like
local ItemIndex = {
{
Name = "One item",
--more info about it
},
--do the same for the rest
}
and simply index item info by getting ItemIndex[someIndex]
Then, it’s not too bad. xuefei already gave a nice method to get relative positioning on the plot, so you should just use that info to create a 2D array that you’ll store. Get the relative position, divide it by whatever stud increment your plot has (for example, if you have 10x10 slots, divide the position (50,20) by 10 to get (5,2) and make your grid much cleaner for storing), and use the coordinates to make a simple grid. Note that I omitted the Y value in these vectors as I doubt that’ll matter much for you in this use case.
--To store
local GridData = {}
for i = 1, xSize do --xSize is the X dimension of your plot, in terms of slots
GridData[i] = {}
end
for _, item in pairs(ItemsOnPlot) do
--pos is the position you get relative to the plot, using methods already posted above
--item.Index is just representing the index you used to reference this item via ItemIndex earlier, and also the one you will use for loading later
GridData[pos.X][pos.Y] = item.Index
end
--To load
for x = 1, #PlotData do --assuming PlotData is the grid that you load in
local row = PlotData[i]
for z = 1, #row do
local index = PlotData[x][y]
--Check, as there might not necessarily be an item in the spot
if index then
local item = ItemIndex[index]
--See! How easy was that? Do whatever you want from here; you have the grid position for the item along with an easy way of accessing all the static information that might go into it
end
end
end
If I messed something up let me know, I haven’t dealt with this in a while