Discord
Login
Community
DARK THEME

Download the asset before downloading the game

Can I download the asset before downloading the game?

I need to load it at the same time as the sprites so that when I run the init() function, it is already fully loaded

When you start the game, all resources are downloaded first, i.e. graphics, maps, music.

The init section should not start without all this (although sometimes when I test what other users have done, the game can start without music, then I have to do F5).

You can download assets in the init section

download_assets = function( list )
   for item in list 
     local image = asset_manager.loadImage( item, function() end )
      while not image.ready 
      end
      print("pass " + image.ready)
   end
end


init = function()  
  list = ["bu", "mu"]
  download_assets( list )
end

I understand this solution, but in this case the file (if it is large enough) is loaded together with the update() function.

I want to load a CSV file with the localization of the game. If it is not loaded when init() is run, the console is filled with errors, since text strings are missing in all functions of the game.

I came up with this solution: in init(), assign an empty list [[], [], []] to the localization variable. Text strings will be empty when the game first starts working.

In update(), I will replace localization with the processed CSV file after it has been loaded. Text strings will be displayed as expected.

In most cases, this will take less than a second and should be acceptable to the user.

However, this is a "cheat", I would like to load the CSV before init() is run. I will wait for this in future versions of Microstudio. Maybe there is a way to add asset loading by changing the files of the desktop version of Microstudio, but this is too complicated for me.

asset_manager in the load function works as if it created a new thread and this thread was responsible for downloading data.

asset_menager will return to you the data that it downloaded in the function that you provided as callback. It is similar to promises in javascript.

How the code I provided works.

In the init section there is a callable function download_assets that receives a list of file names (images to download).

The download_asset function performs downloading for each element of the list to a variable named image (I changed it to asset). Since this function is executed in a different thread, you have to wait until the download is complete. That is why there is a loop waiting for the image.ready field to have the value true. Then you know that the given asset has been downloaded. (You can also order multiple downloads at once, but then you have to receive this data in the callblack function - I made this function empty, in this function you can also receive errors if they occur)

The update and draw functions are not called until the code from the init section has finished.

Here is a slightly improved code to make it more readable. See that the update function is not called until all the code from init has been executed.

Add a csv file to the assets tab (MicroStudio supports this format).

I added a second function that should make your work easier and help you understand how it works.


download_assets = function( list )
  local result = []
  for item in list 
      local asset = asset_manager.loadImage( item, function() end )
      while not asset.ready
      end
      print("image pass " + asset.ready )      
      result.push( asset.image )
  end
  return result
end

download_assets_csv = function( list )
  local result = []
  for item in list 
      local loader = asset_manager.loadCSV( item, function() end )
      while not loader.ready
      end
      print("csv pass " + loader.ready )      
      result.push( loader.text )
  end
  return result
end

init = function()  
  list = ["bu", "mu"]
  l = download_assets( list )
  csv = download_assets_csv( ["example"] )
  print( "init pass ")
end

update = function()
  print( "update")
end

Thanks, this is a really good solution.

But it doesn't work if you create a program in Python. I've been looking at forums for similar problems, and it seems to be a feature of the while function in Python. The whole program just hangs when I use this construction:

while loader.ready == 0: #something

Show your project .

I get the message

"AttributeError: 'Javascript Object' object has no attribute 'load_csv', for asset_manager object.

To me this looks like a bug in the implementation of the asset_manager <> python interface.

Here is an example of a simple application. When the while is running, the application just freezes. This is repeated both on the desktop version and in the browser.


def download_assets_csv(item):
    result = None
    loader = asset_manager.loadCSV(item)
    while loader.ready == 0: pass
    result = loader.text
    return result


def init():
    global my_csv, csv_download
    my_csv = 0
    csv_download = False
    csv = download_assets_csv('en')
    print('init() completed')

def update():
    global my_csv, csv_download
    if not csv_download:
        print(my_csv)
        csv_download = True


def draw():
    screen.clear()
    screen.drawRound(0, 0, 50, 50, '#fff')
    screen.drawRound(-10, 5, 5, 5, '#fff')
    screen.drawRound(10, 5, 5, 5, '#fff')
    screen.fillRound(0, -6, 20, 20, '#fff')
    screen.fillRound(0, -5, 20, 20, '#000')

I didn't know the solution to this problem.

I guess that the init section in Python works differently than in MicroScript.

Probably in Python asynchronous calls are blocked.

I tried to modify this code in several ways but it always hangs (I don't know Python).

One thing I came up with - throwing out the while loop - the code works but a dozen update loops will be called before it collects the data, the collected data must be passed to a global variable.

Two, using an asynchronous function and calling with the keyword "await", now it doesn't hang but it doesn't receive the data either.

GPT suggests calling "time.sleep( 0.01) " in the while loop but it doesn't work in MicroStudio.

async def download_assets_csv(item):
    result = None
    loader = await asset_manager.loadCSV(item, lambda : loader )
    while loader.ready == 0: 
      pass

    result = loader.text
    print( "download pass")
    print( "text = >>" + loader.text +"<<")
    return result


def init():
    global my_csv, csv_download
    my_csv = 0
    csv_download = False
    csv = download_assets_csv('en')
    print('init() completed')

def update():
    global my_csv, csv_download
    if not csv_download:
        print(my_csv)
        csv_download = True


def draw():
    screen.clear()
    screen.drawRound(0, 0, 50, 50, '#fff')
    screen.drawRound(-10, 5, 5, 5, '#fff')
    screen.drawRound(10, 5, 5, 5, '#fff')
    screen.fillRound(0, -6, 20, 20, '#fff')
    screen.fillRound(0, -5, 20, 20, '#000')

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community