Starters Core
This page is both a reference to the 'starters core' package as well as a reference for individual libraries from the package that are also included in other starters.
This package includes these elements:
- ButtonMaker - A module for easily making push, toggle, and radio buttons.
- Easy Ads - A module to simplify the use of the Appodeal and AppLovin ad plugins.
- Easy IAP - A module that layers on top of IAP Badger to make it even easier to set up In App Purchases.
- Globals - Useful global varaibles.
- Persistent Data - Easy and efficient persistent data module.
- Sound Manager - A basic sound management module.
- Utilities - A module containing a number of useful utility functions.
globals.lua
All starter kits/packages come with the module file easy/globals.lua
.
Contrary to some folks' beliefs, global variables are not evil. In fact, they would not be possible if they were not useful and sometimes needed.
This module sets some useful globals that help save you time typing common values and figuring out commonly needed things:
w
- Shorthand fordisplay.contentWidth
h
- Shorthand fordisplay.contentHeight
centerX
- Shorthand fordisplay.centerX
centerY
- Shorthand fordisplay.centerY
fullw
- Shorthand fordisplay.actualContentWidth
fullh
- Shorthand fordisplay.actualContentHeight
unusedWidth
-display.actualContentWidth - display.contentWidth
unusedHeight
-display.actualContentHeight - display.contentHeight
left
- Position of left-edge of visible content.top
- Position of top-edge of visible content.right
- Position of right-edge of visible content.bottom
- Position of bottom-edge of visible content.
persist.lua
The starter core includes a special module that makes it easy and efficient to maintain persistent data in your games. This module can be found in the file easy/persist.lua
This is directly adopted from SSK2, but the 'secure' save/load feature has been removed.
Setting Default Values
The persist
library lets you set 'default' values for any named field. This way, if you call get() for a named field before you set() it, you will get a valid value.
To set a defaut value for a field in a persistent data set, use this function:
persist.setDefault( fileName, fieldName, value [ , params ] )
fileName
- Name of file to save persistent data in.fieldName
- Name of setting to set default for.value
- Value to set as default forfieldName
.params
({}
) - Optional table of parameters.save
(true
) - Save immediately to file.base
(system.DocumentsDirectory
) - Base folder type for save file.
Setting and Getting Values
Use these functions set and get values respectively:
persist.set( fileName, fieldName, value [ , params ] )
persist.get( fileName, fieldName [ , params ] )
fileName
- Name of file to save persistent data in.fieldName
- Name of setting to set default for.value
- Value to set as default forfieldName
.params
({}
) - Optional table of parameters.save
(true
) - Save immediately to file.base
(system.DocumentsDirectory
) - Base folder type for save file.
Multiple Sets Allowed?
As you may have determined already, this module supports multiple separate data sets. Simply use different names for the fileName
setting and your data will be separated into independent sets. (See Complete Example below.)
Data Caching
This library utilizes caching to speed up data accesses.
Whenever a persistent data set is mofified the following checks occur:
- Is data cached?
- If not, load it from file or create blank record.
- Cache data in persist module.
- Is there a value for the field user is accessing?
- If not, and if there is a default set,
- Set field in cached copy to default
- Return default value.
- If not, and if there is a default set,
- Did user set
save
parameter tofalse
?- If not, save cached data immediately
I will be modifying this slightly in the future to handle back to back set/setDefault calls with save set to true. This modification will prevent these saves from blocking and affecting frame rate.
Complete Persist Example
The following example shows a example usage with automatic secure mode enabling when on device.
-- Set up two data sets with defaults
--
persist.setDefault( "store.json", "disabled_ads", false , { save = false } )
persist.setDefault( "store.json", "bought_extra_content", false )
persist.setDefault( "settings.json", "sound_enabled", true, { save = false } )
persist.setDefault( "settings.json", "last_score", 0 )
-- Check persistent data fields
print( "Ads disabled? ", persist.get( "store.json", "disabled_ads") )
print( "Last Score: ", persist.get( "settings.json", "last_score" ) )
-- Set 'last score' to a random value
persist.set( "settings.json", "last_score", math.random(100, 10000) )
-- Check persistent data fields again
print( "Ads disabled? ", persist.get( "store.json", "disabled_ads") )
print( "Last Score: ", persist.get( "settings.json", "last_score" ) )
soundMgr.lua
All starter kits/packages come with the module file easy/soundMgr.lua
.
This is a very basic sound library that takes the drudgery (and likelihood of error) out of playing sounds in your game.
While it works fine as is, this module is meant to give you a starting point from which to expand and improve.
Sound Categories
- Effect - An effect is a short sound. You may play these in rapid succession and even overlap them. These are usually .wav files.
- Music - A long sound, usually played as the soundtrack in a game, or as a voice-over. These are usually stored as .mp3 files.
Basic Usage
local soundMgr = require "easy.soundMgr"
Adding Sounds
addEffect( name, src )
soundMgr.addEffect( "click", "sounds/sfx/click.wav" )
addMusic( name, src )
soundMgr.addMusic( "soundTrack", "sounds/music/Blipotron.mp3" )
Playing Sounds
playEffect( name, options )
options
- You can pass any of the options supported by (audio.play)(https://docs.coronalabs.com/api/library/audio/play.html#parameter-reference).
soundMgr.playEffect( "click" )
playMusic( name, options )
options
- You can pass any of the options supported by (audio.play)(https://docs.coronalabs.com/api/library/audio/play.html#parameter-reference).
soundMgr.playMusic( "soundTrack", { fadein = 5000 } )
Stopping Sounds
This module will find and stop all instances of a named sound.
stopEffect( name )
soundMgr.stopEffect( "click" )
stopMusic( name )
soundMgr.stopMusic( "soundTrack" )
Removing Sounds
These featuers automatically stop and remove and then unload all instances of a named sound.
removeEffect( name )
soundMgr.removeEffect( "click" )
removeMusic( name )
soundMgr.removeMusic( "soundTrack" )
utils.lua
All starter kits/packages come with the module file easy/utils.lua
.
This is a terse and alphabetical quick reference to the functions found in easy.utils.lua
and easy.files.*
Remember: You've got the source so if there is any confusion about using these, you can dig into the code. :)
autoIgnore( name, obj )
- Checks if obj is still valid and if not, removes named listener. Returns 'true' if object was invalid and listener was removed.createSlicedImage( group, path, x, y, width, height )
- Draws a rectangle from 9 sub-slices.path
- Folder containing 9 slice images: slice_1.png .. slice_9.png.
deepCopy( src, dst )
- Copies multi-level tables; handles non-integer indexes; does not copy metatabledump( theTable, padding, marker )
- Table dumper.files.*
- Grouped by category:files.documents.getRoot()
files.documents.getPath( path )
files.resource.getRoot()
files.resource.getPath( path )
files.temporary.getRoot()
files.temporary.getPath( path )
files.desktop.getDesktopRoot()
files.desktop.getDesktopPath( path )
files.desktop.getMyDocumentsRoot()
files.desktop.getMyDocumentsPath( path )
files.desktop.getDrivePath( path ) by current OS.
files.desktop.explore( path )
files.util.exists( path )
files.util.dumpAttributes()
files.util.isFile( path )
files.util.readFile( path )
files.util.readFileToTable( path )
files.util.writeFile( data, path )
files.util.appendFile( data, path )
files.util.rmFile( path )
files.util.mvFile( src, dst )
files.util.cpFile( src, dst )
files.util.isFolder( path )
files.util.repairPath( path, forceForward )
files.util.rmFolder( path )
files.util.mkFolder( path )
files.util.mvFolder( src, dst )
files.util.cpFolder( src, dst )
files.util.saveTable( tbl, path, secure )
files.util.loadTable( path, secure )
files.util.getFilesInFolder()
files.util.keepFileTypes()
files.util.getLuaFiles()
files.util.getResourceFiles()
files.util.flattenNames()
vutil.findAllFiles()
first_upper(str)
- Make first letter in string 'upper-case'.fnn( ... )
- Return first argument passed that does not evaluate tonil
.generateButtonPresets( using2XImages )
- This utility will generate button prese for any images found in images/buttons. Passtrue
as first argument if your images are twice as large as you want them displayed.hexcolor( code )
- Converts hex color codes to rgba Graphics 2.0 table.isValid( obj )
- Returns 'true' if obj is still a valid display object.ignore( name, listener )
- Shorthand for Runtime:removeEventListener( name, listener ).ignoreList( list, obj )
- Safe Runtime listener remover similar to 'ignore' (above), but takes list (table of strings) for each listener name to remove.isInBounds( obj, obj2 )
- Returns true if obj is in bounds of obj2listen( name, listener )
- Shorthand for Runtime:addEventListener( name, listener ).load( fileName, base )
- Loads table from file. (Uses JSON library as intermediary)onDesktop()
- Returns true if on a desktoponDevice()
- Returns true if on a deviceos()
- Returns: 'ios', 'android', 'tvos', win, osx, or nil if OS is unknown.post( name, params )
- Shorthand helper that does job of Runtime:dispatchEvent( event ).print_r( t )
- Dumps indexes and values inside multi-level table (for debug).getImageSize = ( path, basePath )
- Returns width, and height of image at path.repairPath( path )
- Force path to use forward slashes for consistency in later parsing.round(val, n)
- Rounds floating point-values to n places.rpad( str, len, char)
- Places padding on right side of a string, such that the new string is at least len characters long.save( theTable, fileName, base )
- Saves table to file. (Uses JSON library as intermediary).split(str,tok)
- Splits a string on specified token and returns table of values.shallowCopy( src, dst )
- Copies single-level tables; handles non-integer indexes; does not copy metatableshuffle( t, iter )
- Randomizes the order of a numerically indexed (non-sparse) table.triml(s)
- Trim whitespace from left-side of string only.trimr(s)
- Trim whitespace from right-side of string only.trim(s)
- Trim whitespace.utils.onSim(noOs)
- returns: onSim, os- onSim - 'true' if on simulator
- os - nil if not on simulator, 'win' or 'osx' otherwise.
version( )
- Print and return current version number ofutils.*
module in YYMMDD format.
Copyright © Roaming Gamer, LLC. 2008-2018; All Rights Reserved