Local scope variables and dofile

Post Reply
ThorfinnS
Member
Posts: 311
Joined: Mon Feb 25, 2019 22:05
GitHub: ThorfinnS

Local scope variables and dofile

by ThorfinnS » Post

Stripped to essentials

init.lua:

Code: Select all

local x="tim"
dofile("do_x.lua")
do_x.lua:

Code: Select all

if x == "tim" then minetest.log("Stuff")

This throws:

Code: Select all

WARNING[Main]: Undeclared global variable "x" accessed at...
Is there a good way to accomplish what I'm trying to do? Without creating a global, I mean?

User avatar
Pyrollo
Developer
Posts: 385
Joined: Mon Jan 08, 2018 15:14
GitHub: pyrollo
In-game: Naj
Location: Paris

Re: Local scope variables and dofile

by Pyrollo » Post

[ Display Modpack ] - [ Digiterms ] - [ Crater MG ] - [ LATE ]

ThorfinnS
Member
Posts: 311
Joined: Mon Feb 25, 2019 22:05
GitHub: ThorfinnS

Re: Local scope variables and dofile

by ThorfinnS » Post

Thanks!

So for minetest scripting, probably the most straightforward way to deal with it is to use mymod.myvar, rather than hassling with passing local variables as parameters?

User avatar
rubenwardy
Moderator
Posts: 6972
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: Local scope variables and dofile

by rubenwardy » Post

You shouldn't do that with anything private, such as insecure environments
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

ThorfinnS
Member
Posts: 311
Joined: Mon Feb 25, 2019 22:05
GitHub: ThorfinnS

Re: Local scope variables and dofile

by ThorfinnS » Post

What vulnerability are you envisioning?

I'm not seeking to do anything much different than any settingtypes.txt value. Rather than running a codeblock to determine which mods and settings are selected in each individual dofile, each time it's parsed, and keep them all updated, I'd just be moving that block to run once in the init.lua (or only once at load time) and set essentially an environment variable.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests