Code is too long for turning pages

I’m fairly new to corona. I’ve been playing around with it for about 2 months now. I’m trying to create a text adventure game but the code I’m using makes it so that I have to make a different function for each page. I wanted to know if there’s a way to implement page turning with a table or list.

Yes there is.

Look at the code you’ve been writing.  Identify the commonalities.  Implement code to take advantage of those commonalities.

You can use tables to store the data and smart programming to handle the progression.

By the way, this is a natural process.  You can’t expect to get it right the first time.  It is very normal to first approach things long hand only to later realize you could have written 1% of the code you did initially.

Eventually, you’ll move to a data driven approach, but the only way to get there is through hard earned learning.

Here is an example:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/06/basicTextAdventure.zip

https://www.youtube.com/watch?v=y9QzQyOnAIg&feature=youtu.be

Excluding the data definitions for the 100 (generated) pages, the active code for this example was about 60 lines:

local public = {} function public.run( numPages ) -- load all data local pages = {} for i = 1, numPages do pages[i] = require( "pages.page" .. i ) end --table.dump(pages) -- forward declare function names and locals local showPage local group -- define page showing function showPage = function( num ) -- Delete old group and prepare new one display.remove(group) group = display.newGroup() -- Get page data local page = pages[num] table.dump(page) -- Draw title and text content local options = { text = page.title .. "\n\n" .. page.content, x = left + 20, y = top, width = fullw - 40, font = native.systemFont, fontSize = 32, } local pageText = display.newText( options ) group:insert(pageText) pageText.anchorX = 0 pageText.anchorY = 0 -- Draw choice buttons local bw = 160 local bh = 40 local curY = pageText.y + pageText.contentHeight + 2 \* bh local curX = centerX - (#page.choices\*bw)/2 + bw/2 for i = 1, #page.choices do local choice = page.choices[i] local buttonText = choice[1] local toPage = choice[2] easyIFC:presetPush( group, "default", curX, curY, bw-20, bh, buttonText, function() --print(toPage) showPage( tonumber(toPage) ) end ) curX = curX + bw end end showPage(1) end return public

Warning: My example uses SSK for the buttons.  You could also do this with your own custom button maker or the widget.* lib.

The important bit is the data files in pages/ and the draw code.

Also, my generating code is kind of advanced and also uses SSK, but you don’t need it.

It keeps saying

module ‘pages.page1’ not found:…

In the main.lua however, I wrote

…require “widget”… instead of …require “ssk2.loadssk”…

  1. What?  I didn’t mean you could simply replace ssk2 with widget by requiring widget instead of ssk.  

I meant you could replace my button code in the example with widget code of your own, but I won’t do that because I don’t use widget code.  

This was just an example to show one easy way to make a basic text story with page changing buttons.

  1. The example as downloaded works fine.  You must have changed something.  Don’t cut-copy-from the forums. 

A. Download the example and run it without changes.  Then examine: main.lua, draw.lua, and the files in pages/.

B. Once you get the idea write you own code.  Don’t use mine and try to Frankenstein it into a game.  This is a demo of the concept.  You’ll learn more if you start from scratch.

Oh ok, I get it now. Thanks

Yes there is.

Look at the code you’ve been writing.  Identify the commonalities.  Implement code to take advantage of those commonalities.

You can use tables to store the data and smart programming to handle the progression.

By the way, this is a natural process.  You can’t expect to get it right the first time.  It is very normal to first approach things long hand only to later realize you could have written 1% of the code you did initially.

Eventually, you’ll move to a data driven approach, but the only way to get there is through hard earned learning.

Here is an example:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/06/basicTextAdventure.zip

https://www.youtube.com/watch?v=y9QzQyOnAIg&feature=youtu.be

Excluding the data definitions for the 100 (generated) pages, the active code for this example was about 60 lines:

local public = {} function public.run( numPages ) -- load all data local pages = {} for i = 1, numPages do pages[i] = require( "pages.page" .. i ) end --table.dump(pages) -- forward declare function names and locals local showPage local group -- define page showing function showPage = function( num ) -- Delete old group and prepare new one display.remove(group) group = display.newGroup() -- Get page data local page = pages[num] table.dump(page) -- Draw title and text content local options = { text = page.title .. "\n\n" .. page.content, x = left + 20, y = top, width = fullw - 40, font = native.systemFont, fontSize = 32, } local pageText = display.newText( options ) group:insert(pageText) pageText.anchorX = 0 pageText.anchorY = 0 -- Draw choice buttons local bw = 160 local bh = 40 local curY = pageText.y + pageText.contentHeight + 2 \* bh local curX = centerX - (#page.choices\*bw)/2 + bw/2 for i = 1, #page.choices do local choice = page.choices[i] local buttonText = choice[1] local toPage = choice[2] easyIFC:presetPush( group, "default", curX, curY, bw-20, bh, buttonText, function() --print(toPage) showPage( tonumber(toPage) ) end ) curX = curX + bw end end showPage(1) end return public

Warning: My example uses SSK for the buttons.  You could also do this with your own custom button maker or the widget.* lib.

The important bit is the data files in pages/ and the draw code.

Also, my generating code is kind of advanced and also uses SSK, but you don’t need it.

It keeps saying

module ‘pages.page1’ not found:…

In the main.lua however, I wrote

…require “widget”… instead of …require “ssk2.loadssk”…

  1. What?  I didn’t mean you could simply replace ssk2 with widget by requiring widget instead of ssk.  

I meant you could replace my button code in the example with widget code of your own, but I won’t do that because I don’t use widget code.  

This was just an example to show one easy way to make a basic text story with page changing buttons.

  1. The example as downloaded works fine.  You must have changed something.  Don’t cut-copy-from the forums. 

A. Download the example and run it without changes.  Then examine: main.lua, draw.lua, and the files in pages/.

B. Once you get the idea write you own code.  Don’t use mine and try to Frankenstein it into a game.  This is a demo of the concept.  You’ll learn more if you start from scratch.

Oh ok, I get it now. Thanks