Skip to content

"Deleting" variables #228

Open
Open

Description

@Luke-2029

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!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions