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..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?
"Questions that don't deserve their own thread" thread
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- 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
Re: "Questions that don't deserve their own thread" thread
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.DavidOliveiraSilva wrote: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..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?
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, ... }
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
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: "Questions that don't deserve their own thread" thread
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.
- 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
I understood wrong. sorry
-
- Prole
- Posts: 5
- Joined: Thu Jul 24, 2014 12:06 am
Re: "Questions that don't deserve their own thread" thread
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:
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
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: "Questions that don't deserve their own thread" thread
Like luadocs?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
Re: "Questions that don't deserve their own thread" thread
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
Re: "Questions that don't deserve their own thread" thread
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
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: "Questions that don't deserve their own thread" thread
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).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
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
Re: "Questions that don't deserve their own thread" thread
LuaDoc is definitely the most common format. It actually looks a lot like the example you gave.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
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
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 5 guests