Changes between Version 8 and Version 9 of Tutorial
- Timestamp:
- 01/14/10 04:12:20 (14 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Tutorial
v8 v9 50 50 ---- 51 51 == Getting Variables == 52 LuaD has a special syntax for getting variables from a table, to ensure as much type safety as you want. If you want to get any variable from Lua (including ''nil''), you can do this:52 If you want to get any variable from Lua (including ''nil'') regardless of type: 53 53 {{{ 54 54 #!d 55 auto value = LuaObject inlua["SomeField"];55 auto value = lua["SomeField"]; 56 56 }}} 57 57 This is the equivalent of doing the following in Lua: 59 59 local value = SomeField 60 60 }}} 61 Say you just wanted to see what our global variable actually was at any time; you could do:61 Say you just wanted to see what the value of our variable is; you could do: 62 62 {{{ 63 63 #!d 64 auto value = LuaObject inlua["SomeField"];64 auto value = lua["SomeField"]; 65 65 Stdout(value.typeName)(": ")(value.toString).newline; 66 66 }}} 67 This would print the type of the object, and its value as a string. 68 69 As a random case, say we wanted to erase our global variable if it was a function: 70 {{{ 71 #!d 72 auto value = LuaObject in lua["SomeField"]; 73 if(value.isFunction) 74 lua["SomeField"] = nil; 75 }}} 67 This would print the type of the object and its value. 76 68 77 69 === Type Safety === 78 Very often, you expect your variable to be of a certain type. Having to add checks every time to check the type of the variable would be redundant and require a lot of extra code.79 70 80 The LuaD solution is to expect a certain type when you get it from a table:81 {{{82 #!d83 auto func = LuaFunction in lua["SomeFunction"];84 }}}85 The above errors if the our global isn't a function. Unhandled errors are thrown as exceptions of type LuaErrorException.86 87 A typical scenario: you want to get a function and call it, as a form of callback.88 89 That's simple enough:90 {{{91 #!d92 auto func = LuaFunction in lua["SomeFunction"];93 func();94 }}}95 Calling a LuaFunction object works just like calling any other D function. You can of course, like in Lua, pass any and any amount of arguments to this function:96 {{{97 #!d98 auto print = LuaFunction in lua["print"];99 print("SomeString", 123, ["I'm a table!"]);100 }}}101 102 This is the equivalent of doing the following in Lua:103 {{{104 print("SomeString", 123, {"I'm a table!"})105 }}}106 Pretty easy!107 108 ----109 71 == More on Tables == 110 So far we've only really been working with the global table. Other tables work exactly the same !72 So far we've only really been working with the global table. Other tables work exactly the same: 111 73 {{{ 112 74 #!d 116 78 lua["OurTable"] = table; 117 79 118 // You can simplify this caseas:80 //The above can also be written as: 119 81 120 82 lua["OurTable"] = ["hello,", " world"]; 121 83 }}} 122 Pretty straight-forward so far. Everything in LuaD looks a lot like things do in Lua.123 84 124 85 ----