Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Managing game elements — Gideros Forum

Managing game elements

So, next. I have done the method below to manage the objects (monsters, items, heroes etc) of the games, until it works but I would like your opinion or if you have a different and more efficient method or suggestion.


In short, on the enterframe I go through an array of objects to determine collision, movement, damage, etc ... And when I finish I go through the same array again to check who is dead and eliminate it from the screen and memory. I mean I think this is what is happening LOL.

Thankful.
--in init
self.sprites = {}
 
-- In create obj function
function self:RemoveObj()
	table.insert(self.sprites, Bitmap or MovieClip)
	self.sprites[#self.sprites].dead = false
	self:addChild(self.sprites[#self.sprites])
end
 
-- In EnterFrame
for x=1, #self.sprites do
	-- self.sprites[x] events and actions
end
self:RemoveObj()
 
--In remove obj function
function self:RemoveObj()
	local dead = true
	repeat
		dead = false
		for x = 1, #self.sprites do
			if self.sprites.dead then
				self:removeChild(self.sprites[x])
				table.remove(self.sprites, x)
				dead = true
				break
			end
		end
	until(not dead)
end

Comments

  • I do it this way:
    -- update
    function LF_Dynamic_Character:onEnterFrame(e)
    	if not self.body.isdead then -- NOT DEAD
    		local posx, posy = self.body:getPosition()
    		local vx, vy = self.body:getLinearVelocity()
    		-- more stuff...
    	end
    	-- DEAD?
    	if self.body.type == G_PLAYER and self.body.isdead then
    		scenemanager:changeScene("levelX", 3) -- RESTART LEVEL
    	end
    	for k, v in pairs(self.world.nmes) do -- LIST OF ENEMIES
    		if v.isdead then
    			self.world:destroyBody(v)
    			k:getParent():removeChild(k)
    			self.world.nmes[k] = nil
    			v = nil
    			k = nil -- useful???
    		end
    	end
    end
    ???

    Likes: kinrpg

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • kinrpgkinrpg Member
    It looks complex, but at the same time, a lot of time and code.

    "v in pair", did not know this feature of Lua, cool, it already brings all the data of the current position inside "v".

    "k: getParent (): removeChild (k)", so this is how you use getParent? So your monsters are an external class with their own init and enterframe?

    Very cool. I will study this code.

  • MoKaLuxMoKaLux Member
    edited June 2020
    kinrpg said:

    "v in pair", did not know this feature of Lua, cool, it already brings all the data of the current position inside "v".

    Yes, I have just learnt k, v in pair(table) a couple of weeks ago and it is fantastic :).
    In my case every time I create an enemy I add it to the enemies_list.
    self.nme01 = Character.new("monster01.png")
    self.nme01.body.lives = 3
    self.nme01.body:setPosition(x, y)
    self.fg:addChild(self.nme01) -- here I add the enemy to the foreground
    enemies_list[self.nme01] = self.nme01.body -- and here I add it to the enemy list k, v
    The Character class inherits the Sprite class.
    kinrpg said:

    So your monsters are an external class with their own init and enterframe?

    That's right, the code in the previous post is the enterframe function of the Character class (player, enemies, npcs, ...)

    Likes: kinrpg

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • rrraptorrrraptor Member
    edited June 2020
    kinrpg said:

    In short, on the enterframe I go through an array of objects to determine collision, movement, damage, etc ... And when I finish I go through the same array again to check who is dead and eliminate it from the screen and memory.

    Why you cant destroy objects in same loop? Just go backwards.
    for i = #myTable, 1, -1 do 
    	local obj = myTable[i]
    	if obj.dead then 
    		table.remove(myTable,i)
    	end
    end
    Shorter version of
    k:getParent():removeChild(k)
    is
    k:removeFromParent()

    Likes: MoKaLux, kinrpg

    +1 -1 (+2 / -0 )Share on Facebook
  • kinrpgkinrpg Member
    @rrraptor Boy, do you happen to be going through this list from highest to lowest position? It is possible to do this o.O ... This would save too much loop: D

    How I didn't think of that before HAUAHAUAHAU
  • kinrpg said:

    It is possible to do this o.O

    3d arg to "for" is the step, which is 1 by default

    If order is matter, you can also use while loop:
    local i = 1
    while i <= #myTable do 
    	local obj = myTable[i]
    	if obj.dead then 
    		table.remove(t,i)
    	else
    		-- do stuff
    		i += 1
    	end
    end

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.