Hello all,
I had a couple questions about using Lua modules in Defold.
- First, is this the recommended way of handling read-only data structures? I try to keep the data structure in a local table and return the individual items through functions. Or would this be a case for using the global scope?
local M = {}
local weapons = {
{
hash = hash("melee"),
name = "Melee",
sprite_id = "weapon-scalpel",
fire_sound = "melee-swing",
ammo_type = hash("melee"),
player_props = {
fire_interval = 0.5
},
enemy_props = {
fire_interval = 1
}
},
{
hash = hash("rifle"),
name = "Rifle",
sprite_id = "weapon-rifle",
fire_sound = "rifle-fire",
ammo_type = hash("bullet"),
player_props = {
fire_interval = 1,
magazine_size = 20,
reload_time = 1.5,
shot_speed = 940,
shot_damage = 3,
shot_range = 300,
shot_spread = 10,
shots = 1
},
enemy_props = {
fire_interval = 1,
shot_speed = 940,
shot_damage = 1,
shot_range = 300,
shot_spread = 10,
shots = 1
}
},
{
hash = hash("shotgun"),
name = "Shotgun",
sprite_id = "weapon-shotgun",
fire_sound = "shotgun-fire",
ammo_type = hash("bullet"),
player_props = {
fire_interval = 0.8,
magazine_size = 10,
reload_time = 1.2,
shot_speed = 800,
shot_damage = 2,
shot_range = 200,
shot_spread = 10,
shots = 7
},
enemy_props = {
fire_interval = 1,
shot_speed = 800,
shot_damage = 2,
shot_range = 160,
shot_spread = 10,
shots = 5
}
},
}
function M.get_by_hash(hash)
for k,v in pairs(weapons) do
if v.hash == hash then
return v
end
end
return nil
end
function M.get_by_index(index)
return weapons[index]
end
return M
- Second question: is there a way to callback to a gameobject script from a Lua Module? For example, if I wanted to attach a function called “fire_weapon” to my weapons in the module and have it trigger the bullet spawning from the enemy/player scripts - is that possible?
Thank you!