Referencing a specific function in a general way

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.
Post Reply
User avatar
baconhawka7x
Party member
Posts: 484
Joined: Mon Nov 21, 2011 7:05 am
Contact:

Referencing a specific function in a general way

Post by baconhawka7x » Tue Jun 17, 2014 9:37 am

When I say general way, I mean in a way to make it convenient.


When we talk about tables, we talk about how they enable us to conveniently reference more than one block of data.

instead of

Code: Select all

appleString = "apple"
bananaString = "banana"
orangeString = "orange"
print(appleString)
print(bananaString)
print(orangeString)
we can simply do

Code: Select all

fruit = {"apple","banana","orange"}
for i=1,#fruit do
    print(fruit[i])
end
but what if we have a series of similar functions instead? I am working on a game that involves a lot of different maps, and I was hoping to just have a unique update function for each one. Just in case I feel like adding some special properties to certain maps that may not need to be applied to all.

I have a folder that contains all of my maps data, and ideally I would like to do something like this:

Code: Select all

function update_map["castle"](dt)
end
So I can simply execute it by running:

Code: Select all

update_map[current_map](dt)
but obviously that isn't possible.

is there a way to run these functions in a organized way such as this? Each function is in it's own individual file, in case there is some way to run a function that corresponds to the file that it is in. I know that it is not that much extra effort to just not try to do something like this and have a conditional at the beginning of each script saying like..

Code: Select all

function update_castle(dt)
     if current_map == "castle" then
         --Code 
     end
end
But I figured it would be worth knowing if there is a simpler way to accomplish this.

Thanks in advanced.

PS It's two in the morning and I am extremely sleep deprived, as usual. So I apologize if it is slightly unclear what I am asking; I can try to explain myself more if it is.

gestaltist
Prole
Posts: 49
Joined: Thu May 29, 2014 10:56 am

Re: Referencing a specific function in a general way

Post by gestaltist » Tue Jun 17, 2014 10:20 am

Hi baconhawka7x.

I can think of 3 ways to do this. I am sure more experienced users will offer other advice, too.

1) OOP.

Make all maps instances of the same class using a library like middleclass or simply make them tables with a field .update which links to a function. Then you can create a variable for the active map in main.lua, and call the update something like this: activemap.update(dt)

If you use proper OOP, you can even have one generic update function and only override it for some of the maps.

2) have one generic update function that takes parameters from the map. (call it with the map as the argument, and have some field in the map called something like “ruleset” which will drive the function’s behavior). This is probably the most complicated approach, but it makes sense for some situations (e.g., I have used it for my CA based map generation algorithm, where you can have a lot of various rulesets for the behavior of your Cellular Automata, but the function does really the same thing for every ruleset).

3) create a table with map names as keys and their update functions as values. Name the table update_map. Then you can do exactly update_map["castle"](dt), which was your suggestion.

User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Referencing a specific function in a general way

Post by Zilarrezko » Tue Jun 17, 2014 10:54 am

^ true dat

To put it into code...

Code: Select all


map_update = {
     theSpammer = function(mssg) print(mssg) end
}

function love.update(dt)
     map_update["theSpammer"]("lul")
end
works

but obviously, practically you want

Code: Select all

function love.load()
     function setCurrentMap(map)
          currentMap = map
     end
     
     function getCurrentMap()
          return currentMap
     end

     map_update = {
          munkee = function(dt)
                print("bacon")
          end,
          anotherMap = function(dt)
               --block
          end
     }

     setCurrentMap("Castle_2")
end

function love.update(dt)
     map_update[getCurrentMap()](dt)
end

function love.draw()
     map_draw[getCurrentMap()]()
end
That would seem to be the way you would do it. But of course the others that gestaltist mentioned will get the job done. :awesome:

Have fun coding at night!

User avatar
baconhawka7x
Party member
Posts: 484
Joined: Mon Nov 21, 2011 7:05 am
Contact:

Re: Referencing a specific function in a general way

Post by baconhawka7x » Tue Jun 17, 2014 8:09 pm

Works perfectly, thanks a ton!:D

User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Referencing a specific function in a general way

Post by undef » Sun Jun 29, 2014 12:17 am

Interesting approach, but why constantly ask for something that is deterministic?

Code: Select all

local map

maps = {
    mapA = {
        update = function () if <you are moving to mapB> then map = maps.mapB end end,
        draw = function() <...> end,
    }
    mapB = {
        update = function () if <you are moving to mapA> then map = maps.mapA end end,
        draw = function() <...> end,
    }
}


function love.load()
    map = maps.mapA
end

function love.update( dt )
    map.udate(<probably t or something>)
end

function love.draw()
    map.draw()
end

This way your program doesn't have to check all the time which map you are currently using, because you just switch out the map whenever needed.
twitter | steam | indieDB

Check out quadrant on Steam!

Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests