Scripts / arrays in loops ? (i searched for it first yes)

Author
Message
rudyardcatling
7
Years of Service
User Offline
Joined: 25th Oct 2017
Playing:
Posted: 3rd Jan 2018 15:21
its easy to work around but im curious

i have a map with a lot of lightpoles and some elecricity boxes that all have a script based on lightswitch but altered to light/extinguish certain zones / streets each separately, all is working but since i'm trying to be as global and library as possible i have

g_firstlight = {}

in a script thats triggered way before any of the boxes are in range

now if i put in the altered script (i have a separate one for each box since they all ahve a range of lights, i made sure the lights are entities that all number up one after another from top of the hill to bottom
so if i put in the script

if blabla condition
if blabla condition

all fulfilled -> for lus = 740,743 hidelight(lus) do
no problem
i have in the init

g_firstlight[e] = 740 (for this particular one ... it would make it easy to copy paste if i just have to alter two number and the function names lightswitch1 lightswitch2 etc...)

then if i put in the _main

local firstlight = g_firstlight[e]
and the rest blabla --> for lus firstlight,743 do
etc ..
then no problem BUT
if i use for lus g_firstlight[e],g_lastlight[e] (or just one of those indexed ones first or last)
i get an error SO
and this is just a question cos it only takes two extra lines of code but i like as few as possible

is it simply not possible to use the g_variable[e] in for loops in GG lua ?

thanks in advance (if anyone)

if it behaves like a cop it might be one

AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 3rd Jan 2018 17:08 Edited at: 3rd Jan 2018 17:10
First of all they are NOT arrays and treating them as such may end in tears.

Secondly, if I am understanding what you are trying to achieve, you should be really using the entity name directly as a key.

So use <script>_init_name( e, name )

Then set your list up using the name and also store the name in another list so you can look it up later.

It will make the code far easier to read and you can make it more generic that way.

Use the generic 'for' to iterate lists and use 'pairs' wherever you have a sparse list. i.e. use the form:

for k, v in pairs( <list> ) do .... end

This will iterate all key value pairs in <list> giving you k = the key and v = value for each one.

For example to search for all entities that are 'active' and have positive 'health' you could do:

for k, v in pairs(g_Entity) do
if v.active == 1 and v.health > 0 then
<do something with the entity>
end
end
Been there, done that, got all the T-Shirts!
PM
Bolt Action Gaming
GameGuru Tool Maker
11
Years of Service
User Offline
Joined: 24th Oct 2013
Location: Harrisburg, PA (USA)
Posted: 3rd Jan 2018 18:16


AmenMoses strikes again! I'm not as well versed but I can definitely point you to at least one useful resource on the subject:
https://www.lua.org/pil/3.6.html
rudyardcatling
7
Years of Service
User Offline
Joined: 25th Oct 2017
Playing:
Posted: 4th Jan 2018 16:28
yagh indeed ... tables hm ? im gonna have to wrap my old head around all those dynamics but (and thanks) (again xD)


this is what i have


it didnt work before but it seems to do now, maybe i overlapped something somewhere due to electricity boxes within range thats hard to say
i already had a working version where i just used the numbers IN the script but this way i only have to alter 2 numbers and the script name/number for every instance i use it on, which is all supreme now
i wont ask you to pour more of your time in it im sure you have plenty to do there but if you have any suggestions, or on how to implement this the way you said (i tried but i dont see how to link it as keyvalues here) or how i could fill the table in the main script using all entity numbers of electricityboxes + relevent lights numbers ... sth like g_table = { e , {740,741,742,...}} ??!?
or maybe this is just the simplest way without much hassle.
If you do have time to spare all opinion and feeback is ofcourse welcome and otherwise thanks again for paying attention
you da man
if it behaves like a cop it might be one

Bolt Action Gaming
GameGuru Tool Maker
11
Years of Service
User Offline
Joined: 24th Oct 2013
Location: Harrisburg, PA (USA)
Posted: 12th Jan 2018 21:12
I'm not entirely sure what you're trying to do but one thing I see is you're storing your lights in separate tables.
Wouldn't you want it to be g_light[firstlight] and g_light[lastlight], with g_light being your table containing the object id's of ALL the lights?

Login to post a reply

Server time is: 2024-12-23 00:34:10
Your offset time is: 2024-12-23 00:34:10