Programming Efficiency (new programmer question)

Started by The Pink Bunny, March 17, 2016, 09:58:56 PM

Previous topic - Next topic
March 17, 2016, 09:58:56 PM Last Edit: March 17, 2016, 10:12:46 PM by The Pink Bunny
I'm programming an item that is a short range fire spell that creates a few custom entities right in front of the hero. I currently have each of them created in a separate function local to the item so that I can create each of them sequentially. Right now I just have each of them take the hero, map, and game as input for things like where to be and not to be created if it's on a wall tile. I'm wondering if it would be faster for the computer to take the hero's position in on_using() and use that as the input, or does that really matter for something like this?

Edit: I realize that there's a lot of things in here that I could probably optimize better, but would it really affect performance?

Code ( lua) Select
function flamethrower:on_using()

if not item_cooldown then

local map = self:get_map()
local hero = map:get_entity("hero")
local game = self:get_game()
local flame_base
local flame_mid = {}
local flame_end = {}
item_cooldown = true

flame_base = flamethrower_base(map, hero, game)
flame_mid = flamethrower_mid(map, hero, game, flame_base)
flame_end = flamethrower_end(map, hero, game, flame_mid)

end

flamethrower:set_finished()

end


And the flamethrower_base function:
Code ( lua) Select
local function flamethrower_base(map, hero, game)

local x,y,layer = hero:get_position()    -- these two lines happen in each of the three functions. Should I do it differently?
local dir = hero:get_direction()

if dir == 0 then
x = x + 12
elseif dir == 1 then
y = y - 12
elseif dir == 2 then
x = x - 12
elseif dir == 3 then
y = y + 12
else
print("Error in flamethrower.lua; flamethrower_base(): direction is not integer 0 to 3")
end


flame_base = map:create_custom_entity{ 
name = "flame base",
x = x,
y = y,
width = 8,
height = 8,
layer = layer,
direction = dir,
sprite = "entities/flame_base",
}

flame_base:set_origin(4,4)

local xb, yb, layerb = flame_base:get_position()

if map:get_position(xb, yb, layerb) == "wall" then
flame_base:remove()
base_exists = false
end

flame_base:add_collision_test("overlapping", function(entity, other)

if other:get_type() == "enemy" then
other:hurt(1)
end
end)

return flame_base

end


(also, is there a hotkey to break an infinite loop while the game is running? I keep doing that to myself)

No, there is no significant performance difference, simple functions like hero:get_position() are very fast.

To stop the game when you have an infinite loop, if you run the game from the quest editor, you can press the play/stop button. In 1.5, this will be also possible when running the quest without the quest editor (there will be a window with menus).

Code (lua) Select
      if map:get_position(xb, yb, layerb) == "wall" then
                flame_base:remove()
                base_exists = false
        end


shouldn't this be map:get_ground ?

March 18, 2016, 05:59:35 PM #3 Last Edit: March 18, 2016, 06:05:09 PM by The Pink Bunny
Okay, thanks! And yes, that should definitely be map:get_ground() . Whoops.

And hitting the stop button doesn't seem to work when that happens. I keep having to go into task manager to stop the game.

March 18, 2016, 06:31:53 PM #4 Last Edit: March 18, 2016, 06:33:46 PM by MetalZelda
If the game freeze but stay active (no crash but need to force manual close through task manager (the so-called infinite loop)) then it can be something related to your code (timer, while loop ?) or it is a bug.