Help with HUMP class system

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
yoyohoho
Prole
Posts: 14
Joined: Tue Apr 14, 2020 12:07 pm

Help with HUMP class system

Post by yoyohoho »

Hey everyone :)

I'm working on the foundation of a CS50 lecture, but there are a couple of things regarding the HUMP class system that I just don't understand, and I haven't been able to find answers on the forum nor anywhere else.

I've created a three classes (Player.lua, Enemy.lua, Orb.lua) and I'm at a point where I would like the Enemy to cause Damage to the player if they are the same position on the x-axis.

I've made a function in Player.lua called

Code: Select all

Player = Class{}

function Player:init(map)
-- Here I load in all the attributes of the player.
	self.map = map -- I'll come back to this line
end
function Player:update(dt)
-- Here I update the state, animations, and update positions as well as create a function that looks like:
	function takeDamage()
		self.health = self.health - 1
		return self.health
	end
end
Now in my Enemy class I would like the player to take damage when the conditions are right, but I have no idea how to call the takeDamage function in another class.. hoping one of you can help me.

Furthermore, I've been experimenting a little and I'm not quite sure what the self.map = map does. My guess is that it takes the parameter and creates a variable with it.

It would be much appreciated if you could explain it to me like I was 5 years old. Any other sources would also be greatly appreciated. I just don't think I'e quite grasped the concept yet and I kind of feel like I'm cheating myself a little using HUMP, yet I'm not totally sure what it actually provides me with.

I've attached Player.lua. It looks very similar to Enemy.lua.
Player.lua
my sphaghetti code
(11.84 KiB) Downloaded 137 times
Thank you! :)
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Help with HUMP class system

Post by pgimeno »

Uh, there seems to be some confusion of concepts here.

Lua function declarations are syntactic sugar for assignments. When you do this:

Code: Select all

function takeDamage()
  self.health = self.health - 1
  return self.health
end
what Lua is doing under the hood is:

Code: Select all

takeDamage = function ()
  self.health = self.health - 1
  return self.health
end
In Lua, global functions are global variables whose value is a function object; similarly, local functions are local variables whose value is a function object, and class methods are table fields whose value is a function object. Since takeDamage is not declared as local, it is by default a global variable. Therefore, you're creating takeDamage as a global function.

That is not a good thing in this case, because you want it to affect only the Player object, and if you have several Player objects, you want it to affect one of them and be able to choose which one. Yet as things are in your code now, if you have the same thing in Enemy, it means that every time you call Player:update(), you're defining takeDamage as a global function, and if you then call Enemy:update(), you're overwriting the takeDamage global with another function that only affects enemies. That's not what you want at all. Furthermore, by defining it every time Player:update() is called, you're creating and deleting lots of function objects unnecessarily.

I think you would do well in reading an introduction to Lua scope, tables, functions and objects. I suggest the PIL. It's for Lua 5.0 but there are very few changes with 5.1 that most probably won't affect you.
yoyohoho
Prole
Posts: 14
Joined: Tue Apr 14, 2020 12:07 pm

Re: Help with HUMP class system

Post by yoyohoho »

pgimeno wrote: Tue Apr 28, 2020 8:51 am Uh, there seems to be some confusion of concepts here.

Lua function declarations are syntactic sugar for assignments. When you do this:

Code: Select all

function takeDamage()
  self.health = self.health - 1
  return self.health
end
what Lua is doing under the hood is:

Code: Select all

takeDamage = function ()
  self.health = self.health - 1
  return self.health
end
In Lua, global functions are global variables whose value is a function object; similarly, local functions are local variables whose value is a function object, and class methods are table fields whose value is a function object. Since takeDamage is not declared as local, it is by default a global variable. Therefore, you're creating takeDamage as a global function.

That is not a good thing in this case, because you want it to affect only the Player object, and if you have several Player objects, you want it to affect one of them and be able to choose which one. Yet as things are in your code now, if you have the same thing in Enemy, it means that every time you call Player:update(), you're defining takeDamage as a global function, and if you then call Enemy:update(), you're overwriting the takeDamage global with another function that only affects enemies. That's not what you want at all. Furthermore, by defining it every time Player:update() is called, you're creating and deleting lots of function objects unnecessarily.

I think you would do well in reading an introduction to Lua scope, tables, functions and objects. I suggest the PIL. It's for Lua 5.0 but there are very few changes with 5.1 that most probably won't affect you.
Alright, thank you very much, I'm going to start reading. I have definitely mixed some concepts together and got them all messed up..
Just to sum up, how would you change a variable from one class (Player) if some conditions are met in another (Enemy)?
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Help with HUMP class system

Post by pgimeno »

It would be a method of the Player class, that is a field of the class table, and the Enemy would need a reference to an instance of the class Player so that it knows what player to damage. Then I would somehow pass the Player instance to the damage method of the Enemy class, and the Enemy would call the method of that Player instance.

I have not used hump.class so I don't know how it is supposed to implement the above. But it surely allows creating methods in the class that the instances can use.

I can tell you how I would make it in pure Lua, without hump.class:

Code: Select all

local Player = {}
local PlayerMT = {__index = Player}

function Player.new(initial_health)
  local self = {
    health = initial_health;
  }
  return setmetatable(self, PlayerMT)
end

function Player:takeDamage(value)
  self.health = self.health - value
  return self.health
end

local Enemy = {}
local EnemyMT = {__index = Enemy}

function Enemy.new()
  local self = {}
  return setmetatable(self, EnemyMT)
end

function Enemy:damage(player)
  player:takeDamage(1)
end

-- create the instances
local player = Player.new(20)
local enemy = Enemy.new()
-- make the enemy damage the player
enemy:damage(player)
Post Reply

Who is online

Users browsing this forum: Google [Bot], togFox and 172 guests