DVader wrote: "Avram, yeah I was conscious of all those variables starting to look a little messy. I have looked into tables a little as I wanted to make this code way more automated. However, I can't see how I can do that yet as there is no way of using the table in an effective way as yet to my knowledge. Still I am new as new can be to Lua and only understand it at all because I have used AGK etc in the past."
I'm not sure how you can't see the benefit of tables. You can do pretty much everything with tables: make arrays, hash tables (key->value storage) or classes for OOP development. For example, you could replace all your variables in global.lua with one (global) table, and then add whatever variable (and it's value) you want into that one table. If you have seen
my aTimer class, you can see there how you can avoid messy code by using tables as classes.
DVader wrote: "Obviously if I made this in say AGK, I would know the id's of each object and could store any relevant data into a array. Then it would be simply a matter of retrieving it when you interact with that object ID. But here I have no clue apart from when you are interacting with each object."
All (dynamic) entities are stored in g_Entity table (as an array), but I don't know if their indexes change with each game run. I'm currently working on a entity class that will allow easier manipulation of entities within LUA. For example, this is how you can create entity object and get it's distance from the player.
function script_main(e)
local this = aEntity:new(e) -- create new entity object for this entity (e)
local dist = this:player_distance() -- get distance between this entity and the player
if dist < 100 then
Prompt("Player distance from this entity is " .. dist)
end
end
DVader wrote: "To make it mostly automatic, I need to have a property set on each plant type for exactly which it is. If I could do that, it would be easy to convert all this info into an array and make it way better! Unfortunately, I can't see any way to do this yet."
My Entity class will support fetching entities by their name:
function script2_main(e)
local this = aEntity:new(e)-- create new entity object for this entity (e)
local other_ent = aEntity.get_by_name('other entity') -- create new entity object for another entity based on it's name
local dist = this:player_distance() -- get distance between this entity and the player
local ent_dist = this:distance(other_ent) -- get distance between two entities!
if dist < 100 then
Prompt("Distance from this entity and " .. other_ent:attr("name") .. " is " .. ent_dist)
end
end
Unfortunately, at this time, g_Entity is not populated with entity names from FPSC editor, so all entities (that are going to be fetched by name) need to have this line in their main script:
local this = aEntity:new(e, 'other entity')
This will create object for that particular entity and give it a name (if it doesn't have one yet). You can then retreive it's name with this:attr('name') or you can read whatever is in g_Entity array with this:attr('attr_name') or set new attribute to an entity with this:attr('attr_name', 'attr_value')
I don't know if this is worth releasing yet, as you can't do anything useful except getting the distance between entities. We need more control over game in LUA!
p.s. Sorry for poisoning the thread. This may be split into separate thread.