Documentation
¶
Overview ¶
Code generated by gobindlua; DO NOT EDIT.
Example ¶
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/ChrisTrenkamp/gobindlua"
lua "github.com/yuin/gopher-lua"
)
const script = `
local primitive_struct = require "primitive_struct"
--[[ You can call the constructor functions and methods and access the fields from Lua. ]]
data = primitive_struct.new()
data.my_bool = true
data.my_int = 42
data.my_int64 = 0xDEADBEEF
data.my_float = 3.14
data.my_string = "all your lua are belong to us"
print("MyBool: " .. tostring(data.my_bool))
print("MyInt: " .. tostring(data.my_int))
print("MyInt64: " .. tostring(data.my_int64))
print("MyFloat: " .. tostring(data.my_float))
print("MyString: " .. tostring(data.my_string))
print("WillBeExcluded has been excluded: " .. tostring(data.will_be_excluded == nil))
data:set_specialized_int(9001)
print("MySpecializedInt: " .. tostring(data.my_specialized_int))
print("DivideMyInt: " .. tostring(data:divide_my_int(2)))
local _, err = pcall(function () data:divide_my_int(0) end)
print("DivideMyInt error: " .. err)
print("ExcludedMethod has been excluded: " .. tostring(data.excluded_method == nil))
`
func main() {
L := lua.NewState()
defer L.Close()
var registrarPointer *PrimitiveStruct
gobindlua.Register(L, registrarPointer)
// For ease of use, you can pass in &PrimitiveStruct{} to the Register function as well.
// The Register function does not require the pointer to contain a value. It's simply
// for registering the metadata tables.
if err := L.DoString(script); err != nil {
log.Fatal(err)
}
data := L.GetGlobal("data").(*lua.LUserData).Value.(*PrimitiveStruct)
jsonBytes, err := json.MarshalIndent(data, "", "\t")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(jsonBytes))
}
Output: MyBool: true MyInt: 42 MyInt64: 3735928559 MyFloat: 3.14 MyString: all your lua are belong to us WillBeExcluded has been excluded: true MySpecializedInt: 9001 DivideMyInt: 21 DivideMyInt error: <string>:23: divide by zero error ExcludedMethod has been excluded: true { "MyBool": true, "MyInt": 42, "MyInt64": 3735928559, "MyFloat": 3.14, "SomeString": "all your lua are belong to us", "WillBeExcluded": "", "MySpecializedInt": 9001 }
Index ¶
- type PrimitiveStruct
- func (p PrimitiveStruct) DivideMyInt(divisor float64) (float64, error)
- func (p PrimitiveStruct) ExcludedMethod()
- func (r *PrimitiveStruct) LuaMetatableType() string
- func (goType *PrimitiveStruct) LuaModuleLoader(L *lua.LState) int
- func (goType *PrimitiveStruct) LuaModuleName() string
- func (goType *PrimitiveStruct) LuaRegisterGlobalMetatable(L *lua.LState)
- func (p *PrimitiveStruct) SetSpecializedInt(i SpecializedInt)
- type SpecializedInt
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PrimitiveStruct ¶
type PrimitiveStruct struct {
// All exported fields will have bindings created for GopherLua
MyBool bool
MyInt int32
MyInt64 int64
// Notice how `lua_PrimitiveStruct.go` will properly cast the float64 to this named type
MyFloat primitivesubpackage.SomeFloat64
// You can use tags to explicitly declare the Lua name of the field.
SomeString string `gobindlua:"my_string"`
// You can also exclude fields
WillBeExcluded string `gobindlua:"-"`
MySpecializedInt SpecializedInt
}
func NewPrimitiveStruct ¶
func NewPrimitiveStruct() PrimitiveStruct
Use the gobindlua:constructor directive to declare a function as a constructor in the Lua bindings. If there's multiple return values, the first value MUST be the struct you're creating bindings for. The return type can be a pointer as well. If constructors have a name that matches New(StructName)[OptionalQualifier], they will be added as a metatable field in the form of new[_optional_qualifier]. Otherwise, the function will be added with the original name, but in snake_case form. This function will be added as "new".
func (PrimitiveStruct) DivideMyInt ¶
func (p PrimitiveStruct) DivideMyInt(divisor float64) (float64, error)
Use the gobindlua:function directive to bind a method in Lua.
func (PrimitiveStruct) ExcludedMethod ¶ added in v0.0.4
func (p PrimitiveStruct) ExcludedMethod()
There's no gobindlua directive, so this method has been excluded.
func (*PrimitiveStruct) LuaMetatableType ¶
func (r *PrimitiveStruct) LuaMetatableType() string
func (*PrimitiveStruct) LuaModuleLoader ¶ added in v0.0.9
func (goType *PrimitiveStruct) LuaModuleLoader(L *lua.LState) int
func (*PrimitiveStruct) LuaModuleName ¶ added in v0.0.9
func (goType *PrimitiveStruct) LuaModuleName() string
func (*PrimitiveStruct) LuaRegisterGlobalMetatable ¶ added in v0.0.9
func (goType *PrimitiveStruct) LuaRegisterGlobalMetatable(L *lua.LState)
func (*PrimitiveStruct) SetSpecializedInt ¶ added in v0.0.5
func (p *PrimitiveStruct) SetSpecializedInt(i SpecializedInt)
type SpecializedInt ¶ added in v0.0.5
type SpecializedInt uint32