Global variables

mathuin

Chieftain
Joined
Oct 30, 2010
Messages
7
I am nearly finished with my first mod: a transformation of the technology tree traversal from depth-first to breadth-first. Clicking on "Mathematics" will queue up Animal Husbandry, then Archery, then The Wheel as opposed to the old way which was Animal Husbandry, then The Wheel, then Archery. Trust me, it's something that's bugged me since release and I'm excited to fix it. Anyway. :-)

I've currently put all my new code into a new Lua file called BreadthFirst.lua which is referenced near the beginning of both TechTree.lua and TechPopup.lua with an include statement. Other than the include statement, the only other changes to the other two Lua files are the hooks into my code.

The code works almost perfectly. The intermediate technologies are chosen in the correct order, and automatically process in order. The tech tree displays the little numbers next to the technologies and everything. However, that same information isn't seen in the tech popup -- and changing technologies in the tech popup leaves the tech tree page still displaying the old queue numbers. I am pretty sure it's because I have two separate "global" variables, one for the tech tree and one for the tech popup. How do I reference a truly global variable from two different Lua files?

Jack.
 
a global variable can be called from any of those files, as long as all files are being included

if the variable has the same name, then only the variable from the last file will exist (can only be one variable name). if any of the variables are declared with local, for example:

local myvariable

then that variable's scope is to the file only, and other files can have the same name no prob. not sure if there is a way to call variables from specific files, maybe if lua filesnames were treated as tables
 
So both TechTree.Lua and TechPopup.lua have the line 'include("BreadthFirst");' near the top of each file. BreadthFirst.lua doesn't actually declare the variable -- named BF_g_TechPreReqList -- I think -- it just refers to the variable.

I added a declaration 'BF_g_TechPreReqList = {};' outside of all the functions in BreadthFirst.lua to see if that made a difference and it didn't. Here's the test scenario I used: I opened the tech popup and selected Pottery and it was bubbly and that was normal behavior. I went into the tech tree next, and it looked right. I selected mathematics and that acted the way I wanted it to act -- Animal Husbandry was bubbly, Archery got a 1, The Wheel got a 2, and Mathematics got a 3. I then went back to the tech popup, and it was wrong. Animal Husbandry was still bubbly which is good, but Pottery had a 0 and Archery had no number -- which tells me the tech popup version of BF_g_TechPreReqList was unchanged by the changes I made in the tech tree version. Ugh! I want to refer to a single variable here, why is it so hard? :-)

Jack.
 
that's a lot of bubbly are you celebrating something? :D

maybe you can put up your changes might be easier to help, i got lost in your last explanation
 
Heh! By bubbly I meant the blue with the bubbles in the background that's a sign of the technology being studied.

Tonight I'll take a bunch of screenshots and post them along with my code.

Jack.
 
First, a couple of screenshots to show the point of my mod. DepthFirst.png is the stock tree traversal. Note that the game queues technologies from left to right before moving down. BreadthFirst.png is the way I like it. Note that the technology queue goes down before going to the right. it's not a big deal when going for Mathematics but trust me it is a big deal when going for say Globalization.

The actual code is in BreadthFirst.lua (there's a .txt so I could upload it) and the diffs with stock TechTree.lua and TechPopup.lua are there as TechTree.lua-diff.txt and TechPopup.lua-diff.txt.

BFBug.png shows the bug I'm trying to fix. To recreate the bug using my addon, start a game with the addon enabled. Build that first city, and open the tech tree via the popup. Select Mathematics. See the one true way to traverse the tree as shown in BreadthFirst.png :-) and then close the tech tree. Open the tech popup and select Pottery. What *should* happen is the BF_g_TechPreReqList table variable should be set to a single entry for TECH_POTTERY, but that only seems to happen to the tech popup's version of the variable. This can be seen by opening the tech tree where what you see is what is seen in BFBug.png.
 

Attachments

  • DepthFirst.png
    DepthFirst.png
    233.2 KB · Views: 149
  • BreadthFirst.png
    BreadthFirst.png
    217.6 KB · Views: 110
  • BFBug.png
    BFBug.png
    221.7 KB · Views: 205
  • BreadthFirst.lua.txt
    BreadthFirst.lua.txt
    4.8 KB · Views: 102
  • TechTree.lua-diff.txt
    TechTree.lua-diff.txt
    990 bytes · Views: 86
  • TechPopup.lua-diff.txt
    TechPopup.lua-diff.txt
    437 bytes · Views: 68
i think your problem is that each include in techtree and techpopup makes it so they each have their own BF_g_TechPreReqList thats populated, in this case the queue has up to math + pottery

namespace in lua is different from what im used to.. so not really sure on the syntax to access the variables from include files, but i guess if you could make sure both versions of the tables are always exact copies you'd be fine.

did you try just using these functions instead??

player:ClearResearchQueue
player:PushResearch
player:PopResearch
player:GetLengthResearchQueue

the queue should function like any array
 
i think your problem is that each include in techtree and techpopup makes it so they each have their own BF_g_TechPreReqList thats populated, in this case the queue has up to math + pottery

namespace in lua is different from what im used to.. so not really sure on the syntax to access the variables from include files, but i guess if you could make sure both versions of the tables are always exact copies you'd be fine.

did you try just using these functions instead??

player:ClearResearchQueue
player:PushResearch
player:PopResearch
player:GetLengthResearchQueue

the queue should function like any array

I didn't even know those functions existed. How did you find them? With those it'll be trivial to make my magical widget work. :-)

Jack.
 
Back
Top Bottom