Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Network Request without Listeners?
Started by rts Apr 27 2018 01:14 PM

1 reply to this topic
network requests http
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

rts

[GLOBAL: userInfoPane.html]
rts
  • Observer

  • 1 posts
  • Corona SDK

Hi,

 

I'm relatively new to Corona and Lua and have been trying to implement some API calls within my game. However, I've been creating multiple network requests and unique listeners for each one but is there an easier way?

 

I estimate there to be about 15 different API calls in my game as the server delivers a fair bit of content. However, having all of these listeners seems a bit tedious and any ways to make this more efficient would be appreciated.

 

Thanks!



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,920 posts
  • Corona SDK

I'm not sure what you're doing exactly, but when I find myself in a situation where I need to make many network requests that do similar things and use a similar listener I do one of two things:

 

1. Write a single homogeneous listener that can be used for all requests

 

2. Write a generator/wrapper that  both generates a custom listener and issues the request.

 

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/04/generatedListener.zip

 

This is an example of the latter:

local baseURL = "https://raw.githubusercontent.com/roaminggamer/RG_FreeStuff/master/AskEd/common/cards/"

local function createCard( x, y, w, h, num )
   local tmp = display.newRect( x, y, w, h )

   local function networkListener( event )
      if ( event.isError ) then
         display.remove( tmp )
       
       elseif ( event.phase == "ended" ) then
         tmp.isVisible = true
         tmp.fill = { type = "image", filename = event.response.filename, baseDir = event.response.baseDirectory }
      end
   end

   local to    = "cardClubs" .. num .. ".png"
   local from  = baseURL .. to   

   local params = {}
   network.download( from, "GET", networkListener, params, to, system.TemporaryDirectory )
end

It is kind a silly example, but it shows how, knowing a little of what is common in the use cases, I can make a wrapper to generate most of the code I would otherwise write by hand over and over.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: network, requests, http