Page 1 of 1

Lua insert table entry [Solved]

Posted: Sat Feb 28, 2015 7:25 am
by Anakin
Hi,

i want to inseart some entries in an table, but all my tries failed. So this is the table:

Code: Select all

this.planetValue = {
			["cor"] = { victory = 60, defeat = 20, turn = 3 },
			["dag"] = { victory = 50, defeat = 20, turn = 3 },
			["fel"] = { victory = 50, defeat = 20, turn = 3 },
			["geo"] = { victory = 100, defeat = 35, turn = 10 },
			["hot"] = { victory = 60, defeat = 20, turn = 3 },
			["kas"] = { victory = 50, defeat = 20, turn = 3 },
			["kam"] = { victory = 100, defeat = 35, turn = 10 },
			["mus"] = { victory = 60, defeat = 20, turn = 3 },
			["myg"] = { victory = 50, defeat = 20, turn = 3 },
			["nab"] = { victory = 60, defeat = 20, turn = 3 },
			["pol"] = { victory = 50, defeat = 20, turn = 3 },
			["tat"] = { victory = 50, defeat = 20, turn = 3 },
			["uta"] = { victory = 60, defeat = 20, turn = 3 },
			["yav"] = { victory = 50, defeat = 20, turn = 3 },
		}
and these are the entries:

Code: Select all

["bes"] = { victory = 60, defeat = 20, turn = 3 },
["rhn"] = { victory = 50, defeat = 20, turn = 3 },

Code: Select all

this.planetMission = {
			["cor"] = { ["con"] = "cor1r_con", },
			["dag"] = { ["con"] = "dag1r_con", },
			["fel"] = { ["con"] = "fel1r_con", },
			["geo"] = {	["con"] = "geo1r_con", },
			["hot"] = {	["con"] = "hot1r_con", },
			["kam"] = {	["con"] = "kam1r_con", },
			["kas"] = {	["con"] = "kas2r_con", },
			["mus"] = {	["con"] = "mus1r_con", },
			["myg"] = { ["con"] = "myg1r_con", },
			["nab"] = { ["con"] = "nab2r_con", },
			["pol"] = {	["con"] = "pol1r_con", },
			["tat"] = {	["con"] = "tat2r_con", },
			["uta"] = {	["con"] = "uta1r_con", },
			["yav"] = {	["con"] = "yav1r_con", },
		}

The entries look the same way as the stock example above.

I want to add them optionaly, so i cannot simply add them to the list.

Re: lua inseart table entry

Posted: Sat Feb 28, 2015 10:26 am
by razac920
I know nothing about Galactic Conquest, but why don't you just use an if-block to define this.planetValue to have the 2 extra extras if it meets a condition, and not otherwise, rather than modifying it after it's been created?

Re: lua inseart table entry

Posted: Sat Feb 28, 2015 10:36 am
by Anakin
Because this if else would be double length from the whole script.
I need one table for these cases:
only BCC
only BPF
only RVH
only RVC
RVH + RHV
RVH + BPF
RVH + BCC
.
.
.


It's much easier to search for any bespin installation, add bespin. next search for any rhen vari installation, add rhen var. Next you look what maps are installed and modify the entries.

Re: lua inseart table entry

Posted: Sat Feb 28, 2015 10:55 am
by razac920
Would this help?

Have you tried this?

Code: Select all

(this.planetMission).bes = { victory = 60, defeat = 20, turn = 3 }
(this.planetMission).rhn = { victory = 50, defeat = 20, turn = 3 }
Syntactically, I think this is correct. However, I have no idea if you can (safely) add maps/missions/planets in the middle of a Galactic Conquest.

Re: Lua insert table entry

Posted: Mon Mar 02, 2015 2:37 pm
by Anakin
Yes it's working this way

Code: Select all

if .. or .. then
    this.planetMission["bes"] = { victory = 60, defeat = 20, turn = 3 }
end

if .. or .. then
    this.planetMission["rhn"] = { victory = 50, defeat = 20, turn = 3 }
end
i couldn't belive it is that easy. normaly i program with c++ and there you'll get many errors if you try to acces an table's position that doesn't exsits. But for lua it seams to work.

Re: Lua insert table entry [solved]

Posted: Mon Mar 02, 2015 2:47 pm
by razac920
Do you mean arrays in c++? Those are entirely different that these "associative arrays/maps" in Lua

Re: Lua insert table entry [solved]

Posted: Mon Mar 02, 2015 2:52 pm
by Anakin
razac920 wrote:Do you mean arrays in c++? Those are entirely different that these "associative arrays/maps" in Lua
yes i mean arrays, but if you take a vector you couldn't simply acces an position that does not exist. First you need to increase the vector and than you can add something.

Re: Lua insert table entry

Posted: Wed Mar 04, 2015 1:04 pm
by [RDH]Zerted
Anakin wrote:i couldn't belive it is that easy. normaly i program with c++ and there you'll get many errors if you try to acces an table's position that doesn't exsits. But for lua it seams to work.
Yes, Lua is awesome like that. You could also save a few characters and do it like:

Code: Select all

this.planetMission.bes = { victory = 60, defeat = 20, turn = 3 }
I really miss that shortcut when writing Python.

Lua tables have a metatable which has getters and setters for the table. When you access an element it goes though one of those function. By default they'll create the entries as needed. Moving through an extra function for each access is slightly slower, so there's also raw get and set methods that bypass the metatable when higher performance is needed.