language:corona:lua
차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판이전 판다음 판 | 이전 판 | ||
language:corona:lua [2017/04/06 19:49] – kieuns | language:corona:lua [2024/04/23 22:44] (현재) – 바깥 편집 127.0.0.1 | ||
---|---|---|---|
줄 1: | 줄 1: | ||
+ | ====== 언어 퀵 요약 ====== | ||
+ | ==== 일반 ==== | ||
+ | |||
+ | <code lua> | ||
+ | -- 한줄 주석 | ||
+ | |||
+ | --[[ | ||
+ | 블록 주석 | ||
+ | ]] | ||
+ | |||
+ | -- NULL | ||
+ | local v_nil = nil | ||
+ | |||
+ | -- 변수 | ||
+ | foo = 3 | ||
+ | |||
+ | -- 함수 호출 | ||
+ | print(foo) | ||
+ | |||
+ | print(" | ||
+ | -- ';' | ||
+ | |||
+ | v1 = 10 -- | ||
+ | local v2 = 12 -- local | ||
+ | print(" | ||
+ | |||
+ | a = " | ||
+ | do | ||
+ | print(" | ||
+ | local b = " | ||
+ | c = " | ||
+ | end | ||
+ | print(" | ||
+ | print(" | ||
+ | print(" | ||
+ | |||
+ | |||
+ | print(type(" | ||
+ | print(type(10.4 * 3)) | ||
+ | print(type(print)) | ||
+ | |||
+ | --[[ type | ||
+ | - nil | ||
+ | - true, false | ||
+ | - string | ||
+ | - number | ||
+ | ]] | ||
+ | |||
+ | --[[ 비교 연산자 | ||
+ | > , < >=, <= , ==, | ||
+ | ~= (같지않다.) | ||
+ | ]] | ||
+ | |||
+ | --[[ 논리 연산자 | ||
+ | and, or, not | ||
+ | ]] | ||
+ | |||
+ | --[[ 그외 연산자 | ||
+ | 문자열연결 : " | ||
+ | 테이블문자열 길이 변환: # | ||
+ | ]] | ||
+ | |||
+ | -- if 을 사용하려면 | ||
+ | math.randomseed(os.time()) -- 어쩌다 한번 해주는게 좋지 | ||
+ | local rnd = math.random(1, | ||
+ | if rnd % 2 == 0 then | ||
+ | print(rnd .. ": 짝") | ||
+ | else | ||
+ | print(rnd .. ": 홀") | ||
+ | end | ||
+ | |||
+ | -- 반복문 for | ||
+ | for i = 10, 1, -1 do -- 1, 10, 1 == 변수, 조건, 증감연산자 | ||
+ | print(i) | ||
+ | end | ||
+ | |||
+ | -- 반복문 while (선검사 후실행) | ||
+ | local i2 = 1 | ||
+ | while i2 <= 5 do | ||
+ | print(i2) | ||
+ | i2 = i2 + 1 | ||
+ | end | ||
+ | |||
+ | -- 반복문 repeat (선실행 후검사) | ||
+ | local i3 = true | ||
+ | repeat | ||
+ | local rnd = math.random(1, | ||
+ | print(" | ||
+ | if rnd < 50 then | ||
+ | i3 = false | ||
+ | end | ||
+ | until i3 == false | ||
+ | |||
+ | ------------------------------------------------------------------------------- | ||
+ | |||
+ | -- 함수 | ||
+ | function add1(a, | ||
+ | return a+b; | ||
+ | end | ||
+ | -- 함수 : local, global 있다 | ||
+ | local function add2(a,b) | ||
+ | return a+b | ||
+ | end | ||
+ | -- 함수 : 변수에 할당 가능 | ||
+ | local fn_add3 = function(a, b) | ||
+ | return a^b | ||
+ | end | ||
+ | print(fn_add3(10, | ||
+ | -- 함수 두개 이상 값 돌려줄 수 있다. 각자 타입이 달라도 된다. | ||
+ | |||
+ | |||
+ | ------------------------------------------------------------------------------- | ||
+ | -- 모듈1 | ||
+ | |||
+ | -- string 모듈 | ||
+ | print(string.byte(" | ||
+ | print(string.char(65)) | ||
+ | print(string.find(" | ||
+ | -- gmatch(string, | ||
+ | -- string.gsub(스트링, | ||
+ | -- string.gsub(스트링, | ||
+ | |||
+ | -- math | ||
+ | -- os | ||
+ | --os.exit() -- 종료 | ||
+ | |||
+ | -- 프로퍼티와 메소드에 접근할 때 | ||
+ | -- . 연산자를 이용합니다. | ||
+ | -- Lua의 경우 프로퍼티는 . 로 접근하지만 메소드는 : 을 이용합니다. | ||
+ | |||
+ | ------------------------------------------------------------------------------- | ||
+ | -- 모듈2 | ||
+ | |||
+ | local ControlMyCar = require " | ||
+ | local car1 = ... -- ' | ||
+ | ControlMyCar.connect(car1) | ||
+ | ControlMyCar.lockDoors(false) | ||
+ | |||
+ | |||
+ | ------------------------------------------------------------------------------- | ||
+ | -- 코루틴 | ||
+ | |||
+ | -- 코루틴은 쓰레드가 아니다. 아래 코드를 실행하면 fn1()만 무한 실행된다. | ||
+ | --[[ | ||
+ | local _b = true | ||
+ | local function fn1() | ||
+ | while _b do | ||
+ | print(" | ||
+ | end | ||
+ | print(" | ||
+ | end | ||
+ | local function fn2() | ||
+ | _b = false | ||
+ | end | ||
+ | local co1 = coroutine.create(fn1) | ||
+ | local co2 = coroutine.create(fn2) | ||
+ | coroutine.resume(co1) | ||
+ | coroutine.resume(co2) | ||
+ | ]] | ||
+ | |||
+ | -- yield로 현재루틴을 잠시 중지하고 다른 코루틴이 실행 되도록 한다. | ||
+ | local _b = true | ||
+ | local function fn1() | ||
+ | while _b do | ||
+ | print(" | ||
+ | coroutine.yield() | ||
+ | end | ||
+ | print(" | ||
+ | end | ||
+ | local function fn2() | ||
+ | _b = false | ||
+ | end | ||
+ | local co1 = coroutine.create(fn1) | ||
+ | local co2 = coroutine.create(fn2) | ||
+ | coroutine.resume(co1) | ||
+ | coroutine.resume(co2) | ||
+ | -- yield는 실행을 그 지점에서 종료하는 것, 뒤부분을 실행하려면 resume()를 추가로 호출해야한다. | ||
+ | coroutine.resume(co1) | ||
+ | |||
+ | ------------------------------------------------------------------------------- | ||
+ | -- 테이블: 배열, | ||
+ | |||
+ | local _table = {} | ||
+ | _table[1] = 1 | ||
+ | _table[" | ||
+ | print(_table[1] + _table[" | ||
+ | |||
+ | -- 숫자, 문자, 함수, 모듈, 객체 모두 저장가능 | ||
+ | local function temp_fn_1() | ||
+ | print(" | ||
+ | end | ||
+ | local _tbl2 = {a=3, b=" | ||
+ | print(tostring(_tbl2.d.random())) | ||
+ | print(_tbl2.c() ) | ||
+ | --print(_tbl2.c() .. _tbl2.d.random()) | ||
+ | |||
+ | local _tbl3 = {3, | ||
+ | -- table.concat() 요소연결 | ||
+ | print(table.concat(_tbl3,' | ||
+ | -- table.copy() 테이블 합치기 | ||
+ | -- table.indexOf() | ||
+ | table.sort(_tbl3) | ||
+ | print(table.concat(_tbl3,' | ||
+ | -- table.remove | ||
+ | table.remove(_tbl3, | ||
+ | print(table.concat(_tbl3,' | ||
+ | --print(table.indexOf(_tbl3, | ||
+ | |||
+ | ------------------------------------------------------------------------------- | ||
+ | |||
+ | -- API | ||
+ | --[[ | ||
+ | tostring() | ||
+ | tonumber() | ||
+ | pairs() 테이블을 key, value 형태로 되돌려주는 함수 | ||
+ | ipairs() 키가 숫자로 된것만 리턴하는 | ||
+ | ]] | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== 커스텀모듈 ==== | ||
+ | |||
+ | <code lua> | ||
+ | -- 별도 파일을 만들어서, | ||
+ | |||
+ | -- 이렇게 시작 | ||
+ | local Class = {} | ||
+ | |||
+ | Class.car = nil | ||
+ | |||
+ | Class.connect = function(car) | ||
+ | Class.car = car | ||
+ | print(" | ||
+ | end | ||
+ | |||
+ | Class.lockDoors = function(isLock) | ||
+ | print(" | ||
+ | end | ||
+ | |||
+ | Class.ignition = function(isOn) | ||
+ | print(" | ||
+ | end | ||
+ | |||
+ | Class.move = function(isForward, | ||
+ | print(" | ||
+ | end | ||
+ | |||
+ | Class.handling = function(isLeft, | ||
+ | print(" | ||
+ | end | ||
+ | |||
+ | return Class | ||
+ | </ | ||
+ | |||
+ | 쓸때는 | ||
+ | |||
+ | <code lua> | ||
+ | local ControlMyCar = require(" | ||
+ | -- ControlMyCar.lua 파일 있음 | ||
+ | local car = ... | ||
+ | ControlMyCar.connect(car) | ||
+ | </ | ||
+ | |||
+ | 다른 폴더 밑에 두었다면, | ||
+ | |||
+ | <code lua> | ||
+ | local ControlMyCar = require " | ||
+ | </ |