Bully-Board

Bully Modding Section => Modding Questions/Help => Topic started by: Histeria Beagles on July 26, 2022, 10:22:08 PM

Title: How to manage array variables?
Post by: Histeria Beagles on July 26, 2022, 10:22:08 PM
I want to have multi units instanceable, storing many peds as needed without having to create multiple variables and if sentences for each one. However, I have no idea of how to manage it, I don't know how to return the value of the previous array, once I set a new one.

So, for example, if I store many peds in array ={}, like this:
integer = integer + 1
array[integer] = ped

Whenever I use a condition like PedMePlaying(array[integer], "Default_KEY", true), it will only detect the last stored ped.
How do I get the value of the previous ones?
Title: Re: How to manage array variables?
Post by: Altamurenza on August 01, 2022, 12:27:48 AM
Whenever I use a condition like PedMePlaying(array[integer], "Default_KEY", true), it will only detect the last stored ped.

You can simply put a number to call something from the table (if only the table is indexed with a number like this case).
Code: [Select]
if PedIsValid(TABLE[1]) and PedMePlaying(TABLE[1], 'DEFAULT_KEY') then
  -- do something to the first ped.
end

if PedIsValid(TABLE[3]) and PedMePlaying(TABLE[3], 'DEFAULT_KEY') then
  -- do something to the third ped.
end

How do I get the value of the previous ones?
Code: [Select]
if PedIsValid(TABLE[table.getn(TABLE) - 1]) and PedMePlaying(TABLE[table.getn(TABLE) - 1], 'DEFAULT_KEY') then
  -- do something to the previous ped from the last.
end



However, you can simply manage them all with for loop & ipairs (https://stackoverflow.com/questions/55108794/what-is-the-difference-of-pairs-vs-ipairs-in-lua) function.
Code: [Select]
for ID, PED in ipairs(TABLE) do
  if PedIsValid(PED) and PedMePlaying(PED, 'DEFAULT_KEY') then
    -- do something to all registered pedestrians in the table.
  end
end