[함께해요, 로블록스 코딩]
*해당 게시글 내용은 오규환 교수님께서 실제 게임 개발자들이 하는 방식으로 짠 코드입니다. 천천히 따라해보세요. 만약 아래 내용이 어렵다면, 기사를 다운받아 교수님의 코드를 좀 더 쉽게 요약한 내용을 확인해보세요.
점프맵 만들기 4. 체크 포인트로 게임 난이도 조절하기
오늘은 특정 발판을 밟으면, 그 발판에서 다시 게임이 시작되는 ‘체크 포인트’를 만들어 볼게요! 이 기능이 없으면 매번 게임을 처음부터 시작해야 해서 플레이어가 게임에 흥미를 잃을 수 있어요.
체크포인트란?
플레이어 캐릭터가 게임에 처음 등장하거나 진행 중에 리스폰될 때 캐릭터가 놓여지는 위치를 의미해요. 게임을 진행하다가 체크포인트를 지나면 위치가 저장되고 캐릭터가 죽게 되면 최근에 저장된 체크포인트에 캐릭터가 리스폰 되지요.
준비 : 4개의 스폰로케이션, 킬 블록, 스크립트 파일 생성
‘탐색기→Workspace→→SpawnLocation‘을 눌러 4개의 스폰로케이션을 만들어요. 그리고 테스트를 위해 플레이어 캐릭터가 닿았을 때 캐릭터가 죽는 파트를 배치해요.
탐색기에서 4개의 스폰로케이션 이름을 각각 “1”, “2”, “3”, “4” 로 변경해요. 스폰로케이션 이름을 지정한 이유는 1씩 증가하도록 해 이전의 스폰로케이션 번호와 비교해 1이 증가한 지점에서 체크포인트가 저장되도록 하기 위함이에요.
‘탐색기→Workspace→→Folder‘로 폴더를 만들고 이름을 Checkpoints로 변경해요. 그리고 “1”, “2”, “3”, “4”를 Checkpoints 아래에 가져다 놓아요. 4개의 스폰로케이션과 캐릭터가 죽는 파트를 게임 화면에 적절히 배치해요.
‘탐색기→Workspace→ →Script‘ 로 스크립트를 만들고 이름을 CheckpointManager로 변경탐색기→Workspace는 다음과 같이 구성해요.
플레이어 캐릭터의 리스폰 위치가 저장되는 장소 확인
게임이 플레이되면 ‘탐색기→Players’아래에 현재 접속중인 플레이들이 보여요. 여기에 보이는 플레이어 개체를 선택하면 보이는 속성 중 RespawnLocation 에 저장된 스폰로케이션에 캐릭터기 생성되지요. 이 값이 없을때에는 리스폰 가능한 스폰 위치 중 한 곳에 무작위로 리스폰 돼요.
체크 포인트 처리 스크립트 요약
체크 포인트 스크립트에 들어가기 앞서, 간략한 개요를 알아볼게요. CheckpointManager에 다음 단계의 작업을 수행하는 내용을 코딩해요.
1. 모든 스폰로케이션에 터치 이벤트 설정
2. 스폰로케이션에 터치 이벤트가 발생하였을 때 체크포인트 저장
2번 체크포인트 저장은 다음 2단계를 통해 진행.
2-1 스폰로케이션 파트에 파트가 터치되면 해당 파트가 캐릭터 모델의 일부인지 확인
2-2 만약 그렇다면 캐릭터 모델에 해당하는 플레이어 개체를 찾아 RespawnLocation 속성 값을 현재 터치된 스폰로케이션으로 변경
CheckpointManager 코딩 1단계 : 스폰로케이션 터치 이벤트 설정
local checkpoints = checkpointFolder:GetChildren() local checkpointFolder = game.Workspace.Checkpoints
function checkpointSetup(checkpoint) checkpoint.Touched:Connect(function(touchedPart) -- 1 -- end end) end
for i = 1, #checkpoints do checkpointSetup(checkpoints[i]) end |
local checkpointFolder = game.Workspace.Checkpoints local checkpoints = checkpointFolder:GetChildren() |
Workspace.Checkpoints를 폴더로 checkpointFolder 변수에 저장 이 폴더의 모든 자식 개체를 checkpoints에 저장 (여기서는 checkpoints에 총 4개의 스폰 로케이션이 저장되어 있음) |
function checkpointSetup(checkpoint) checkpoint.Touched:Connect(function(touchedPart) -- 1 -- end end) end
for i = 1, #checkpoints do checkpointSetup(checkpoints[i]) end |
checkpoints에 저장된 각각의 스폰로케이션에 checkpointSetup 함수를 통해 터치 이벤트 설정 이렇게 하면 CheckpointManager 스크립트만으로 모든 스폰로케이션의 이벤트 처리를 할 수 있음 |
CheckpointManager 코딩 2단계 : 체크포인트 저장
-- 1 — 번에 해당되는 부분임
local checkpointFolder = game.Workspace.Checkpoints local checkpoints = checkpointFolder:GetChildren()
function checkpointSetup(checkpoint) checkpoint.Touched:Connect(function(touchedPart) local character = touchedPart.Parent local player = game.Players:GetPlayerFromCharacter(character) local playerCheckpoint = player.RespawnLocation.Name if tonumber(playerCheckpoint)+1 == tonumber(checkpoint.Name) then player.RespawnLocation = checkpoint end end) end
for i = 1, #checkpoints do checkpointSetup(checkpoints[i]) end |
local character = touchedPart.Parent local player = game.Players:GetPlayerFromCharacter(character) |
터치된 파트의 부모 모델에 해당하는 플레이어 개체를 찾아서 Player 변수에 저장 |
local playerCheckpoint = player.RespawnLocation.Name if tonumber(playerCheckpoint)+1 == tonumber(checkpoint.Name) then player.RespawnLocation = checkpoint end |
플레이어 개체에 현재 저장된 스폰로에케이션 이름과 부딪힌 파트 이름과 비교한다. 스폰로케이션 이름을 “1”, 2“, ”3:, “4”로 해 놓아서 플레이어 게제체 저장된 이름보다 숫자가 1더 많은 경우에는 체크포인트를 저정한다. |
CheckpointManager 코딩 3단계 (마지막) : 게임에 처음 들어 왔을 때 스폰로케이션 초기화
플레이어가 처음 게임에 접속해을대 스폰이 되는 위치를 정한다.
local checkpointFolder = game.Workspace.Checkpoints local checkpoints = checkpointFolder:GetChildren()
function checkpointSetup(checkpoint) checkpoint.Touched:Connect(function(touchedPart) local character = touchedPart.Parent local player = game.Players:GetPlayerFromCharacter(character) local playerCheckpoint = player.RespawnLocation.Name if tonumber(playerCheckpoint)+1 == tonumber(checkpoint.Name) then player.RespawnLocation = checkpoint end end) end
for i = 1, #checkpoints do checkpointSetup(checkpoints[i]) end
game.Players.PlayerAdded:Connect(function(player) player.RespawnLocation = checkpointFolder:FindFirstChild("1") end) |
game.Players.PlayerAdded:Connect(function(player) player.RespawnLocation = checkpointFolder:FindFirstChild("1") end) |
게임에 처음 접속하면 game.Players.PlayerAdded 이벤트가 발생한다. 게임에 접속한 player의 RespawnLocation을 heckpointFolder 폴더 아래에 저장된 4개의 스폰로케이션 중 “1”로 지정한다. |