log("Hello World!")
-- 변수 선언
local number = 1
local sum = 0
log(sum)
-- 함수
void Sum()
{
--반복문 선언
for count = 1, 10, 1 do -- 1부터 10까지 1씩 증가
-- 조건문
if count%2 == 0 then
sum = sum + count
end
end
}
log() 내부에 출력할 값을 넣어주면 된다.local을 사용한다. 변수 선언 방식은 local number = 1 과 같이 loal 자료형 = 초기화 값 의 형태로 이루어진다.local키워드 없이 사용하여 명시적으로 지역으로 정의하지 않으면 glocal(전역)변수로 취급하게 된다. 이에 local(지역)으로 사용하길 원한다면 명시해야 한다.Lua Script는 순차적인 구조로 소스 코드가 실행된다.for count = 1, 10, 1 do log(count) endfor 이후에 반복자 = 초기값, 조건, 증감값 do 로 시작한 후, 반복할 실행문이 끝나는 지점에서는 end로 닫아준다.
local sum = 0 for cnt = 1, 10, 1 do if cnt % 2 == 0 then sum = sum + cnt end end조건문의 경우 if 조건식 then 으로 시작하여 end 사이 조건을 만족하였을 때의 수행문을 입력해주면 된다.
Funtion은 쉽게 말해 기능을 모아둔 것을 의미한다.
위와 같이 함수를 추가할 경우 다양한 함수들이 있고, 본인이 원하는 함수를 새롭게 만들어 사용할 수도 있다.
컴포넌트가 초기화가 될 때 (처음 시작할 때) 처리하는 로직이다.
viod OnbeginPlay() { local myEntity = self.Entity -- 자기 자신이 적용된 엔티티 참조 가능. log(myEntity.Name.."HelloMSW") -- 콘솔 창에 myEntity의 이름과 "HelloMSW" 출력 --OnInitialize와는 달리 아래와 같이 다른 엔티티, 다른 컴포넌트의 참조가 보장된다. local otherComponent = myEntity.컴포넌트이름 local otherEntity = _EntityService:GetEntityByPath("엔티티 경로") local otherEntityComponent = otherEntity:GetComponent("컴포넌트 이름") }
각 컴포넌트 내 OnBeginPlay의 호출 순서는 보장되지않기 때문에, 특정 컴포넌트의 OnBeginPlay로 설정이 완료된 값을 받아오게 되면 오작동 할 가능성이 있다.
예를들어, 컴포넌트 A의 OnBeginPlay에서 컴포넌트의 A의 프로퍼티 B를 ""에서 "Hello"로 설정한다 했을 때, 컴포넌트 C의 OnBeginPlay에서 프로퍼티 B를 참조하면 값이 "Hello"가 아닌 ""로 받아올 가능성이 있다.
ComponentA{
Property :
[Sync]
string B = ""
Function :
[Server Only]
void OnbeginPlay() -- 바뀌어야하는 부분
{
self.B = "Hello"
log(self.B)
}
}
ComponentC{
Property :
Function :
[Server Only]
void OnBeginPlay()
{
local myEntity = self.Entity
local componentA = myEntity.componentA
log(componentA.B) -- ComponentA와 ComponentC의 OnBeginPlay중 어떤게 먼저 호출될지 모르므로, 콘솔 창에 ""이 출력될 수 있다.
}
}
따라서 다른 컴포넌트의 OnBeginPlay에서 컴포넌트 A의 프로퍼티를 참조해야 한다면, 컴포넌트 A에서는 다음과 같이 OnInitialize를 활용해 프로퍼티 값을 설정하는 것이 좋다.
ComponentA{
Property :
[Sync]
stirng B = ""
Function :
[Server Only]
void OnInitialize() --바뀐 부분
{
self.B = "Hello"
log(self.B)
}
}
ComponentC{
Property :
Function :
[Server Only]
void OnBeginPlay()
{
local myEntity = self.Entity
local componentA = myEntity.componentA
log(componentA.B) -- ComponentA에서 OnInitialize를 통해 값을 할당했으므로 콘솔 창에 "Hello"가 출력된다..
}
}
프레임마다 주기적으로 불리는 함수이다.
void OnUpdate(number delta)
{
if self._T.Time == nil then self._T.Time = 0 end
self._T.Time = self._T.Time + delta
-- 3초마다 Console 창에 HelloMSW를 출력
if self._T.Time >= 3 then
self._T.Time = 0
log("HelloMSW")
end
}
끝날 때 처리하는 로직이며, OnDestroy와 함께 엔티티가 제거되는 시점에 1회 호출되는 함수이다.
void OnEndPlay()
{
log("OnEndPlay!!")
--console Result
--OnEndPlay!!
}