Categories
Lua Tutorials

Lua code for OneSignal notification

OneSignal notification example code that are provided here include languages such as Node.js, C#, Python, PHP, Java, Ruby, Perl…, but there’s no Lua.

So here it is, the Lua version for Corona SDK (including a listener):

local json = require "json"

local function listener(e) 
	if e.isError then 
		print("Network Error: ", e.response) 
	else 
		print("Response: ", e.response) 
	end 
end 

local headers = { ["Content-Type"] = "application/json;charset=utf-8", ["Authorization"] = "Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj" } 
local body = { ["app_id"] = "5eb5a37e-b458-11e3-ac11-000c2940e62c", ["included_segments"] = "All", ["data"] = {["foo"] = "bar"}, ["contents"] = {["en"] = "English Message"} } 

network.request("https://onesignal.com/api/v1/notifications", "POST", listener, { headers=headers, body=json.encode(body) })
Categories
Corona SDK Lua Tutorials

UI data binding for Corona SDK

Here is a simple demo of data-binding to user-interface elements in Corona SDK. The purpose is to update variables so that UI elements (textfields in this demo) are updated automatically.

-- UI data binding demo for Corona SDK
--
-- Update variables and UI elements (textfields in this demo) are updated automatically
--
-- Created by:  Dave Yang / Quantumwave Interactive Inc.
-- Version:     1.00

-- place variables inside UIdata
local UIdata = {
    sum = 0,
    rand = -1,
}

-- create the UI elements
local sumTxt = display.newText( UIdata.sum, 150, 125, native.systemFont, 24 )
local randTxt = display.newText( UIdata.rand, 150, 250, native.systemFont, 24 )

-- bind the data to the UI elements
local UIdataBinding = {
    sum = sumTxt,
    rand = randTxt,
}

---------------------------------------------------------------------------

-- reference to the original UIdata table
local _uidata = UIdata

-- empty UIdata to make it work with the metatable below
UIdata = {}

-- the metatable defines what to do with access and change to data in UIdata
local UIdataMT = {
    __index = function(t,k)
        return _uidata[k]
    end,

    __newindex = function(t,k,v)
        -- changes to the data is updated in the textfield; other UI elements can be applied according to type
        UIdataBinding[tostring(k)].text = v
        -- update the original data
        _uidata[k] = v
    end
}

setmetatable(UIdata, UIdataMT)

-- single line if you prefer to replace the lines above starting from 'local UIdataMT = {'
--setmetatable(UIdata, {__index=function(t,k) return _uidata[k] end, __newindex=function(t,k,v) UIdataBinding[tostring(k)].text=v;_uidata[k]=v end})

---------------------------------------------------------------------------

-- change and access data

UIdata.sum = 42
print(UIdata.sum)

UIdata.rand = math.random(99)
print(UIdata.rand)

The latest updates can be found at github.

Categories
Corona SDK Lua OOP Tutorials

Simple demo of implementing Undo and Redo in Corona / Lua

Just posted a simple demo of HistoryStack, to show how Undo and Redo can be implemented in Corona SDK / Lua. It also demonstrates using EventDispatcher to dispatch events to update button states.

https://github.com/daveyang/HistoryStack

 

Categories
Others

This site was hacked

Thanks to hackers from around the world, this site was hacked and code was injected into the blogging system. Spams were sent from my server and I finally found the source of the problem.

As a result, this site was taken down and the blogging system was reinstalled from scratch with a couple of security measures to prevent the same hacks. Some external files (graphics, images, external scripts…etc.) are missing for the time being.

Categories
Others

It’s all about solving problems

Having been a software designer and developer, what I’ve been doing is really just problem solving. For more than 2 decades, I’ve been solving problems for different clients on various types of projects – from live multimedia presentations, digital mixed media software, off-the-shelve games, computer-assisted learning, last-minute project rescue, enterprise apps, sales apps, educational apps, to native device apps… and more.

In the last couple of years, I’ve decided to spend more time on solving my own problems for my own projects. And it’s been even harder than solving problems for clients.

The main reason it’s harder is because I’m my worst critic. Or I’ve set my standard too high after solving problems for clients for so long. I want to do the best work – my own work, and most results are simply “not good enough”.

As I look at various apps on different app stores, I know I’m being too harsh on myself. But how do I solve this problem?

Categories
Corona SDK Lua News

ZeroBrane Studio updated to 0.90

The excellent ZeroBrane Studio for Lua development has been updated to 0.90, download it here or check out the source code at github.

This open source and free IDE is the best I’ve used for Lua / Corona SDK development. Support from the developer is also quick and excellent. What else can one ask for?

Update: Version 0.95 has been released.
Update: Version 1.00 has been released!

P.S. Check out this blog post for adding Corona SDK show reference.

Categories
Corona SDK Lua OOP

EventDispatcher docs

Documentation has been added for EventDispatcher in LDoc format, generated from the source file.

Here’s the basic usage:

local EvtD = require "EventDispatcher"

local dispatcher = EvtD()

-- listener as table
local listener = {
   eventName = function(event, ...)
      print(event.name, event.target, event.source)
   end
}

-- listener as function
local function listener(event, ...)
    print(event.name, event.target, event.source)
end

dispatcher:addEventListener( "eventName", listener ) -- or
dispatcher:on( "eventName", listener )

dispatcher:once( "eventName", listener )

dispatcher:hasEventListener( "eventName", listener )

dispatcher:dispatchEvent( { name="eventName" } ) -- or
dispatcher:dispatchEvent( "eventName" ) -- or
dispatcher:emit( { name="eventName" } ) -- or
dispatcher:emit( "eventName" )

dispatcher:removeEventListener( "eventName", listener )

dispatcher:removeAllListeners( "eventName" ) -- or
dispatcher:removeAllListeners()

dispatcher:printListeners()

All listeners receive the following fields in the parameter event table:

  • event.name (name of the event)
  • event.target (the listener itself)
  • event.source (the dispatcher)

Grab the latest code at Github.

Categories
Corona SDK Lua OOP

EventDispatcher update

Just a quick post that EventDispatcher has been updated with new features including new methods on(), emit(), once() that are similar to Node.js, a new method removeAllListeners(), and a debug function printListeners(). The on() and emit() methods are actually aliases to the addEventListener() and dispatchEvent() methods.

Chris Byerley (aka develephant) who developed Coronium.io and Coronium.gs is using EventDispatcher under the hood for the game server client code. Check out his projects if you’re a Lua / Corona developer.

Here’s the original post for EventDispatcher if you wish to read about it.

Grab the latest code at Github.

Categories
Corona SDK Lua Tips & Tricks

ZeroBrane Studio

For Lua development, whether it is for plain Lua or platforms such as Corona SDK, ZeroBrane Studio is an excellent open-source IDE (written in Lua) for Windows, Mac OS and Linux. I still use Sublime Text with the Corona Editor plugin but ZeroBrane Studio is my first choice for Lua development.

This IDE is updated frequently and has great support from Paul Kulchenko. To grab the latest update before a new version is released, download from the github page, and follow these steps on Mac OS:

  1. Locate ZeroBraneStudio.app in the Applications folder
  2. Right-click on the app and choose Show Package Contents
  3. Open the Contents folder, and then the ZeroBraneStudio folder
  4. Drag and drop everything(*) inside the (github) ZeroBraneStudio folder into the above folder inside the app

* Actually not all files are needed, see an example of files and folders to drag over in the following image:

 

If you have made changes to a theme or configuration (such as tomorrow.lua in the cfg folder), make sure you have a backup copy before replacing everything as described above.

Download the IDE here: http://studio.zerobrane.com

To show Corona SDK reference from right-clicking on a keyword in the editor, add the following code to the end of your user settings (user.lua) found under the Edit | Preferences menu:

showreference = {
	target = 'http://docs.coronalabs.com/api/%s.html',
	transform = function(s)
		local tip = G.GetTipInfo(G.ide:GetEditor(), s)
		if tip then s = tip:match("%)%s*(%S+)") or s end
		s = (G.type(G[s]) == "function" and "global." or "")..s
		s = s..(s:find("[%.%:]") and "" or ".index")
		s = s:find("^_") and "type."..s:sub(2) or "library."..s
		return(s:gsub("[%.%:]","/"))
	end,
}

Add showreference.lua from ZeroBranePackage to the ~/.zbstudio folder if it’s not there already. Quit and relaunch the IDE to see the update.

Categories
Others

DigitalOcean VPS $10 credit

If you’ve been thinking of having your own virtual private server, here’s a referral link to get $10 credit from DigitalOcean. While I won’t be getting any referral credit until $25 has been spent by you, it’s still a good deal to check it out with the two months credit (if you sign up for the $5/month plan).