Jump to content

Acquire an instance from another .lua file?


Recommended Posts

Question: What's the relationship between lua files when you use use? Can the calling lua file see the "usee" as a local variable, but not the other way round?

 

==========================

 

Backstory (and specific problem):

 

I'm a new to how LUA communicates between files, so I wonder if someone would be able to shine some light on it please

 

Within modmain.lua I've created a straightforward object called checkPoint

 

checkPoint = Class(function(self, inst)    self.inst = inst    self.autoSaveCount = 0    self.firstSave = true    self.checkPointWasWhere = "nil"end)

Simple enough. And within modmain I find that I can (appear to) reference any of the instance variables simply by calling checkPoint.[variable name]

 

However, I'm wondering if/how this can be referenced from another LUA file?

 

I have a file called outcomes.lua which appears to hook into modmain with:

local Outcomes = use "outcomes"

So within modmain: I can now call its methods with Outcomes.[method name]

 

Here's where it gets annoying... there's a method within outcomes which ideally needs to know some of the instance variables of checkPoint. I'm not fussed whether it accesses them directly or simply calls a method in modmain which will complete the logic instead

 

The problem: The method in question isn't called by modmain directly (so it cannot pass the checkPoint instance in as an arg)

 

Instead, the method I want checkPoint in acts independently (it's a listener that reacts on the player's death)

 

So I'm wondering...

 

Is there a way I can make the working instance of checkPoint reachable within outcomes.lua? If so, how?

 

I appreciate it sounds a bit ambiguous... so if you want more info / code - Please don't hesitate to shout

Link to comment
Share on other sites

@silktie,

 

Waiiiit.... what? I know use is used in some other languages as the equivalent of the typical "include" keyword, but to the best of my knowledge there is no such keyword or function in lua.  In order to execute code in other files in lua, one generally uses require, dofile(), or the package system.

 

That said, you seem to be implying that your code doesn't crash and burn on attempting to use "use" O.o  Could you either attach or PM a copy of your mod?

Link to comment
Share on other sites

@Corrosive I think they're using this.

 

@silktie You can pass a function to outcomes.lua from modmain.

 

modmain:

local function do_something()   -- Do something from checkPoint hereendoutcomes.set_function(do_something)

outcomes:

local some_function = function() endlocal function set_function(fn)    some_function = fnendlocal function use_the_function()    some_function()end
Link to comment
Share on other sites

@Corrosive - Ah yes, my apologies: That is indeed the use I'm using use for :-) (way too many use...)

 

@Blueberrys - That's pretty awesome - I forgot you could pass around functions like that! Let me crank up the logging and give it a play about. It still seemed temperamental at first, but that's likely local / global variables at play - So I'll explore the finer details

 

Thanks for the quick replies guys!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...