Skip to content

Polycode User's FAQ

fodinabor edited this page Mar 10, 2015 · 6 revisions

How to combine C++ and Lua?

Basically by making a custom version of the Polycode Player or if you want to write the main application in Lua write a Polycode module and use it from Lua.

How to get just something show up on the screen?

Lua (Note: You need to make sure you have a font loaded for this to work; the IDE/Player will load one automatically):

scene = Scene(Scene.SCENE_2D)
scene:getActiveCamera():setOrthoSize(640, 480)
label = SceneLabel("Hello from Lua!", 32)
label:setPosition(-50, -50, 0)
scene:addChild(label)

I get a segfault in Lua.

One likely cause is that you add something to the scene or some other entity, but don't store a reference to it in a persistent location. The asiest way to get started is to just store entities in global variables, or make a global table for them.

How to get click events on buttons?

You have to set processInputEvents = true for each entity from the root to the top.

Lua: Start with scene.rootEntity.processInputEvents = true, then do something like:

button = UIButton("Button", 100, 50)
button:setPosition(100, -100)
scene:addEntity(button)
class "Thing" (EventHandler)
function Thing:Thing()
    EventHandler.EventHandler(self)
end
function Thing:on_click_event(e)
    print("Thing:on_click_event()")
end
thing = Thing()
button:addEventListener(thing, Thing.on_click_event, UIEvent.CLICK_EVENT)

My UI theme is screwed up with parts of it upside down; WTF?

There is a thing called Scene.SCENE_2D_TOPLEFT that you should use with UI.

How to set font size for UI elements?

Lua, for buttons: CoreServices.getInstance():getConfig():setNumericValue("Polycode", "uiButtonFontSize", 30)

In most cases it is probably best practice to use the .xml configuration file for these; see eg. UIThemes/dark/theme.xml. You can load those with CoreServices.getInstance():getConfig():loadConfig("Polycode", "path/to/Assets/UIThemes/dark/theme.xml").

Also, there are references to image files in theme.xml. You can set the base path for those this way: CoreServices.getInstance():getResourceManager():addArchive("path/to/Assets");