Jump to content

[Help] Scripting


Midas67

Recommended Posts

Greetings guys,

 

I would like to ask you a question about scripting in avorion, as I am bit new to Lua I am having trouble getting a simple value from table. I'm trying to get a warhead & body by name.

 

Table(C&P from TorpedoGenerator):

local Warheads = {}
table.insert(Warheads, {type = WarheadType.Nuclear,     name = "Nuclear"%_T,        hull = 1,     shield = 1,       size = 1.0, color = ColorRGB(0.8, 0.8, 0.8)})
table.insert(Warheads, {type = WarheadType.Neutron,     name = "Neutron"%_T,        hull = 3,     shield = 1,       size = 1.0, color = ColorRGB(0.8, 0.8, 0.3)})
table.insert(Warheads, {type = WarheadType.Fusion,      name = "Fusion"%_T,         hull = 1,     shield = 3,       size = 1.0, color = ColorRGB(1.0, 0.4, 0.1)})
table.insert(Warheads, {type = WarheadType.Tandem,      name = "Tandem"%_T,         hull = 2.0,   shield = 2.0,     size = 1.5, color = ColorRGB(0.8, 0.2, 0.2), shieldAndHullDamage = true})
table.insert(Warheads, {type = WarheadType.Kinetic,     name = "Kinetic"%_T,        hull = 2.5,   shield = 0.25,    size = 1.5, color = ColorRGB(0.7, 0.3, 0.7), damageVelocityFactor = true})
table.insert(Warheads, {type = WarheadType.Ion,         name = "Ion"%_T,            hull = 0.25,  shield = 3,       size = 2.0, color = ColorRGB(0.2, 0.7, 1.0), energyDrain = true})
table.insert(Warheads, {type = WarheadType.Plasma,      name = "Plasma"%_T,         hull = 1,     shield = 5,       size = 2.0, color = ColorRGB(0.2, 0.8, 0.2)})
table.insert(Warheads, {type = WarheadType.Sabot,       name = "Sabot"%_T,          hull = 3,     shield = 0,       size = 3.0, color = ColorRGB(1.0, 0.1, 0.5), penetrateShields = true})
table.insert(Warheads, {type = WarheadType.EMP,         name = "EMP"%_T,            hull = 0,     shield = 0.025,   size = 3.0, color = ColorRGB(0.3, 0.3, 0.9), deactivateShields = true})
table.insert(Warheads, {type = WarheadType.AntiMatter,  name = "Anti-Matter"%_T,    hull = 8,     shield = 6,       size = 5.0, color = ColorRGB(0.2, 0.2, 0.2), storageEnergyDrain = 50000000})

 

The way I'm trying to get a value from this table:

function addTorpedoes(faction, warhead, body, rarity, tech, amount, velocity, reach, shielddmg, hulldmg)
warhead = Warheads[warhead]
if warhead then
    body = Bodies[body]
    if body then
	rarity = getRarity(rarity or 0)
	if rarity then
			local tech = math.max(1, tonumber(tech) or 6)
        local dps = Balancing_TechWeaponDPS(tech)
			local torpedo = TorpedoTemplate(tech, rarity)

 

It always return false with a implemented debug message that no such warhead was found, and the same thing happens with body.

 

Any help is appreciated.

Link to comment
Share on other sites

I'm assuming your function addTorpedoes() is defined in your own script. I'm not sure how much about Lua you know so I'm also explaining some basic stuff.

 

TorpedoGenerator.lua stores specific functions and variables in TorpedoGenerator (line 8 in my file, v16.2 beta) and returns this table whenever the Lua script is run. This table and its functions and vars can be used in your own scripts by storing it within a variable (i.e torpGen = require("torpedogenerator"). Because the Warheads and Bodies tables are declared as local variables and are not stored in the TorpedoGenerator table, those cannot be accessed without copy-pasting the tables into your own code or modifying TorpedoGenerator.lua.

 

If copy-pasting, you'll need to rewrite the type entries to refer to a returned TorpedoGenerator table's BodyType and WarheadType tables in the Warheads and Bodies tables, or just replace each type with the correct integer (refer to Lines 14 to 52). Or if you're doing more copy-pasting, then just copy-paste the BodyType and WarheadType tables too.

 

The structure of the Warheads and Bodies tables are actually an array of tables indexed numerically from 1 to #Warheads or #Bodies (in Lua #{table} returns the sequential integer size of tables). So if the Warheads table is given like so and I want to get a warhead by name, I can use a for-loop:

 

-- Given a string warhead_name
for i = 1, #Warheads do
    if Warheads[i].name == warhead_name then
        warhead = Warheads[i]
        break
    end
end

 

You can also use pairs() and ipairs() to do the same thing.

 

Also from a glance, I think the problems are with how the Warheads/Bodies tables are defined (and whether the type reference is referenced correctly), and retrieving the warheads/bodies with a string as the index (i.e warhead = Warheads["Nuclear"]). Feel free to ask for clarification, or point out something I'm missing.  :D

Link to comment
Share on other sites

  • 2 weeks later...

Thanks for your reply I really appreciate it. I managed to fix the problem. The problem was the key by which I was supposed search, it should have been the warhead type not the name so a ez quick fix has been done. It's woking all fine a custom torpedo spawner. ;)

 

If anyone is interested pm me, I will send you the Lua file

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...