Weird variable bug

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

Weird variable bug

by webdesigner97 » Fri Feb 26, 2016 10:46

(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
 

blert2112
Member
 
Posts: 244
Joined: Sat Apr 25, 2015 04:05
GitHub: blert2112

Re: Weird variable bug

by blert2112 » Fri Feb 26, 2016 15:03

One problem may be that you can't register stuff in a minetest.after(). All registering must be done at load time not after.
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

Re: Weird variable bug

by webdesigner97 » Fri Feb 26, 2016 15:20

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...
 

blert2112
Member
 
Posts: 244
Joined: Sat Apr 25, 2015 04:05
GitHub: blert2112

Re: Weird variable bug

by blert2112 » Fri Feb 26, 2016 17:43

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).
Last edited by blert2112 on Fri Feb 26, 2016 18:21, edited 2 times in total.
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

Re: Weird variable bug

by webdesigner97 » Fri Feb 26, 2016 18:19

If I'm not wrong, 0 means that the Function is executed before the first Server step... Oh, and it does work ;)
 

blert2112
Member
 
Posts: 244
Joined: Sat Apr 25, 2015 04:05
GitHub: blert2112

Re: Weird variable bug

by blert2112 » Fri Feb 26, 2016 18:22

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 "_".?
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

Re: Weird variable bug

by webdesigner97 » Fri Feb 26, 2016 21:26

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 :)
 

Byakuren
Member
 
Posts: 441
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri

Re: Weird variable bug

by Byakuren » Fri Feb 26, 2016 23:19

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.
Every time a mod API is left undocumented, a koala dies.
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

Re: Weird variable bug

by webdesigner97 » Fri Feb 26, 2016 23:26

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...
 

Byakuren
Member
 
Posts: 441
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri

Re: Weird variable bug

by Byakuren » Sat Feb 27, 2016 00:18

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.
Every time a mod API is left undocumented, a koala dies.
 

blert2112
Member
 
Posts: 244
Joined: Sat Apr 25, 2015 04:05
GitHub: blert2112

Re: Weird variable bug

by blert2112 » Sat Feb 27, 2016 00:34

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" :)
 

Byakuren
Member
 
Posts: 441
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri

Re: Weird variable bug

by Byakuren » Sat Feb 27, 2016 01:24

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.
Every time a mod API is left undocumented, a koala dies.
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

Re: Weird variable bug

by webdesigner97 » Sat Feb 27, 2016 07:19

Ah, that could explain everything! I'll try it as soon as I'm back home!
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

Re: Weird variable bug

by webdesigner97 » Sun Feb 28, 2016 13:15

This fixed the problem! Thank you very much for your explanation, now I can understand what was happening!
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 6 guests

cron