Zecrit
|
  |
| Joined: 24 Jan 2013 |
| Total Posts: 2618 |
|
|
| 03 Sep 2014 01:42 PM |
My dictionary:
local EngDictionary = { ["Plasma expulsion exhaust"] = 20, ["Plasma ignition ports"] = 35, ["Spacetime impulse drive"] = 45, ["Spacial elastic catapult"] = 60, ["Quantum slipstream drive"] = 75, ["Spacetime compresser"] = 90, } My print: print(EngDictionary[3]) > Prints nil
Wat. |
|
|
| Report Abuse |
|
|
|
| 03 Sep 2014 01:52 PM |
| You are using a key-value table (similar to a Map in other programming languages). This means that it is not ordered, so EngDictionary[3] or #EngDictionary will not work. You can, however, do this: print(EngDictionary["Plasma expulsion exhaust"]) --> 20 |
|
|
| Report Abuse |
|
|
Zecrit
|
  |
| Joined: 24 Jan 2013 |
| Total Posts: 2618 |
|
|
| 03 Sep 2014 01:56 PM |
That wont work for what I'm doing. How do I order it? |
|
|
| Report Abuse |
|
|
|
| 03 Sep 2014 02:15 PM |
local EngDictionary = { {["Plasma expulsion exhaust"], 20}, {["Plasma ignition ports"], 35}, etc }
print(EngDictionary[1][1]) -- [#][1] for name, [#][2] for number |
|
|
| Report Abuse |
|
|
Zecrit
|
  |
| Joined: 24 Jan 2013 |
| Total Posts: 2618 |
|
|
| 03 Sep 2014 02:26 PM |
Still printing nil.
local EngDictionary = { {["Plasma expulsion exhaust"] = 20}, {["Plasma ignition ports"] = 35}, etc }
print(EngDictionary[1][1]) >Nil
I could swear it was working two days ago. |
|
|
| Report Abuse |
|
|
|
| 03 Sep 2014 02:34 PM |
Oops sorry I did a bit wrong.
{"Plasma expulsion exhaust", 20}, {"Plasma ignition ports", 35},
That is how you put int he elements. Don't forget to add your other ones and get rid of the "etc" |
|
|
| Report Abuse |
|
|
Zecrit
|
  |
| Joined: 24 Jan 2013 |
| Total Posts: 2618 |
|
|
| 03 Sep 2014 02:43 PM |
Wunderbar
One last question :L
My plan is to correlate the name of a value to a dictionary entry. For example:
Value's name is "Spacetime compresser" How would I get it to search for a dictionary entry which is equal to the value's name? |
|
|
| Report Abuse |
|
|
|
| 03 Sep 2014 02:48 PM |
You would do what you did the first time.
local EngDictionary = { ["Plasma expulsion exhaust"] = 20, ["Plasma ignition ports"] = 35, ["Spacetime impulse drive"] = 45, ["Spacial elastic catapult"] = 60, ["Quantum slipstream drive"] = 75, ["Spacetime compresser"] = 90, }
local val = "Plasma expulsion exhaust" local cost = EngDictionary[val] |
|
|
| Report Abuse |
|
|
Zecrit
|
  |
| Joined: 24 Jan 2013 |
| Total Posts: 2618 |
|
|
| 03 Sep 2014 02:54 PM |
Yes, I know. The problem is that when I tried that it didn't work which is how this started. :L |
|
|
| Report Abuse |
|
|
|
| 03 Sep 2014 02:57 PM |
Ah, well then you can do this with the new version of your table:
local find = "Item name to find" for i, item in pairs(EngDictionary) do if item[1] == find then return item[2] -- Return the cost end end
Also, remember that pairs can be used to search through key-value tables, so if finding all the products was your problem you could always use that. |
|
|
| Report Abuse |
|
|
Zecrit
|
  |
| Joined: 24 Jan 2013 |
| Total Posts: 2618 |
|
|
| 03 Sep 2014 03:12 PM |
It repeats the find for the entire dictionary.
local find = engine for i, item in pairs(EngDictionary) do if item[1] == find then return item[2] end addspeed = item[2] -- print(addspeed)
engine == the value with the name in it which I want found in the dictionary It prints all the [2]s, which means it's changing addspeed to all the values as it goes through them. So I'm assuming it either doesn't know what v.Name is or is ignoring it. |
|
|
| Report Abuse |
|
|
|
| 03 Sep 2014 03:20 PM |
local EngDictionary = { ["Plasma expulsion exhaust"] = 20, ["Plasma ignition ports"] = 35, ["Spacetime impulse drive"] = 45, ["Spacial elastic catapult"] = 60, ["Quantum slipstream drive"] = 75, ["Spacetime compresser"] = 90, }
print(EngDictionary["Plasma explusion exhaust"]) |
|
|
| Report Abuse |
|
|
Zecrit
|
  |
| Joined: 24 Jan 2013 |
| Total Posts: 2618 |
|
|
| 03 Sep 2014 03:22 PM |
@Super
Doesn't work, that was the first method. :L |
|
|
| Report Abuse |
|
|
Zecrit
|
  |
| Joined: 24 Jan 2013 |
| Total Posts: 2618 |
|
|
| 03 Sep 2014 03:24 PM |
| I'm going to put us out of our misery and just use another independent value. |
|
|
| Report Abuse |
|
|
| |
|
iiEssence
|
  |
| Joined: 18 Jun 2014 |
| Total Posts: 3467 |
|
|
| 03 Sep 2014 03:26 PM |
local find = engine for i, item in pairs(EngDictionary) do if item[1] == find then return item[2] -- Stops the loop end addspeed = item[2] -- Is still in for loop, so it changes every value until the if condition is met and item[2] is returned. print(addspeed)
Just add an end after the end closing the if statement. |
|
|
| Report Abuse |
|
|
Zecrit
|
  |
| Joined: 24 Jan 2013 |
| Total Posts: 2618 |
|
|
| 03 Sep 2014 03:28 PM |
Because yours would work except what I'm doing is finding the name of a value, making it a variable and then searching for it which is apparently too much for the "best game making studio for beginners on the internet".
|
|
|
| Report Abuse |
|
|
Zecrit
|
  |
| Joined: 24 Jan 2013 |
| Total Posts: 2618 |
|
|
| 03 Sep 2014 03:36 PM |
Thanks iiE
Had to twiddle that a bit but now it works.
THANK YOU EVERYBODY |
|
|
| Report Abuse |
|
|