Loading...
  OR  Zero-K Name:    Password:   

Spring.GetFeatureResources()

8 posts, 608 views
Post comment
Filter:    Player:  
sort

4 years ago
I want to call Spring.GetFeatureResources() from a widget to see whether a caretaker is reclaiming metal or energy. From the uses in the codebase (e.g. https://github.com/ZeroK-RTS/Zero-K/blob/master/LuaUI/Widgets/gui_chili_selections_and_cursortip.lua#L1818), and also from https://github.com/spring/spring/blob/develop/rts/Lua/LuaSyncedRead.cpp#L4796 it looks like that function should return 6 values. However, when I call it I just get one value back.

My code reads:
if Spring.ValidFeatureID(featureID) then
   local featureResources = Spring.GetFeatureResources(featureID)
   Log("resources=", tostring(featureResources))
   Log("resources type=", type(Spring.GetFeatureResources(featureID)))
end


which results in output like:
[f=0001009] [uapn] resources=75
[f=0001009] [uapn] resources type=number


I would expect the type to be table. It looks like the GetFeatureResources() that I'm calling is different from what everybody else gets. What am I missing?
+1 / -0


4 years ago
You'll probably need to write

local featureMetal, defFeatureMetal, featureEnergy, defFeatureEnergy, reclaimLeft, reclaimTime = Spring.GetFeatureResources(featureID)


Feel free to replace values you don't need with '_'.
+0 / -0

4 years ago
Huh. That works. I guess I don't understand how lua works, but I'm inferring that {a=foo()} puts the first item of the list returned by foo() into a. (I'm more used to python where a=foo() will put the list in a, and a,b,c=foo() will only work if the list is 3 items long and puts each item into a, b, and c respectively.)

Thank you.
+0 / -0


4 years ago
Yeah, Lua only handles multiple return values as a series of vars, rather than a tuple that can be split up at the call site dynamically.
+0 / -0


4 years ago
A better way to see what type of resource a Caretaker is reclaiming would be to read the Caretaker's income directly.
+1 / -0

4 years ago
Reading caretaker income is even better! What function tells me that?
+0 / -0


4 years ago
GetUnitResources or something. Look at the wiki page.
+0 / -0

4 years ago
Found it:
{local metalMake, metalUse, energyMake, energyUse = Spring.GetUnitResources(unitID)}

Works beautifully.
+0 / -0