Problem with scoping (different files)

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
FLÖRGEN
Prole
Posts: 4
Joined: Thu Jun 25, 2015 2:00 am

Problem with scoping (different files)

Post by FLÖRGEN »

Hi, I have a Game class which features an interface:

Code: Select all

local Game = class("Game")
Game:include(Stateful)

local Menu = Game:addState("Menu")

function Menu:enteredState()
	self.interface = gui.Interface:new():loadFromFile("menu/main.lua")
end
And the menu being loaded from file looks like this:

Code: Select all

local INTERFACE = {}

INTERFACE.button	=	gui.Button:new("New Game", function()
						-- This function is the onClick event for the button
					end)

return INTERFACE
My issue is that I want the onClick event shown above to be able to access the Menu instance (the self part of self.interface line in the first code snippet) and call one of its methods. I'm not entirely sure how to do this due to the button (and its onClick function) being defined in a different file. Just a note, I don't have to do it this way if I must use another to do what I want to but I would still like to be able to load the interface from a file if possible. Thank you :)
User avatar
0x72
Citizen
Posts: 55
Joined: Thu Jun 18, 2015 9:02 am

Re: Problem with scoping (different files)

Post by 0x72 »

One of the ways might be something like that (direct one to one relation between the interface and the menu):

Code: Select all

function Menu:enteredState()
   self.interface = gui.Interface:new(self):loadFromFile("menu/main.lua")
  -- gui.Interface:new(self, "menu/main.lua") ?
end

Code: Select all

local INTERFACE = class("INTERFACE") -- edited (I've wrote {} here, fixing for posterity)

INTERFACE:initialize(menu)
  -- self.menu = menu
  self.button = gui.Button:new("New Game", function()
    menu:enteredState()
  end)
end

return INTERFACE
not sure if it works - what gui and class lib do you use?

--- edit ----
actually you should be able to:

Code: Select all

INTERFACE:initialize(menu)
  self.button = gui.Button:new("New Game", menu:enteredState)
end
Last edited by 0x72 on Thu Jun 25, 2015 7:26 pm, edited 1 time in total.
User avatar
FLÖRGEN
Prole
Posts: 4
Joined: Thu Jun 25, 2015 2:00 am

Re: Problem with scoping (different files)

Post by FLÖRGEN »

Hm, it doesn't seem to be working (EDIT: It cannot index the parent) :/ I am using middleclass and loveframes (with the gui.Component:new() functions being an attempt to neaten the code). Perhaps this is not the best way to be doing this and there is a more elegant way? Anyhow, I uploaded a .love below:
Attachments
game.love
(179.86 KiB) Downloaded 109 times
User avatar
0x72
Citizen
Posts: 55
Joined: Thu Jun 18, 2015 9:02 am

Re: Problem with scoping (different files)

Post by 0x72 »

You have misspelled initialize in interface.lua - when you fix it, it'll work :)
User avatar
FLÖRGEN
Prole
Posts: 4
Joined: Thu Jun 25, 2015 2:00 am

Re: Problem with scoping (different files)

Post by FLÖRGEN »

Thank you so much :D I'm just wondering though, do you think there is a more elegant way to do this or would what I have be good enough?
User avatar
0x72
Citizen
Posts: 55
Joined: Thu Jun 18, 2015 9:02 am

Re: Problem with scoping (different files)

Post by 0x72 »

First of all: It's simple now, I'd move forward.

But if you feel like working on menu today (instead of let's say - gameplay) then instead of menu object (self) you could pass game to the interface, or you could even make game a global variable[1]. The issue there is that you don't know if game is the only thing you might need and there (you actually could pass anything you need there now) and if game has a particular methods when in a particular state.

But, I personally like the idea of state passing everything that interface might need - it's easy to reasoning about the interface when it's related to the state (also parent might be renamed to like data in case you pass there anything else.

not sure if it's elegant but it's simple-and-working

You can also build proper, robust, callback driven event system - please don't go this way - it's a mess.

[1] some people hate global variables and they have good reasons for that, but for a game object I might make an exception :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 6 guests