Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
HTML Export and loadfile — Gideros Forum

HTML Export and loadfile

perrochonperrochon Member
edited April 2020 in General questions
Getting
classes/newTileMap.lua:9: attempt to call a nil value
stack traceback:
	classes/newTileMap.lua:9: in function 'init'
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:66: in function 'new'
	classes/TileMap.lua:35: in function 'init'
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:66: in function 'new'
	classes/ScenePlay.lua:19: in function 'init'
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:66: in function 'new'
	classes/scenemanager.lua:287: in function 'changeScene'
	classes/SceneLobby.lua:35: in function 
	classes/Button.lua:54: in function
I looked at a previous thread, but the solution isn't fully clear to me. Not sure how to structure this so it properly packs on export.

Line 9 is
map = loadfile("level1.lua")()
level1.lua is a file generated by tiled. I tried to mark it "exclude from execution" or not, neither worked. It seems to be part of the export as the log says
Exporting level1.lua
How am I supposed to this this?

level1.lua starts like this, basically returning a big structure. It's a straight Export As Lua from tiled, and Gideros wouldn't really manage the file except for packing it up. It does work on the local player.
return {
  version = "1.2",
  luaversion = "5.1",
  tiledversion = "1.3.4",
  orientation = "orthogonal",
  renderorder = "right-down",
  width = 36,
  height = 36,
  tilewidth = 100,
  tileheight = 100,
  nextlayerid = 4,
  nextobjectid = 1,
  properties = {
    ["Title"] = "Bushido Battle Level 1"
  },
  tilesets = {
    {
      name = "Bushido",
      firstgid = 1,
      tilewidth = 100,
      tileheight = 100,
      spacing = 0,

Comments

  • MoKaLuxMoKaLux Member
    edited April 2020 Accepted Answer
    I am also using TILED and it works on html5 (https://mokalux.itch.io/gideros-platformer-lf).
    As you wrote, I only exclude the tiled.lua from execution.
    A wild guess, is your path to your tiled.lua file correct?
    Is your tiled.lua file in the assets folder?
    Example: self.tiledlevel = loadfile("tiled/level01.lua")()
    PS: an easy way to insert any files into your code is right click the file and choose insert into document :)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • hgy29hgy29 Maintainer
    Hi @perrochon, it is the third time you are having issues with exports, and all three seem related somehow, as if the options you set don’t completely make it into your exported project. Since it is html, I am wondering: do you always do full exports (don’t use ‘asset only’ unless you know what you are doing), and you always upload all files to your web server ? Is there any cache enabled in your browser ?
  • I always do full export, yes. Haven't done "asset only" yet, because I don't know what I am doing :-) I also do shift-reloads on browser regularly, which should bypass the cache. I also try different browsers, and anonymous windows.

    What you see is selection bias on what I post in the forums :-) Also, I am exporting pretty trivial games, but the export is still the same high complexity.

    I have lots of issues beyond exporting. I just don't bother the forums, because I can generally figure things out. Much of my other problems have been worked through years ago by others, many have sample code, and many are more straight-forward to debug. It's slow going, but I can all do it on one machine, within the tool. It's actually pretty awesome. I spent 2 days getting my tiled import to work because I create my own texture maps from scratch, and couldn't get any of the demo projects to work and eventually rewrote it from scratch (learning from the demo projects), and 2h of it were lost because I didn't have a self:addChild(tilemap) at the end of my init() code. But it's doable and I learn Lua and Gideros. Persistence leads to outcomes here.

    The export issues are trickier as they are often not documented, not well discussed in the forums, and very configuration dependent. I don't fully understand the HTML export and player magic, and I am only slowly figuring out how to debug it. I don't fully understand what "exclude from execution" actually means. Is is still packed? Does it go into the same directory? Which directory does it go into anyway? What is the asset folder, and why do some files in some demos end up in there, and others don't. Much of that works in the IDE and the local player, but on export, stuff happens. It doesn't help that the export files are all packed into an archive that I haven't figured out yet how to peek into. The problem here is that once I try every combination of options, or many of them, I don't know how to proceed to unstuck.

    For example, I failed at directly creating APKs from Gideros, I have to go through Android Studio. That is suboptimal, and ads time, and lots of manual patching of the exported files, but I have a working solution, so not bothering the forums either with that one, not until I understand better. I may try again in a month or two.

    Also, don't get me wrong. The fact that these exports mostly work is pretty awesome by itself.

    Don't get me started about getting Google Play Console to publish anything, or dealing with Android in general.

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
  • Thanks @MoKaLux
    MoKaLux said:


    Is your tiled.lua file in the assets folder?

    I tried it in the top directory, where my main is, as well as in assets/classes/ I haven't tried just in assets/ That did the job (and yes it's excluded from execution).

    I'll have to figure out where to put all the other files and a workflow with tiled, but that's for tomorrow.
  • hgy29hgy29 Maintainer
    Well I can't see any issue in what you are doing, so I was trying non gideros related gotchas...

    About player vs export differences, there are few well known ones:
    - in the player, files are uploaded independently, and neither encrypted nor compiled. During export lua files are compiled, merged together (unless excluded from execution), and then encrypted (if required)
    - in the prebuilt desktop player, all plugins are available even if not selected in the project. On export only those selected are actually exported
    - on windows, file names are not case sensitive, but they are on most other platforms

    About the 'exclude from execution': in Gideros all lua files are read and executed by default, you don't need to 'require' them, and if you do those files will be parsed twice. To tell Gideros that a particular file shouldn't be read and executed by default, you "exclude (it) from execution". During export, and unlike on the player, files meant to be executed on start are merged together in a single, compiled main.lua file. This shouldn't cause any trouble since they are not expected to be require'ed or loaded from some other lua code. Gideros 2020.4 will have an option to only load main.lua by default.

    Now about the assets folder: this is a relatively new addition. Gideros used to have a virtual file hierarchy, in which all files were just links to real files anywhere on your hard drive. This was inconvenient when working with versionning systems, archiving projects and other related stuff. Gideros now has an assets folder that is synchronized with the studio's file view. If you create files from the studio, they go in that folder. If you add files externally into this folder, then they will appear in the studio on next 'Refresh'. Links still exists though, they are marked with a little 'chain' icon in the studio.

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited April 2020
    @perrochon here is an example which uses a class to draw your Tiled maps.
    My workflow is:
    - put all your files in the assets folder :)
    - I put my tileset in a separate folder
    - then in the Tiled lua file I remove the ../
    perrochon said:


    So I wrote code to remove the "tiles/" from each image. It works, but there must be an easier way.

    I would be interested in that code :)

    Congrats for your game. Don't be shy to show your work: https://www.perrochon.com/BushidoBattlePreCompress/ :)

    PS: a little self promotion: I built the tileset with my program https://mokalux.itch.io/onetiletotileset made with gideros :) Hope you are on windows :*
    zip
    zip
    simple_tiled_example.zip
    8K
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • perrochonperrochon Member
    edited April 2020
    My game is pretty much @Greywine's tutorial. The link above gives you the game after I was done with the tutorial. It's not "(my) work" just yet. Most of my work was getting it to run and deploy.

    A better URL for a sneak peak at the game is the WIP version at https://www.perrochon.com/BushidoBattle/ which has the tiled map. It's always work in progress, e.g. as of this writing, the damage circles no longer work while I am refactoring the layers code. I also have an Android internal release, PM me if you want to get on the testers list. The game is so basic, you won't spend more than 5 minutes until you are bored :-)

    Thanks @hgy29 for the explanation. I was speculating something along the lines as I did notice the little link items. I was slowly reverse engineering it :-) I do like the way it is (I think :-) )

    @MoKaLux I remove the "tiles/" like this when I read the tiled generated level.lua file
    tile.image = tileset.tiles[i].image:sub(7) --- TODO FIX the path issue
    If I change the length of the directory name it will break, of course. Code smell like this makes me hesitant to share :-) I haven't pointed git at this directory yet, as I am learning how the IDE works and what directory structure I need.

    Full code
    function NewTileMap:init()
    	--[[ Reads a level Lua file (generated by Tiled's export as Lua function)
    		 returns a Gideros TileMap
    	]]
     
    	-- load .lua file exported from tiled
    	DEBUG("NewTileMap:init Loading map level1.lua")
    	map = loadfile("level1.lua")()  -- TODO FIX parametrize file name
     
    	DEBUG("  "..map.properties["Title"])
    	DEBUG(("  Size: %dx%d\n  Tile size: %dx%d"):format(map.width, map.height, map.tilewidth, map.tileheight))
    	DEBUG("  Number of tilesets: ", #map.tilesets)
    	DEBUG("  Number of layers: ", #map.layers)
     
    	-- load texture pack exported from Gideros Texture Packer
    	local pack = TexturePack.new("level1.txt", "level1.png") -- TODO FIX parametrize file names
     
    	-- Get all tiles from the editor map and look them up in the texture pack
    	local tileset = map.tilesets[1] -- Only one tile set (for now?)
    	self.myTiles = {}
    	for i = 1, tileset.tilecount do
    		--DEBUG(i, tileset.tiles[i].id, tileset.tiles[i].image:sub(7))
    		--DEBUG("", (pack:getTextureRegion(tileset.tiles[i].image:sub(7))):getRegion())
    		local tile = {}
    		tile.image = tileset.tiles[i].image:sub(7) --- TODO FIX the path issue
    		tile.description = tileset.tiles[i].properties.description
    		tile.blocked = tileset.tiles[i].properties.blocked == "true" and true or false
    		self.myTiles[tileset.tiles[i].id] = tile                        
    	end
     
    	-- Make sure we got everything
    	for key, value in pairs(self.myTiles) do
    		DEBUG("newTileMap:init", key, value.image, value.description, value.blocked)
    		if value.image == nil or value.description == nil or value.blocked == nil then
    			ERROR("ERROR: newTileMap:init Missing Data in tile", key, value.image, value.description)
    		end
    	end
     
    	-- Now make the TileMaps
    	-- TODO FIX Refactor to remove duplicated code. Defer until we have more than 2 layers
     
    	local ground =  Sprite.new() -- ground groups the terrain and environment layers
    	self.myGround = {} -- myground contains the properties of the tiles
     
    	-- Layer 1 Terrain with basic ground tiles like grass, water, stone, etc.
    	local layer = map.layers[1]	
    	local terrain = TileMap.new(layer.width, 
    								layer.height,
    								pack,
    								tileset.tilewidth,
    								tileset.tileheight,
    								tileset.spacing,
    								tileset.spacing,
    								tileset.margin,
    								tileset.margin,
    								tileset.tilewidth,
    								tileset.tileheight)		
     
     
    	for y=1,layer.height do
    		for x=1,layer.width do
    			local i = x + (y - 1) * layer.width
    			local gid = layer.data[i] - 1 -- TODO FIX Get correct offset from map's ("firstgid")
    			--DEBUG(i, x, y, gid)
    			if gid > 0 then -- layer 1 gids should never be 0, but keep code consistent with below
    				self.myGround[i] = gid
    				image = self.myTiles[gid].image
    				region = pack:getTextureRegion(image)
    				tx, ty = region:getRegion()
     
    				--DEBUG(i, x, y, gid, image, tx/100+1, ty/100+1)
    				terrain:setTile(x, y, tx/100+1, ty/100+1)
    				--DEBUG(i, x, y, terrain:getTile(x, y))
    			end
    		end
    	end
    	ground:addChild(terrain)
     
    	-- Layer 2 Environment on top of the basic ground tiles in layer 1 with walls, trees, etc.
    	layer = map.layers[2]	
    	local environment = TileMap.new(layer.width, 
    								layer.height,
    								pack,
    								tileset.tilewidth,
    								tileset.tileheight,
    								tileset.spacing,
    								tileset.spacing,
    								tileset.margin,
    								tileset.margin,
    								tileset.tilewidth,
    								tileset.tileheight)
     
    	for y=1,layer.height do
    		for x=1,layer.width do
    			local i = x + (y - 1) * layer.width
    			local gid = layer.data[i] - 1 -- TODO FIX Get correct offset from map's ("firstgid")
    			--DEBUG(i, x, y, gid)
    			if gid > 0 then
    				self.myGround[i] = gid
    				image = self.myTiles[gid].image
    				region = pack:getTextureRegion(image)
    				tx, ty = region:getRegion()
     
    				--DEBUG(i, x, y, gid, image, tx/100+1, ty/100+1)
    				environment:setTile(x, y, tx/100+1, ty/100+1)
    				--DEBUG(i, x, y, tilemap:getTile(x, y))
    			end
    		end
    	end
    	ground:addChild(environment)
     
    	self:addChild(ground)
     
    	DEBUG("NewTileMap:init Loaded map level1.lua -- so far so good")
    end

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.