Jump to content

Get state from table


FuriouZ

Recommended Posts

Hello!

I have problem, i don't know how to set up table..

I need to get engine state randomly from table like 1 = true, 2 = false, but I don't understand how to do it

                local engineStates = { 
                                     [ 1 ] = true, 
                                     [ 2 ] = false, 
                                     }   
                local state = unpack ( engineStates [ math.random ( #engineStates ) ] )                                   
                    setVehicleEngineState ( source, state ) 

Thanks!

Link to comment

You can't unpack a boolean, the unpack function only unpacks tables (if you think in it, it makes complete sense). It should be like this:

setVehicleEngineState ( source, engineStates[ math.random ( #engineStates ) ] ) 

I hope your code isn't designed the way you showed, you shouldn't be defining the table whenever that code is executed, rather define it once in a place that can be accessed by that piece of code. The current way works, but the fact that it works doesn't mean you should do it that way.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...