"Questions that don't deserve their own thread" thread

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Locked
User avatar
DavidOliveiraSilva
Prole
Posts: 19
Joined: Fri May 29, 2015 10:19 pm
Location: Quixadá, Brazil
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by DavidOliveiraSilva »

murks wrote:I designed a simple ECS which works with component tables. Those are tables used as arrays and I use the index directly as entity ID. I'm not quite sure that this is a good idea because of the problems with sparse arrays in Lua.

I still wonder a bit about removing entities and components.
For the most part I guess it is OK, as long as I can keep using pairs.

For the actual removal however I guess I will need to simply set values to nil instead of using table.remove(), correct? Otherwise the entries will be shifted down and thus components mixed up. Is there any drawback to this?
I had the same problem when I was working on my graph_editor. I wanna to remove a vertex, but then it was necessary to remove all the edges on the vertex (i had a bad time) and the only solution that works for me was in fact to use the table.remove and then correct all the following entities on my table..
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by s-ol »

DavidOliveiraSilva wrote:
murks wrote:I designed a simple ECS which works with component tables. Those are tables used as arrays and I use the index directly as entity ID. I'm not quite sure that this is a good idea because of the problems with sparse arrays in Lua.

I still wonder a bit about removing entities and components.
For the most part I guess it is OK, as long as I can keep using pairs.

For the actual removal however I guess I will need to simply set values to nil instead of using table.remove(), correct? Otherwise the entries will be shifted down and thus components mixed up. Is there any drawback to this?
I had the same problem when I was working on my graph_editor. I wanna to remove a vertex, but then it was necessary to remove all the edges on the vertex (i had a bad time) and the only solution that works for me was in fact to use the table.remove and then correct all the following entities on my table..
What? If murks wants to delete an entry from a sparse table, then he is very correct in that he needs to just set the value at that key to nil. There are no drawbacks to doing that that that I can think of, using table.remove and shifting the directly following entries back up doesn't help in any way and is very complicated and error-prone.

Also murks: if I understand you correctly, each system has a table like

Code: Select all

positions = { [3] = {10, 20}, [9] = {4, 4}, ... }
sprites = { [3] = entity_sprite_one, [9] = entity_sprite_two, ... }
If that is the case, why don't you do it like this

Code: Select all

entities = {
 [{ sprite = entity_sprite_one, position = {10, 20} }] = true,
 [{ sprite = entity_sprite_two, position = {4, 4} }] = true
 }
 
 -- or basically
 
 entities = {}
 
 -- making a new entity
 local ent = { sprite = sprite, position = {10, 20} }
 entities[ent] = true
instead? That would probably make your code a lot more readable and the performance impact should be negligible.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: "Questions that don't deserve their own thread" thread

Post by bobbyjones »

The reason I mentioned using "nil" vs nil is that he would get the benefit of jit if he iterated his loops using ipairs. Some systems would easily be jitable if he used ipairs. Which would result in a speed up. Using pairs for every system just about guarantees leaving performance on the table. Also using "nil" and setting the entities to the first "nil" spot would generate less trash (maybe) as the table size would remain constant during mostly, although I'm not too sure.
User avatar
DavidOliveiraSilva
Prole
Posts: 19
Joined: Fri May 29, 2015 10:19 pm
Location: Quixadá, Brazil
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by DavidOliveiraSilva »

I understood wrong. sorry ^^
TheLivingTree
Prole
Posts: 5
Joined: Thu Jul 24, 2014 12:06 am

Re: "Questions that don't deserve their own thread" thread

Post by TheLivingTree »

Hello all!

Does anyone know of a good commenting convention for Lua functions? I know Java has its own javadoc for describing and documenting functions, but does Lua have anything similar? I know this differs from coder to coder so I'd love to know what you personally use.

I would think to use something like this:

Code: Select all

	--[[
	-   This function returns all properties of a foo
	-  @param {number} the index of the foo to look up in the foos table   
	-
	-  @returns {table} a table of properties relating to the specified foo
	--]]
	local function foo.getProperties(index)
		return tableOfFoos[index].properties
	end
	
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: "Questions that don't deserve their own thread" thread

Post by bobbyjones »

TheLivingTree wrote:Hello all!

Does anyone know of a good commenting convention for Lua functions? I know Java has its own javadoc for describing and documenting functions, but does Lua have anything similar? I know this differs from coder to coder so I'd love to know what you personally use.

I would think to use something like this:

Code: Select all

	--[[
	-   This function returns all properties of a foo
	-  @param {number} the index of the foo to look up in the foos table   
	-
	-  @returns {table} a table of properties relating to the specified foo
	--]]
	local function foo.getProperties(index)
		return tableOfFoos[index].properties
	end
	
Like luadocs?
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by Beelz »

Quick math help... How would you go about finding the highest divisible number of a number? Here's what I got right now but I just feel like I'm thinking about it the wrong way, any ideas?

Code: Select all

function highestDiv(num)
  for i = num-1, 1, -1 do
    if num/i == math.floor(num/i) then
        return i
    end
  end
end

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: "Questions that don't deserve their own thread" thread

Post by Nixola »

You can start at num/2 instead of num-1
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
pgimeno
Party member
Posts: 3655
Joined: Sun Oct 18, 2015 2:58 pm

Re: "Questions that don't deserve their own thread" thread

Post by pgimeno »

Beelz wrote:Quick math help... How would you go about finding the highest divisible number of a number? Here's what I got right now but I just feel like I'm thinking about it the wrong way, any ideas?

Code: Select all

function highestDiv(num)
  for i = num-1, 1, -1 do
    if num/i == math.floor(num/i) then
        return i
    end
  end
end
You can attempt to find the lowest factor and divide by it. Try 2 first and then try odd numbers starting on 3 and up to math.floor(math.sqrt(num)) (if you get to the end then the number is prime, meaning has no divisors other than itself and 1).

Also, instead of dividing twice I suggest you use num % i == 0.

It's an unusual request. That made me curious. What do you need that for?

Edit: here:

Code: Select all

function highestDiv(num)
  if num % 2 == 0 then return num / 2 end
  for i = 3, math.floor(math.sqrt(num)), 2 do
    if num % i == 0 then return num / i end
  end
  return num -- or 1, depending on what you do with primes
end

print(highestDiv(5)) -- 5
print(highestDiv(33)) -- 11
print(highestDiv(30)) -- 15
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: "Questions that don't deserve their own thread" thread

Post by davisdude »

TheLivingTree wrote:Hello all!

Does anyone know of a good commenting convention for Lua functions? I know Java has its own javadoc for describing and documenting functions, but does Lua have anything similar? I know this differs from coder to coder so I'd love to know what you personally use.

I would think to use something like this:

Code: Select all

	--[[
	-   This function returns all properties of a foo
	-  @param {number} the index of the foo to look up in the foos table   
	-
	-  @returns {table} a table of properties relating to the specified foo
	--]]
	local function foo.getProperties(index)
		return tableOfFoos[index].properties
	end
	
LuaDoc is definitely the most common format. It actually looks a lot like the example you gave.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Locked

Who is online

Users browsing this forum: Bing [Bot], GetAsync, Google [Bot], Semrush [Bot] and 3 guests