Lua insert table entry [Solved]

In this forum you will find and post information regarding the modding of Star Wars Battlefront 2. DO NOT POST MOD IDEAS/REQUESTS.

Moderator: Moderators

Post Reply
User avatar
Anakin
Master of the Force
Master of the Force
Posts: 4817
Joined: Sat Sep 19, 2009 11:37 am
Projects :: RC Side Mod - Remastered - SWBF3 Legacy
Location: Mos Espa (germany)

Lua insert table entry [Solved]

Post 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.
Last edited by Anakin on Mon Mar 02, 2015 2:37 pm, edited 1 time in total.
razac920
2nd Lieutenant
2nd Lieutenant
Posts: 365
Joined: Sun Jan 16, 2011 12:42 am

Re: lua inseart table entry

Post 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?
User avatar
Anakin
Master of the Force
Master of the Force
Posts: 4817
Joined: Sat Sep 19, 2009 11:37 am
Projects :: RC Side Mod - Remastered - SWBF3 Legacy
Location: Mos Espa (germany)

Re: lua inseart table entry

Post 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.
razac920
2nd Lieutenant
2nd Lieutenant
Posts: 365
Joined: Sun Jan 16, 2011 12:42 am

Re: lua inseart table entry

Post 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.
User avatar
Anakin
Master of the Force
Master of the Force
Posts: 4817
Joined: Sat Sep 19, 2009 11:37 am
Projects :: RC Side Mod - Remastered - SWBF3 Legacy
Location: Mos Espa (germany)

Re: Lua insert table entry

Post 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.
razac920
2nd Lieutenant
2nd Lieutenant
Posts: 365
Joined: Sun Jan 16, 2011 12:42 am

Re: Lua insert table entry [solved]

Post by razac920 »

Do you mean arrays in c++? Those are entirely different that these "associative arrays/maps" in Lua
User avatar
Anakin
Master of the Force
Master of the Force
Posts: 4817
Joined: Sat Sep 19, 2009 11:37 am
Projects :: RC Side Mod - Remastered - SWBF3 Legacy
Location: Mos Espa (germany)

Re: Lua insert table entry [solved]

Post 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.
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: Lua insert table entry

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