stendhal/doc/mkdocs/lua/objects/luajava.md

62 lines
1.3 KiB
Markdown
Raw Permalink Normal View History

2022-01-20 15:54:06 -08:00
2023-05-16 20:13:17 -07:00
---
# Contents
2022-01-20 15:54:06 -08:00
[TOC]
2023-05-16 20:13:17 -07:00
2023-04-21 02:52:49 -07:00
---
2023-05-16 20:13:17 -07:00
# Introduction
2022-01-20 15:54:06 -08:00
2023-04-21 02:52:49 -07:00
Object instance: `luajava`
2023-05-16 20:13:17 -07:00
2023-04-21 02:52:49 -07:00
---
2023-05-16 20:13:17 -07:00
## Description
2023-04-21 02:52:49 -07:00
`luajava` is an object of the [LuajavaLib library][home.luaj]. It can be used to coerce Java static
objects to Lua or create new Java object instances.
2023-05-16 20:13:17 -07:00
2023-04-21 02:52:49 -07:00
---
2023-05-16 20:13:17 -07:00
# Examples
2023-04-21 02:52:49 -07:00
Example of exposing a static object &enums to Lua:
2022-01-20 15:54:06 -08:00
2023-05-16 20:13:17 -07:00
```lua
2022-01-20 15:54:06 -08:00
-- store a Java enum in a Lua global variable
ConversationStates = luajava.bindClass("games.stendhal.server.entity.npc.ConversationStates")
-- access the enum values like so
ConversationStates.IDLE
```
Example of creating an object instance:
2023-04-21 02:52:49 -07:00
2023-05-16 20:13:17 -07:00
```lua
2022-01-20 15:54:06 -08:00
-- store instance in local variable
local dog = luajava.newInstance("games.stendhal.server.entity.npc.SilentNPC")
-- access object methods like so
dog:setEntityClass("animal/puppy")
dog:setPosition(2, 5)
-- class with constructor using parameters
local speaker = luajava.newInstance("games.stendhal.server.entity.npc.SpeakerNPC", "Frank")
speaker:setOutfit("body=0,head=0,eyes=0,hair=5,dress=5")
speaker:setPosition(2, 6)
```
2023-05-16 20:13:17 -07:00
2023-04-21 02:52:49 -07:00
---
2023-05-16 20:13:17 -07:00
# Synopsis
2023-04-21 02:52:49 -07:00
To make scripting easier, Stendhal employs a [master script][init.lua] & some
[helper objects & methods][objects] to handle the functionality mentioned above.
[home.luaj]: http://luaj.org/luaj.html
[init.lua]: https://github.com/arianne/stendhal/blob/master/src/games/stendhal/server/core/scripting/lua/init.lua
[objects]: /reference/lua/objects