Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Deleting" variables #228

Open
NickTheFox99 opened this issue Dec 26, 2024 · 0 comments
Open

"Deleting" variables #228

NickTheFox99 opened this issue Dec 26, 2024 · 0 comments

Comments

@NickTheFox99
Copy link

Hello! I am writing small bits of code in attempt to break this engine, or find some faults. So far, it is doing very good!
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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant