[solved] require().method()

How does require().method() appear to be - local or global value?

I have actually a file similar to this. I want to include this file into another one, and call the method setRelation(params).
[lua]local Superparents = {}
function Superparents.setRelation( params )
–code
end
return Superparents[/lua]

I have tried to included the above file like this:
[lua]require(“Superparents”).setRelation( Child1, “RootClass” )[/lua]

AND like this:
[lua]local sp = require(“Superparents”)
sp.setRelation( Child1, “RootClass” )[/lua]

…both variations worked, but which one appears to be created locally (local table) and which one globally (global accessible table)? Which one would you prefer?

I’ve digged into the _G variable, but didn’t found my answer, because I saw both times the same happening. It was both times included in _G and looked like so: Superparents { setRelation = "function ..." } [import]uid: 73502 topic_id: 19307 reply_id: 319307[/import]

You should be able to simply do;

[lua]sp = require “Superparents”[/lua]

Then it will be accessible in other scenes which I assume is your intention?

Peach :slight_smile: [import]uid: 52491 topic_id: 19307 reply_id: 74490[/import]

@Peach: :slight_smile: I know that)) I mean I know how I can create a local or global variable that holds my file.

I want the above example to be local. So local sp = require("Superparents") would do that job. But I wanted to know if require("Superparents") is local too? Or is it global?

In other words … is that sp = require "Superparents" equal to that require("Superparents").setRelation( Child1, "RootClass" )??? Are they both global?

thank you :slight_smile: [import]uid: 73502 topic_id: 19307 reply_id: 74540[/import]

I am not familiar with setRelation - where’s this from? (Sorry!)

Local would be [lua]local sp = require (“Superparents”)[/lua]
Global would be [lua]sp = require (“Superparents”)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 19307 reply_id: 74554[/import]

It’s in the first post :smiley:
[lua]local Superparents = {}
function Superparents.setRelation( params )
–code
end
return Superparents[/lua]

I just wanted to know If i can use require(“file”) without assigning it to a variable (not to local not to global, just not assigning at all). Actually that is possible, since it works. But how is that require() treated/handled then by LUA? as global or local include? I mean are those methods from aboves code example global or local then? [import]uid: 73502 topic_id: 19307 reply_id: 74556[/import]

Sorry, as it was a day between coming back after the first post I was a little jumbled.

It is possible, yes - it would be treated as global I believe.

Peach :slight_smile: [import]uid: 52491 topic_id: 19307 reply_id: 74615[/import]

Thank you so much for your endurance with my questioning, peach :slight_smile: I just like to be always sure about what my code do and question everything :smiley:

Thank you again! [import]uid: 73502 topic_id: 19307 reply_id: 74643[/import]

Hey,

No worries at all - sorry getting an answer was slowed down by my being a bit dense in the second post :wink:

Peach :slight_smile: [import]uid: 52491 topic_id: 19307 reply_id: 74665[/import]