Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

I'm having some issues with spriteSize...

Hey, Codea-users! :)
I've been working on a project and I ran into an error that I'm having trouble fixing. I've written shorter code that causes the same error below:

t = {{"Planet Cute:Character Boy",3},{"Planet Cute:Character Cat Girl",26}}
a = t[1]
local w,h = spriteSize(readImage(a[1]))
print(w+0,h+0)

The error implies that w and h are nil, but when you remove both of the +0s from the print command, it prints two numbers perfectly fine. You can even replace the print command with print(type(w),type(h)), and it will print number number. Another weird thing is if you replace readImage(a[1]) with a[1] on the third line, spriteSize returns 0, 0. I've checked and spriteSize can indeed take in a string as the parameter. I'm not sure if I'm just not thinking straight and I'm forgetting something or not, but I'm hoping you can help me out here. :/

Tagged:

Comments

  • dave1707dave1707 Mod
    Posts: 7,554

    @Kolosso You must be doing something wrong because if I run the below code it prints 101 171.

    function setup()
        t = {{"Planet Cute:Character Boy",3},{"Planet Cute:Character Cat Girl",26}}
        a = t[1]
        local w,h = spriteSize(readImage(a[1]))
        print(w+0,h+0)
    end
    
  • edited December 2016 Posts: 160

    Thanks @dave1707! I just realized the reason it wasn't working was because the code was outside the setup function. I feel kinda dumb now XD

  • dave1707dave1707 Mod
    Posts: 7,554

    @Kolosso It shouldn't matter where you put those 4 lines of code. Maybe you had print(w0,h0) which would give nil.

  • edited December 2016 Posts: 160

    No. I didn't make a mistake like that. As soon as I made it run the code in the setup function it worked perfectly. I don't know how Codea works behind the scenes, but I feel that some of the functions aren't created until just before the setup function runs. That's my only guess.

  • dave1707dave1707 Mod
    Posts: 7,554

    @Kolosso Are you saying you had those lines of code outside of any function. There are just a few things that can be coded outside of any function. I can't give you a correct explanation unless I see those lines of code along with some other code causing the error.

  • @dave1707 @Kolosso I believe the only things that aren't allowed before the setup function are the functions that aren't in the _G table. Correct me if I'm wrong but that's what I've realized from experience.

  • dave1707dave1707 Mod
    Posts: 7,554

    @CamelCoder Functions can be in just about any order. The things that can cause problems is to have code that's not in any function. Some internal variables aren't initialized until the setup function is run, so if you try to use them before the setup function is run, you'll get unknown results.

  • dave1707dave1707 Mod
    Posts: 7,554

    @CamelCoder Here's an example. The print statement outside the function runs first and prints the wrong WIDTH value. The print statement inside setup prints the correct WIDTH value because WIDTH was initialized with the correct size when setup was run. Any code outside of a function is run before setup no matter where it is placed in the project code.

    function setup()
        print("in setup ",WIDTH,HEIGHT)
    end
    
    print("outside a function ",WIDTH,HEIGHT)
    
  • Posts: 2,020

    Stuff to do with graphics doesn't seem possible until setup runs. E.g. Defining a shader before setup will crash the code.

  • edited December 2016 Posts: 160

    I usually create the main tables outside the setup function because I feel it looks neater. It's just the way I code. I suppose I then got a bit too comfortable and ran a function that uses the spriteSize function, outside the setup function. It's fixed now though.

  • dave1707dave1707 Mod
    edited December 2016 Posts: 7,554

    @Kolosso You don't have to create your tables in the setup function. You can put your table creation in any function ( createTable() for example ) at the end of all the code or a seperate tab if you don't want to see it and in the setup function call createTable(). I put everything in a function except supportedOrientations() and displayMode(). Maybe some other things I can't think of offhand.

Sign In or Register to comment.