Share Your Code

js.lua

Posted by jstrahan, Posted on October 21, 2011

Features:

1. js.memCheck — checks memory usage and prints to the terminal
window

2. js.memRepeat — checks memory usage and prints to the terminal
window every so many seconds you set, also cancels
the repeat

3. js.highscoreAdd — adds a name and score to a highscore table and
sorts it highest to lowest can save as many
highscores as you want to

4. js.fileInit — sets up the data file for the game

5. js.fileSave — saves the data file for the game

6. js.fileLoad — loads the data file for the game

7. js.collisionCheck — checks for collision of two objects

8. js.lowWeighted — returns a random number weighted to give more
low numbers then high numbers

9. js.highWeighted — returns a random number weighted to give more
high numbers then low numbers

get it here
http://j-strahan.com/main/?category_name=free


Replies

canupa.com
User offline. Last seen 3 weeks 2 days ago. Offline
Joined: 20 Jun 2011

nice one! thanks!

-finefin

jstrahan
User offline. Last seen 6 hours 43 min ago. Offline
Joined: 29 Jul 2010

welcome
hoping to get an example of using each up soon. I'm working to get graffiti full running first

canupa.com
User offline. Last seen 3 weeks 2 days ago. Offline
Joined: 20 Jun 2011

all in all you provide some great ideas in your module. I especially like the random weighting and highscore table methods. but there are some problems...

first, you naturally have to return the whole js table at the end of the module or the functions won't work.

also, some of the functions do not work properly without some preparations.
Take the collision detection, that expects a table to be passed, i guess -> arg1 ={ x1 = ??, y1 = ??, w1 = ??, h1 = ?? }

I changed it to

1
2
3
4
5
6
7
8
9
10
js.hitTestSquare = function( obj1, obj2 )
            if obj1.x > obj2.x + obj2.width - 1 or
               obj1.y > obj2.y + obj2.height - 1 or
               obj2.x > obj1.x + obj1.width - 1 or
               obj2.y > obj1.y + obj1.height - 1 then
                return false
            else
                return true
            end
end

why hitTestSquare? well, I also added a hitTestCircle function, wich is great for round objects:

1
2
3
4
5
6
7
8
9
10
11
12
13
-- example hitTestCircle(obj1, obj2, [distance in pixels])
js.hitTestCircle = function (obj1, obj2, dist)  
                local dist = dist or 30
                local sqrt = math.sqrt
                local dx =  obj1.x - obj2.x
                local dy =  obj1.y - obj2.y
                local distance = math.sqrt(dx*dx + dy*dy)  -- pythagoras FTW!
                if distance < dist then 
                        return true
                else
                        return false
                end
end

I will check out the other functions as well and tell you if I find something else to add or correct...

-finefin

EDIT: found another one:

line 9: local rdmSeed = rdmseed
should be: local rdmSeed = math.randomseed

jstrahan
User offline. Last seen 6 hours 43 min ago. Offline
Joined: 29 Jul 2010

thanks

dont know how i missed returning the table actually this file is part of a larger file that has more functions then what i included but thought they needed more work so I must have missed a couple things when I pulled these functions out

the only reason i setup the collision that way was if someone had changed the reference point it would break the code but by passing a table with the values you could make sure the values were correct

like the circle option

rdmSeed must had got changed when i did autoreplace and i did not catch it

anyway ill correct problems in file today and i have more ideas for the collision detection so ill be working on that some more

thanks for the comments

jstrahan
User offline. Last seen 6 hours 43 min ago. Offline
Joined: 29 Jul 2010

just updated file everything should work fine now
i decided to leave the collision function the way i have it for now but will be updating it in a few days with better control

canupa.com
User offline. Last seen 3 weeks 2 days ago. Offline
Joined: 20 Jun 2011

allright, I found some more bugs inside the highscoreAdd function:

first, arg1 has to be the pointer itself, not the name of the pointer as a string. otherwise arg1 would be nil.

then I always get the error "attempt to compare number with nil"
on the line: until a == 0 or arg2 < tonumber(arg1[a])
tonumber(arg1[a]) is always nil and therefore not comparable to arg2.
tonumber returns a number from a number-string, if there's no number, it's nil.

took a while until the penny has dropped :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
         js.highscoreAdd = function( arg1, arg2, arg3 )
            -- local a = #arg1 - 1  -- BUG! should be:
            local a = #arg1
            print( a )
            repeat
                a = a - 2
                print( arg1[a], arg2)
            until a == 0 or arg2 < tonumber(arg1[a])
            print("inserting at "..a )
            table.insert( arg1, (a+1), arg3 )
            table.insert( arg1, (a+2), arg2 )
            table.remove( arg1, #arg1 - 1 )
            table.remove( arg1, #arg1 )
            return arg1
        end

LUA tables begin with Index 1, not 0 like in other languages.
now it actually works.

-finefin

jstrahan
User offline. Last seen 6 hours 43 min ago. Offline
Joined: 29 Jul 2010

thanks
I'll have to check it out more cause it works perfect in a game I'm working on