Discord
Login
Community
DARK THEME

"Deleting" variables

Foreword: I have already submitted an issue on the Github repo here, before realizing that there was a forum and Discord server... OOPS!

Hello! I am writing small bits of code in attempt to break this engine, or find some faults. So far, it is a very good engine! The one major thing that I found as an issue - if I am trying to make some OOP-based game, I would want to delete some objects, more specifically, instances of a class. I have tried to do this by setting "this" to 0 when I want to delete an object, and checking if the object exists whenever trying to access it elsewhere. It's hard to explain, so here's an example.

Enemy = class
  hp = 10
  
  update = function() end
  
  hit = function(damage)
    hp -= damage
    print("Ouch! I am "+hp)
    if hp <= 0 then
      print("Argh! I am dead now!")
      this = 0
    end
  end
end

Player = class
  constructor = function(opponent)
    this.opponent = opponent
  end

  atkPts = 5
  
  update = function()
    if keyboard.press.SPACE then atk(opponent) end
  end
  
  atk = function(other)
    if other then
      other.hit(atkPts)
    else
      print("Miss!")
    end
  end
end

init = function()
  enemy = new Enemy()
  player = new Player(enemy)
end

update = function()
  player.update()
  enemy.update()
end

Here I am attacking an enemy on space pressed, and it does work. Console outputs when the enemy takes damage, and when the health is zero, it will print it's dead, but on further attacks, the enemy's health will still go into the negatives, never really dying. Please tell me if it's an issue with my code, or otherwise!

I don't think that the ability to set this to zero actually exists. Instead you'd want to do something like so:

...
  
  hit = function(damage)
    hp -= damage
    print("Ouch! I am "+hp)
    if hp <= 0 then
      print("Argh! I am dead now!")
      global.enemy = 0
    end
...

If there is instead a list of enemies, you'd want to write enemies.removeElement(this) instead (if you decide to add more enemies)

Ok, I will be able to work around this. I have been thinking of implementing an ECS library, where I could just set enemy's id to zero, and use zero as a throwaway id. Thanks anyways! PS I had no idea there was a global keyword

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community