Jump to content

tables and randoms


DakiLLa

Recommended Posts

hi. i have one problem here... well, i have a table with objects. How to get randomly an object and make with him some operations?

lets suppose something like this: 'board from fallout'

rows = 10 
columns = 10 
board = {} 
  
local count = 0 
function createBoard () 
    for i = 1,rows do 
        for j = 1, columns do 
            board[count] = createObject ( 1697, -2274.5741875 + 4.466064 * j, 1858.0903320313 + 5.362793 * i, 102.6078, math.deg( 0.555 ), 0, 0 ) 
            count = count + 1 
        end 
    end 
end 
addEventHandler ( "onResourceStart", getResourceRootElement ( getThisResource () ), createBoard ) 

and i want to attach a marker to one of these blocks (randomly).

function test () 
    rndBlock = math.random (1, #board) 
    m_block = board[rndBlock] 
    m = createMarker ( 0, 0, 0, "corona", 2.0, randInt(0,255), randInt(0,255), randInt(0,255), 150 ) 
    attachElementToElement ( m, m_block ) 
end 
addCommandHandler ( "test", test ) 

but something wrong, i dont know how to make it..can you help ?

Link to comment

Hmm... I can't really spot any mistakes, but you might want to change the color thingies to, say, a minimum of 100. So, 100 + math.random(1,155), since a marker with 0,0,0 will not be black, but invisible. I don't know how many times you tried, but this can also be the reason you didn't see anything. ;)

Link to comment

well another question. If i want to destroy block then i need to remove it from table as i think cse i have warnings when i try to destroy it again (object destroyed, but not removed from table so, you cant destroy nothing). How to do this ? (with my examples ofcourse, with board { } table..)

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...