Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8906303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:37:09+00:00 2026-06-15T02:37:09+00:00

I’m using Lua and LuaBridge with Allegro 5. I decided to port all graphic

  • 0

I’m using Lua and LuaBridge with Allegro 5. I decided to port all graphic objects to Lua, but I’m encountering some problems:

Character class that is called once

Character = {sprite; AI}

function Character:new()
  o = o or {} -- works!
  setmetatable(o, self) -- works!
  self.__index = self  -- works!
  return o -- works!
end

function Character:LoadSprite(filename)
  self.sprite = self.sprite or Bitmap() -- works!
  self.sprite:LoadFile(filename) -- works!
  self.sprite.x = 0 -- works!
  self.sprite.y = 0 --works!
end

function Character:SetX(xx)
  self.sprite.x = xx -- maybe? cannot tell if it works or not.
end

function Character:AddBehavior(fname, cname)
    self.AI = self.AI or Pattern()
    self.AI:AddBehavior(fname, cname)
end

function Character:Draw()
  self.sprite:Draw() -- works!
 end

Foo = Character:new()

Lua script whose functions are called from the main program:

function CoreInit() --called at initialization
  Foo:LoadSprite("Image.png") -- works!
end

function CoreLogic() --called during logic cycle
  Foo:SetX(50)  -- does NOT work!
end

function CoreDraw() --called during drawing/rendering cycle
  Foo:Draw()  --works perfectly!
end

So basically, the script initializes the character with appropriate coordinates and image, and draws it, but either the logic isn’t being called (which it is in the logic loop) or something is wrong with the function to change the X coordinate.

Also, the task manager suggests that there is a memory leak of ~30 KB every cycle, which didn’t happen when I had the image objects in C++.

Here’s a snippet of the Bitmap struct that was exported to Lua via LuaBridge in case it was needed:

void Bitmap::Register(lua_State*lua) {
  luabridge::getGlobalNamespace(lua)
    .beginClass<Bitmap>("Bitmap")
    .addConstructor <void (*) (void)> ()
    .addStaticData("scale", &Bitmap::scale)
    .addFunction("LoadFile", &Bitmap::LoadFile)
    .addFunction("Draw", &Bitmap::Draw)
    .addData("x", &Bitmap::x)
    .addData("y", &Bitmap::y)
    .addData("w", &Bitmap::w)
    .addData("h", &Bitmap::h)
  .endClass();
}


void Bitmap::LoadFile(string file) {
  name = file;
  bitmap = al_load_bitmap(file.c_str());
  w = al_get_bitmap_width(bitmap);
  h = al_get_bitmap_height(bitmap);
}

void Bitmap::Draw() {
  if (scale > 1)
    al_draw_scaled_bitmap(bitmap, 0, 0, w, h, x * scale , y * scale, w * scale, h * scale, 0);
  else
    al_draw_bitmap(bitmap, x, y, 0);
}

Bitmap::Bitmap() :  velocity(1) {
   bitmap = NULL; 

}
Bitmap::~Bitmap() {
  name = "";
  if (!bitmap) 
    al_destroy_bitmap(bitmap);
}

UPDATE : One thing I have been able to figure out is that the memory leak is coming from CoreLogic; I commented out the call to it in C++, the memory leak was gone, but when I left the call intact, but commented out the content of CoreLogic, the leak persisted. Hmmm…

SECOND UPDATE : The memory leak has been narrowed down to something with the AI, which I had not posted:

Pattern = {complete; timer; command; Files; Commands; itr; num}

function Pattern:new()
  o = o or {}
  o.Files = {}
  o.Commands = {}
  o.complete = false
  o.timer = 0
  o.itr = 1
  o.num = 1
  setmetatable(o, self)
  self.__index = self
  return o
end

function Pattern:AddBehavior(filename, commandname)
  self.Files[self.num] = filename
  self.Commands[self.num] = commandname
  self.num = self.num + 1
end

function Pattern:DoBehavior()
  self.command = self.Commands[self.itr]
  dofile(self.Files[self.itr])
end

function Pattern:CheckBehavior()
  if self.complete == true then
    self.itr = self.itr + 1
self.timer = 0
self.complete = false
  end
  if itr >= num then
    self.itr   = 1
self.timer = 0
self.complete = false
  end
end

function Pattern:Initialize()
  self.itr = 1; self.timer = 0
  self.complete = false
  self.command = self.Commands[self.itr]
end

And in the CoreLogic function, it would call Foo.AI:DoBehavior(), which did absolutely nothing and caused a memory leak.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:37:10+00:00Added an answer on June 15, 2026 at 2:37 am

    After some more elbow grease, I was able to resolve the issue; essentially, it went back to the Pattern table’s constructor and how the AI object was initialized in Character. I had to change the constructor from:

    Pattern = {complete; timer; command; Files; Commands; itr; num}
    
    function Pattern:new()
      o = o or {}
      o.Files = {}
      o.Commands = {}
      o.complete = false
      o.timer = 0
      o.itr = 1
      o.num = 1
      setmetatable(o, self)
      self.__index = self
      return o
    end
    

    to:

    Pattern = {}
    
    function Pattern:new()
      local o = {complete = false; timer = 0; command = ""; Files = {}; Commands = {}; itr = 1; num = 1}
      setmetatable(o, self)
      self.__index = self
      return o
    end
    

    And this:

    Character = {sprite; AI}
    
    function Character:new()
      o = o or {} 
      setmetatable(o, self) 
      self.__index = self 
      return o 
    end
    

    to:

    Character = {}
    
    function Character:new()
      local o = {sprite; AI = Pattern:new()}
      setmetatable(o, self)
      self.__index = self
      return o
    end
    

    Now the call to Foo.AI:DoBehavior() does as it’s supposed to with no extra memory being used. This code was my first time trying to implement OOP in Lua, and this is probably the way I’m going to go about it from now on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.