Metatable query

@Monkeyman32123 Is this what you’re after.


function setup()
    a=animal()
    a:aprint()
    h=horse()
    h:hprint()
    h:aprint()
      
end

function draw()
    background(40,40,50)
end

animal=class()

function animal:init()
end

function animal:aprint()
    print("animal")
end

horse=class(animal)

function horse:init()
end

function horse:hprint()
    print("horse")
end

Yes! That’s exactly what I was looking for! Thank you very very much!
But if I make an instance of “horse” does it just run horse:init or animal:init, or does it run both? And if it runs both, in what order?

@Monkeyman32123 An instance of horse will just run horse:init. Same for animal. If you want to check, put a print statement in each init.

Alright, thank you sire, you’ve been a great help!

So, I have a grid of rectangles in a table (called leveltileslocs), where leveltileslocs[0] is he location of the bottom left of the grid and leveltileslocs[#leveltileslocs-1] is the location of the top right of the grid. I wrote the code below as an attempt to tell if the center of the screen is touching anywhere on the grid. And I can’t figure out for the life of me why it won’t work.

function World:isActive()
    if self.leveltileslocs[0].x+self.loc.x < (WIDTH/2) and 
    self.leveltileslocs[0].y+self.loc.y < (HEIGHT/2) and 
    self.leveltileslocs[#self.leveltileslocs-1].x+self.loc.x > (WIDTH/2) and
    self.leveltileslocs[#self.leveltileslocs-1].y+self.loc.y > (HEIGHT/2) then
        self.active = true
        return true
    else
        self.active = false
        return false
    end
end

This problem goes a lot deeper than what I believed to be the case in my previous post…for some reason the entire thing is creating an extra square in the grid, and I can’t figure out why. The extra square doesn’t seem to follow the rules of the other squares, and now I’m wholly confused

@Monkeyman32123 -

what is self.loc.x?

post your grid creation code if you need help with that

Okay, I’ll just post the whole thing…you’ll see what’s wrong with it if you tinker with the grid (there seems to be an unused square, or an extra square or something…can’t figure it out) or if you tinker with the World:isActive() function. The full code is in the gist below.

https://gist.github.com/monkeyman32123/3e52e88bfe08e6541f0b

Nevermind, I’ve figured it out. Now I’ve a brand new problem, though.
The hero collides with the black squares, but it seems to snag on the walls too

https://gist.github.com/monkeyman32123/3e52e88bfe08e6541f0b

In addition to the wall snagging issue, there is also a problem with the synchronization of the chunks (this one is VERY easy to see)

https://gist.github.com/monkeyman32123/3e52e88bfe08e6541f0b

I managed to finish the wall snagging issue (though I don’t fully understand how I did it), but I’m still having a chunk synchronization issue when going from one chunk to another while touching the walls of the entrance (it’s a pretty hardcore issue). My hope is to, once I get this issue fixed, transform this code into a game template that anyone can use (and also to make a pretty little adventure game with the new musics!)

EDIT: chunk synchronization issue fixed by making it only possible to have one active chunk at a time. Now the issue of the hour is that the worldmap isn’t putting any values except for “1” for the world’s level

EDITEDIT: FIXED!! No known bugs :slight_smile:

https://gist.github.com/monkeyman32123/3e52e88bfe08e6541f0b

Here’s a modified version that works very similarly to “The Binding of Isaac”, please use these templates freely, I think some interesting things could be done with them (just be sure to give me some credit if you use them :))

https://gist.github.com/monkeyman32123/3eec242952350ade8f49

I’m trying to draw lines from the player to the edges of the screen at specific angles (and I need to constrain the ends of the lines to the edges of the screen). You’ll see the code for this in the “flashlight” class. Needless to say, it doesn’t work. But I can’t figure out why. Angles have always confused me, and converting a line to a line segment based on a rectangular constraint is confusing me even moar.

https://gist.github.com/monkeyman32123/d521856ab7cb4ad103f0

I’ve been working on this thing a lot, but one issue has been nagging at the back of my mind the entire time: the player gets snagged on walls. I have no clue why or what I did wrong, though I am sure it is in the collision detection code in the hero class’s move function.

https://gist.github.com/monkeyman32123/02f4a8e591badf749b4e

Okay, so, I’m trying to move a circle; I have a move function (with the original code commented out, and the new physics-using code being used). When I run the move() function, no matter what, the circle just goes straight down. What am I doing wrong here?!

function Player:move(mag,ang)
    --[[
    if mag and ang then
    mag = mag/10
    self.loc = self.loc + vec2(mag*math.cos(ang),mag*math.sin(ang))
    self.ang = ang
    end
    ]]--
    
    if mag and ang then
    mag=mag/10
    self.hitbox.linearvelocity = vec2(mag*math.cos(ang),mag*math.sin(ang))
    self.loc = self.hitbox.position
    end
    
end

EDIT: I’m stupid, forgot it was pixels per SECOND so instead of per frame (mag/10) I needed per second (mag*6). Also forgot that gravity is by default 1. IGNORE ME GUYS!

@Monkeyman32123 Can’t help you with metatables, haven’t played around with them. I’m surprised someone hasn’t responded yet. There were a lot of previous discussions on metatables. Have you tried a search for them.

mt = {}
    debug.setmetatable("",mt)
    mt.__add = function (op1, op2) return op1 .. op2 end
    print("foo"+"bar")
    --__add=function(a,b) return(a-b) end
    print(rawget(debug.getmetatable(""),"__add"))
    print(mt.__add)
    print(rawget(debug.getmetatable(""),"__add")==mt.__add)
    --attempt to now modify it for numbers
        debug.setmetatable(5.0,mt)
    mt.__concat = function (op1, op2) return tonumber(tostring(op1) .. tostring(op2)) end
    mt.__len = function(a) return #(tostring(a)) end
    mt.__add = function(a,b) return a - b end
    print(5 .. 4)
    print(#507)
    print(5+4)
    print(mt.__add(5,4))

Okay, so, I’m trying to find a way to find a way to modify the primitive “+” operator for integers (for a demonstration of theoretical quantum mechanics, where the number system and operation of operators are different than in our number system). I figured out how to modify the add (“+”) function to work for strings, but I can’t figure out how to edit the one for integers. (As shown in the second half of the above code)
Just stick this code in the setup to see what happens

EDIT: added working examples of changing the number meta table alongside the non working modification of the primitive addition operator

EDITEDIT: as a side question: what is the practical use of “…” In a functions parameters (eg. “function func(a,b,…)” or “function func(…)”)? I can’t figure out why it exists, as I don’t see a way to interact with these parameters (I believe they go on the stack and are then discarded, correct?); all I know is that it is correct from a syntax standpoint.

The purpose of the … in function func(…) allows you to pass a variable number of parameters to the function. In function func, you loop thru the list of parameters like going thru a table.

@Monkeyman32123 Here’s an example of using the … in a function.


--# Main

function setup()
    test(1,"test",2/5)
    test("qwerty",25,"as",23,56)
    
end

function test(...)
    for z=1,arg.n do
        print(arg[z])
    end
    print()
end

Oh, okay, so it saves all of those parameters in a local variable arg? See, now I see nearly infinite uses for the “…”! The lua documentation didn’t specifically say that it saved those values to a variable, so I thought they were just thrown away. Thank you for that! That makes so many more things possible (because I’m a brace hater)! You sir, are a saint!
(Anything on my other question? I’m not having much luck with it)