Page 1 of 1

Weird variable bug

PostPosted: Fri Feb 26, 2016 10:46
by webdesigner97
(Sorry for the bad topic name, I have no idea how to call this problem...)

I'm setting up an API for the StreetsMod to make it easier to register all the possible combinations of asphalt and marking nodes: There are two key-value-tables containing the data required for registering the nodes (see api.lua/api.lua#L20[/url]). Using minetest.after(0, ...), it loops over these two tables and registers the nodes in all possible combinations (see api_register_all.lua).

The problem is the automatic generation of a craft recipe. The recipe I pass to the API function contains placeholders ("?") that are replaced by the corresponding node names (see streets_roadmarkings/init.lua)(e.g. streets:asphalt for black asphalt with a line; streets:asphalt:red for red asphalt with a line etc.). This name is stored in a local variable called replacement (see api_register_all.lua).
This is the theory. In practice, this variable shows a behaviour I cannot understand. When I insert it into the craft recipe, the field is always "streets:asphalt", even if replacement has the value "streets:asphalt_red".
maybe this console screenshot can help ypu understand what I mean. I marked the content of replacement and the fields of the craft recipe replaced with this variable in yellow:

Image

I tried to fix this multiple times by deleting the whole code and recreating it from scratch, but the bug keeps appearing. I have no idea how to fix this, so now I'm asking the community.

You can find the whole mod code here: Github

Re: Weird variable bug

PostPosted: Fri Feb 26, 2016 15:03
by blert2112
One problem may be that you can't register stuff in a minetest.after(). All registering must be done at load time not after.

Re: Weird variable bug

PostPosted: Fri Feb 26, 2016 15:20
by webdesigner97
blert2112 wrote:One problem may be that you can't register stuff in a minetest.after(). All registering must be done at load time not after.

Afaik, after(0) is the last chance to register something...

Re: Weird variable bug

PostPosted: Fri Feb 26, 2016 17:43
by blert2112
I don't think so. I could very well be wrong though.
edited for clarity...
After() runs a specified function X seconds after it is called. After() runs during gameplay. Using it as you are is saying 'do this as soon as game play starts'. Depending on other mods that may be doing something with after(), this could mean that your function is getting run right away or a few seconds after game play has started. All registration needs to be done during game setup (mod loading).

Re: Weird variable bug

PostPosted: Fri Feb 26, 2016 18:19
by webdesigner97
If I'm not wrong, 0 means that the Function is executed before the first Server step... Oh, and it does work ;)

Re: Weird variable bug

PostPosted: Fri Feb 26, 2016 18:22
by blert2112
Huh, imagine that.

Possibly try minetest.serialize to print the craft table, instead of write_json(). The API states write_json() is stricter than the lua table, maybe it's having issues with the "_".?

Re: Weird variable bug

PostPosted: Fri Feb 26, 2016 21:26
by webdesigner97
blert2112 wrote:Huh, imagine that.

Possibly try minetest.serialize to print the craft table, instead of write_json(). The API states write_json() is stricter than the lua table, maybe it's having issues with the "_".?

interesting idea, but the result is the same... I can't understand how a variable can suddenly have a different value... Thanks for your ideas so far :)

Re: Weird variable bug

PostPosted: Fri Feb 26, 2016 23:19
by Byakuren
The registered road marking craft tables are shared between every iteration of the replacement-filling loop. After the first iteration, all the ? are replaced with streets:asphalt, so in the next iterations, there are no longer any ? replace. This happens because you reuse the same craft templates each time.

What you should do is for each possible replacement, do a deep copy of the craft template, and then replace the ? in there.

Re: Weird variable bug

PostPosted: Fri Feb 26, 2016 23:26
by webdesigner97
Isn't this what I'm doing here? Every iteration step, it copies the craft from the definition table into craft and does the replacement. Or maybe it#s getting to late and I don't see it... I'll try again tomorrow...

Re: Weird variable bug

PostPosted: Sat Feb 27, 2016 00:18
by Byakuren
webdesigner97 wrote:Isn't this what I'm doing here? Every iteration step, it copies the craft from the definition table into craft and does the replacement. Or maybe it#s getting to late and I don't see it... I'll try again tomorrow...


Table values in Lua are actually references to tables. So you are just copying a reference to the table, so now craft points to the same table that marking_data.craft does.

Re: Weird variable bug

PostPosted: Sat Feb 27, 2016 00:34
by blert2112
Byakuren is correct. For proof just print 'craft' right after you "create" it (line 26), you will see it.
Try this...
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local craft = table.copy(marking_data.craft)


@ Byakuren... I see your name and I think "Byakuya" :)

Re: Weird variable bug

PostPosted: Sat Feb 27, 2016 01:24
by Byakuren
blert2112 wrote:Byakuren is correct. For proof just print 'craft' right after you "create" it (line 26), you will see it.
Try this...
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local craft = table.copy(marking_data.craft)


@ Byakuren... I see your name and I think "Byakuya" :)

I'm not going to say what I think of Bleach, but Byakuren is a Touhou character.

Re: Weird variable bug

PostPosted: Sat Feb 27, 2016 07:19
by webdesigner97
Ah, that could explain everything! I'll try it as soon as I'm back home!

Re: Weird variable bug

PostPosted: Sun Feb 28, 2016 13:15
by webdesigner97
This fixed the problem! Thank you very much for your explanation, now I can understand what was happening!