Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Not sure why I get "function arguments expected near 'function'" error! — Gideros Forum

Not sure why I get "function arguments expected near 'function'" error!

edited December 2017 in General questions
Hi all! New here, and am so far loving Gideros... just suddenly I have a problem...

Everything worked fine up until just today where I started getting a "function arguments expected near 'function'" error whenever I try to run the game.
If I add "local" before it, I get "function arguments expected near 'local'"

My code:
onMouseUp = function (self, event)
	playerMoving = true
	print(playerMoving)
end
I have tried changing it to
function onMouseUp(self, event)
(with the function name before "function") but that didn't help either.
If I delete that section, I get the same error on the next function.

Everything worked fine before which is strange.

I get the error with simple functions like above, and longer--but still relatively simple-- ones (that worked fine a few hours ago!) like this:
newRect = function(w,h,c)
 
	local shape = Shape.new()
 
	shape:setFillStyle(Shape.SOLID,c, 1)
 
	shape:beginPath()
	shape:moveTo(0,0)
	shape:lineTo(w,0)
	shape:lineTo(w,h)
	shape:lineTo(0,h)
 
	shape:endPath()
 
	stage:addChild(shape)
	return(shape)
end
Any help would be most appreciated!

Thanks!

Comments

  • hgy29hgy29 Maintainer
    Hi @MidnghtCoffee,

    Do you use latest gideros ? If so does the editor (gideros studio) warns you about the same issue ? It looks like the lua parser is stuck at a point it expects function arguments, so probably after an opening parenthesis. What is the code right before the point where it fails ?
  • edited December 2017
    I am using the version 11.2 of this year, which I believe is the latest version.
    It seems like any function above this bit of code works fine.
    for a = 1, 8, 1 do
    	isDone = false
    	numGen = math.random(2)
    	local newBlock
    	print(numGen)
    	if (numGen == 1 and isDone == false) then
    		newBlock = Bitmap.new(Texture.new("images/Ground1.png"))
    		newBlock:setScale(4,4)
    		isDone = true
    		stage:addChild(newBlock)
    	end
    	if (numGen == 2 and isDone == false) then
    		newBlock = Bitmap.new(Texture.new("images/Ground2.png"))
    		newBlock:setScale(4,4)
    		stage:addChild(newBlock)
    	end
    	newBlock.name = ("block" .. a)
    	newBlock.id = a
     
    	newBlock:setX(a *140-140)
    	newBlock:setY(groundLevel)
    end
    That block is something I "translated" from Corona SDK to Gideros, if that matters, but it works as it should.

    Thank you!

    EDIT: And, if I remove the block of code I pasted above, everything works fine...
    Weird...
  • antixantix Member
    edited December 2017
    @MidnightCoffee, welcome :)

    Try these...
    local function onMouseUp(self, event)
    	playerMoving = true
    	print(playerMoving)
    end
    local function newRect(w,h,c)
     
    	local shape = Shape.new()
     
    	shape:setFillStyle(Shape.SOLID,c, 1)
     
    	shape:beginPath()
    	shape:moveTo(0,0)
    	shape:lineTo(w,0)
    	shape:lineTo(w,h)
    	shape:lineTo(0,h)
     
    	shape:endPath()
     
    	stage:addChild(shape)
    	return(shape)
    end
    Would the following code serve the same purpose as the block above??
    for a = 1, 8 do
      local random = math.random
      local newBlock
     
    	if random() < 0.5 then
    		newBlock = Bitmap.new(Texture.new("images/Ground1.png"))
      else
    		newBlock = Bitmap.new(Texture.new("images/Ground2.png"))
      end
     
      newBlock.name = ("block" .. a)
      newBlock.id = a
     
      newBlock:setScale(4)
      newBlock:setPosition(a *140-140, groundLevel)
     
      stage:addChild(newBlock)
    end
  • I apologize for the late reply. I actually just sent in my laptop for emergency repair and am currently using a different device.
    That code does work exactly the same way and works well. The only reason why I was hesitant to do it any other way is because I am translating from corona and intend on having the ground moving as in the code below. But, I guess it should work the same, if I change it to use Gideros's API...?

    From Corona SDK:
    function updateBlocks()
    	if playerMoving == true then
    		for a = 1, blocks.numChildren, 1 do
    			if(a > 1) then
    				newX = (blocks[a - 1]).x + 140
    			else
    				newX = (blocks[8]).x +140 - speed
    			end
    			if ((blocks[a]).x <-55) then
    				-- Add to score
    				score = score + 1
    				scoreText.text = score
    				scoreText.anchorX = 400
    				scoreText.anchorY = 500
    				scoreText.x = display.contentWidth/2 + 25
    				scoreText.y = 150
    				(blocks[a]).x, (blocks[a]).y = newX, (blocks[a]).y
    			else
    				(blocks[a]):translate( speed * -1, 0 )
    			end
    		end
    		updateSpeed()	
    	end
    end 
     
    timer.performWithDelay(1, update, -1)

    Thanks so much!
  • Use a tilemap as it's much better than moving all the blocks like you would have done with Corona SDK.

    http://docs.giderosmobile.com/reference/gideros/TileMap#TileMap
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • What exactly do you mean by that? (Forgive me, I'm new to Gideros) I only have 2 tiles that I would like to appear in a random order, then scroll/loop like in an endless runner.

    Thanks!
  • Ahh - I thought you had more than 2 tiles on screen.

    With a tilemap the entire screen full of tiles is drawn as a single mesh. I personally would use it also for an endless runner.
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • Is there a tutorial you could recommend for this? I simply want to have the ground autoscroll (and have it stop when the screen is held down).
    I don't need any complicated tilemaps because there is only one row in a loop.

    Thank you for your help!
  • @MidnightCoffee okay cool, just use whatever works. I started with Marmalade Quick which was so slow, then went to Corona SDK which was better but needed internet for compiling, and finally found Gideros and have been here ever since, so I totally get it can be a bit daunting adapting to a new way of doing things.

    @SinisterSoft is correct about TileMaps, they are very powerful. Maybe a little more advanced for a new Gideros user so use whatever works for you now but do have a look at them sometime. If you ever saw "Million Tile Engine" for Corona then that's similar to what the Gideros Tilemap class can do :bz

    Your updateBlocks() function would take some conversion. I start to remember now just how different Corona is from Gideros :)

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • @MidnightCoffee if you post a fake screen of what you want and the actual graphics then I should be able to make a quick demo of an everlasting tilemap for you.

    Likes: antix

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
  • @MidnightCoffee please post a screendump of what it will look like so we can see how many tiles will appear across the screen. and you are going to use only 2 different tile graphics or will there be more?
  • Thank you @SinisterSoft, I appreciate that, but I just have a question:
    Is there a reason why I can't just (and forgive me if this is a stupid question) have a block (and subsequently the rest of the blocks behind) move to the end of the screen, and when it gets off the edge, jump to the other side and continue?
  • edited December 2017
    The image of the blocks put into place via the code above is attached.
    I am currently using two, and at the moment don't plan on adding more...
    Thanks!
    Untitled.png
    320 x 502 - 15K
  • @MidnightCoffee, there's no reason at all. If you want to do it your way then do it :)

    @SinisterSoft is just saying Tilemaps are better. For a very simple starter project I'd recommend just going with what you are comfortable with and ramp up to more complex things as you get to grips with Gideros :)

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • Just a footnote here; in my game Miner Z I built a custom system to deal with tiles AND collision. In retrospect it may have made more sense for me to use TileMap and Bump, which would have been a lot more efficient. But I would have had to spend the time learning them too, so as @antix says, do whatever you feel comfortable with.
    My Gideros games: www.totebo.com
    +1 -1 (+2 / -0 )Share on Facebook
  • antixantix Member
    edited December 2017
    @MidnightCoffee here is a small example (need a break from user interface coding *yawn*)..
    local floor, random = math.floor, math.random
     
    application:setBackgroundColor(0x4186f4)
     
    local TILE_WIDTH, blockCount
     
    local groundLevel = 300
    local scrollSpeed = 1
    local distance = 0 -- distance travelled
     
    local blocks = Sprite.new() -- a container to hold our blocks
    local regions = {} -- table to hold TextureRegions
     
     
    -- create TextureRegions
    local function createRegions()
      local insert = table.insert
      local texture = Texture.new("block1.png", true) -- our texture
      TILE_WIDTH = texture:getWidth() -- we will use this a lot so save it now
      insert(regions, TextureRegion.new(texture, 0, 0, TILE_WIDTH, texture:getHeight())) -- our textureregion
     
      texture = Texture.new("block2.png", true)
      insert(regions, TextureRegion.new(texture, 0, 0, TILE_WIDTH, texture:getHeight()))
    end
     
     
    -- create blockCount blocks
    local function createBlocks()
      local x = -TILE_WIDTH
      for i = 1, blockCount do
        local region = regions[random(1, 2)] -- get a random graphic
        local block = Bitmap.new(region) -- create the block
        block:setPosition(x, groundLevel) -- set the blocks position
        blocks:addChild(block) -- attach block to container
        x += TILE_WIDTH -- determine where the next block will be placed
      end
    end
     
     
    -- scroll and reset blocks
    local function scrollBlocks()
      for i = 1, blockCount do
        local block = blocks:getChildAt(i) -- next block to scroll
        local x = block:getX() - scrollSpeed -- scroll block
     
        if x <= -TILE_WIDTH then -- block exited left
          distance += 1 -- increment distance travelled
          x = x + blockCount * TILE_WIDTH -- reposition current block off screen to right
          if random() < 0.5 then -- choose a new random graphic for the block
            block:setTextureRegion(regions[1])
          else
            block:setTextureRegion(regions[2])
          end
        end
        block:setX(x) -- update blocks position
      end
    end
     
     
    -- call every frame
    local function onEnterFrame(e)
      local dt = e.deltaTime
     
      scrollBlocks()
     
    end
     
    -- kick everything off
    createRegions()
     
    blockCount = floor(application:getContentWidth() / TILE_WIDTH) + 3 -- how many blocks there will be (+3 for scrolling purposes)
    createBlocks()
     
    stage:addChild(blocks)
    blocks:setX(-TILE_WIDTH)
     
    stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
    You will notice in the example that we make as many bitmaps as required and then change their graphics using TextureRegions. This improves performance because you won't be creating bitmap objects endlessly (pun intended). Things like this will become more apparent as you become more familiar with Gideros.

    The example could be optimized further by using TexturePacks and classes which are also really fantastic. I'm sure you will learn about those soon.

    In fact I would recommend you get the hang of classes and TexturePacks quickly because later you will use them pretty much exclusively.
  • Ah, I understand. Thank you so much for the example!

    Likes: antix

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