Problem with consistently accessing lua tables

Post Reply
Posts: 43
Joined: Fri Dec 01, 2017 20:09
GitHub: zeuner

Problem with consistently accessing lua tables

by zeuner » Post

This is probably easy for the more experienced developers, nonetheless I didn't find an easy answer:

I noticed that the lua scripting is inconsistently accessing data depending on the context where the access is from:

I have some tables in local variables which get initialized from "init.lua".

Now, when I access them from scripts triggered from "ScriptApiServer::on_chat_message", everything works fine.

But when I access the same variables from "ScriptApiEnv::environment_Step" (for example due to "minetest.after"), they end up being used uninitialized.

What causes this, and what is the recommended way to access data tables from all contexts where lua scripting may be used?

Thanks in advance!

User avatar
Posts: 1052
Joined: Wed Sep 24, 2014 17:47
GitHub: NathanSalapat
IRC: NathanS21
In-game: NathanS21
Location: Bigsby Texas

Re: Problem with consistently accessing lua tables

by Nathan.S » Post

Locals are local, so they'll only exist where you have defined them. If you want to use the same variables, with the same data, in multiple areas put them in a global table.
something like this;

Code: Select all

mod.table={['variable'] = value}
I record Minetest videos, Mod reviews, Modding tutorials, and Lets plays.
Check out my website, and brand new Minetest Modding Course

User avatar
Posts: 1507
Joined: Fri Aug 26, 2016 15:36
GitHub: sorcerykid
In-game: Nemo
Location: Illinois, USA

Re: Problem with consistently accessing lua tables

by sorcerykid » Post

If all your related functions are defined within the same script (i.e. init.lua), they can easily share variables that are local to that script. Simply declare the variable(s) as local myvar at the head of the file, rather than within each specific function.

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

Re: Problem with consistently accessing lua tables

by rubenwardy » Post

Note that some variables should not typically be stored between callbacks. ObjectRefs may expire, resulting in their methods returning nil

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest