I’m using Twine 2 to create a ‘choose your own adventure’ type story, and I’m outputting the text and structure as a JSON file. I’m reading/decoding the JSON file and then reading from the Lua table (I think, I might be mistaken) with each page render to get the text.
The issue I find is that the pause before rendering is noticeable (in the simulator) and the scrollview is sluggish compared to getting text from a Lua table. Can anyone advise how I can improve this? How would I go about copying out relevant data from the JSON file to a Lua table (if that would lead to better performance)? Any tips would be greatly appreciated.
I’m not sure if ‘t’ below is a Lua table, or why using that would lead to slower performance.
Here’s an excerpt of what i have:
[lua]
local json = require “json”
–Read/decode the json file
local jsonFile = function( filename, base )
if not base then base = system.ResourceDirectory; end
local path = system.pathForFile( filename, base )
local contents
local file = io.open( path, “r” )
if file then
contents = file:read( “*a” )
io.close( file )
end
return contents
end
– Create Lua table from JSON file?
local t = json.decode( jsonFile( “data2.json” ) )
– An example of accessing a data value from t
print (t[“passages”][2][‘text’])
[/lua]
Once I have access to the JSON text values I’m rendering them inside a scrollview:
[lua]
local group = display.newGroup()
local scrollView = widget.newScrollView
{
top = display.screenOriginY,
left = 0,
width = display.actualContentWidth,
height = display.actualContentHeight,
scrollWidth = display.actualContentWidth,
scrollHeight = 8000,
backgroundColor = { 0.05, 0.05, 0.05 },
--hideBackground = true,
horizontalScrollDisabled = true,
friction = 0.95,
hideScrollBar = true,
bottomPadding = 30
}
local options =
{
text = (t[“passages”][#num][“text”]),
width = display.actualContentWidth-30,
font = native.newFont( “Paciencia-Regular”, 32 ),
fontSize = 15,
align = “left”,
parent = group
}
local paragraph = display.newText( options )
paragraph.anchorX = 0.5
paragraph.anchorY = 0
paragraph.x = display.actualContentWidth/2
paragraph.y = 380
paragraph:setFillColor( 0.95, 0.95, 0.95 )
scrollView:insert( paragraph )
[/lua]