I want to be able to make a game that requires you moving on a grid based area of blocks. However, I have searched many places and have not seen any answers. I want to figure out how to make it so your player can move like a board game piece. Here’s a quick example sketch I drew up. Thanks!
I know, but do you know any places where I can learn? Or find an answer? Because if you don’t want to answer, then don’t.
search in
Doesn’t narrow things down at all, but thanks.
make a part for each board tile
then teleport the thing on top of it
(this dude’s being unhelpful)
The best way to do this is to create a 2D array that maps each position on the board, then for each position, state if an object is in that grid. It makes math surprisingly easy if you know which order your 2D array is in, I like to do [y][x]
For example, here’s a 2D array for a 3x3 grid
local board = {
{},
{},
{}
}
local character = Character.new(board)
character:MoveTo(1, 2)
--internally this does
function MoveTo(X, Y)
if self.X and self.Y then
board[self.Y][self.X] = nil
end
self.X = X
self.Y = Y
board[Y][X] = self
end
Generally, each character would hold their own state which maps where they are, it makes this a hella of a lot easier to implement
This is probably way out there but its how i’d implement it.
Thank you for being actually helpful! I will look at this and try it out.
Basing a slight reference off of your code, I pulled off a quick working prototype:
My code:
local grid = {} -- the grid
-- some variables
local gridSize = Vector2.new(11, 11)
local cellSize = Vector2.new(4, 4)
-- fill the grid
for y = 1, gridSize.Y do
-- set grid[Y cordinate]
grid[y] = {}
for x = 1, gridSize.X do
-- set grid[y][X cordinate]
grid[y][x] = Vector2.new(x, y)
-- to visualize the grid
local part = Instance.new("Part")
part.Anchored = true
part.Size = Vector3.new(cellSize.X, 1, cellSize.Y)
part.Position = Vector3.new(cellSize.X * x, -0.5, cellSize.Y * y)
part.Color = x % 2 == 0 and y % 2 == 0 and Color3.new(0.85, 0.85, 0.85) or Color3.new(0.5, 0.5, 0.5)
part.Parent = workspace.GridVisual
end
end
-- function that handles
local function movePartToGrid(part, position)
local fetchedPosition = grid[position.Y][position.X]
part.Position = Vector3.new(fetchedPosition.X * cellSize.X, part.Size.Y / 2, fetchedPosition.Y * cellSize.Y)
end
local part = workspace.Indicator -- the red part in workspace
while true do
local randX = math.random(gridSize.X) -- generate a random x cordinate to move the part to
local randY = math.random(gridSize.Y) -- same with y cordinate
movePartToGrid(part, Vector2.new(randX, randY)) -- finally move it
wait(1)
end
Edit: Place file - Grid MoveTo System.rbxl (23.1 KB)
I would personally keep the rectangles inside an array with the index being their number on the board since its more convenient for a board game, for example a 8x8 grid would have rectangles numbered 0-63
Here’s some example code just place it inside a frame it should scale accordingly:
local ParentFrame = script.Parent
local xGridsize, yGridsize = 8,8
local GridArray = {}
-- Calculate the absolute rectangle size to keep the grid scaled
local xframeSize, yframeSize = ParentFrame.AbsoluteSize.X/xGridsize, ParentFrame.AbsoluteSize.Y/yGridsize
-- just to visualize
local function show_rect_number(rectNumber, parent)
local newtext = Instance.new("TextLabel")
newtext.AnchorPoint = Vector2.new(0.5,0.5)
newtext.Position = UDim2.fromScale(0.5,0.5)
newtext.Size = UDim2.fromScale(1,1)
newtext.Text = rectNumber
newtext.Parent = parent
end
for x = 0, xGridsize-1, 1 do
for y = 0, yGridsize-1, 1 do
local rectNumber = (y + x*yGridsize)
local newFrame = Instance.new("Frame")
newFrame.Size = UDim2.fromOffset(xframeSize,yframeSize)
newFrame.Position = UDim2.fromOffset(x * xframeSize, y * yframeSize)
newFrame.Parent = ParentFrame
GridArray[rectNumber] = newFrame
show_rect_number(rectNumber, newFrame)
end
end
function move_on_grid(player, position)
local frameToMoveTo = GridArray[position]
player.Position = UDim2.fromOffset(frameToMoveTo.Position.X.Offset + (frameToMoveTo.Size.X.Offset/2), frameToMoveTo.Position.Y.Offset + (frameToMoveTo.Size.Y.Offset/2))
end
local playerFrame = Instance.new("Frame")
playerFrame.BackgroundColor3 = Color3.new(1, 0, 0.498039)
playerFrame.Size = UDim2.fromOffset(10,10)
playerFrame.AnchorPoint = Vector2.new(0.5,0.5)
playerFrame.Name = "player"
playerFrame.ZIndex = 5
playerFrame.Parent = ParentFrame
-- move the player to a rectangle on the grid
move_on_grid(playerFrame, 60)
The simplest way would be to just have a 2d array of sorts. Or really it can be considered a vector2.
Board[Y][X] = ""
Ideally, you would want to set the state of each piece as well as the board tile.
local Piece = {
Position = Vector2.new(1,2)
}
local Tiles = {
[1][2] = {
State = "Full";
}
}