News: Welcome back to Bullworth! If you haven't already, you will need to reset your password..


Author Topic: How to manage array variables?  (Read 1281 times)

0 Members and 1 Guest are viewing this topic.

Offline Histeria Beagles

  • Jr. Member
  • **
  • Posts: 4
  • Gender: Male
    • View Profile
How to manage array variables?
« 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?

Offline Altamurenza

  • Full Member
  • ***
  • Posts: 118
  • Gender: Male
  • I love cheat, unique, realistic, & expansion mod.
    • View Profile
Re: How to manage array variables?
« Reply #1 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 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