Protagonist95 Posted February 13, 2016 Share Posted February 13, 2016 Hello guys,stuck on a problem.Heres code i wrote: items = { {1337,1356.69360,1546.84326,10.82031}, {1337,1356.59802,1536.86560,10.8203}, {1337,1368.62585,1547.30847,10.82031} } function spawnLoot() local object ,x, y, z = unpack ( items [ math.random ( #items ) ] ) createObject ( object,x,y,z,0,0,0) end addEventHandler ( "onResourceStart", getRootElement(), spawnLoot) problem is that script spawns only one object.What i want to do,is spawn multiple of them(for example spawn object at first and third point) Thank you) Link to comment
Captain Cody Posted February 13, 2016 Share Posted February 13, 2016 Well that is because this only has one function which will only create one item. Link to comment
Protagonist95 Posted February 13, 2016 Author Share Posted February 13, 2016 Well that is because this only has one function which will only create one item. Any suggestions ? Link to comment
Captain Cody Posted February 13, 2016 Share Posted February 13, 2016 Well what is the purpose of these objects, will they just stay there, move, Etc. Need this info to determine how you'd go about doing this. Link to comment
Protagonist95 Posted February 13, 2016 Author Share Posted February 13, 2016 Well what is the purpose of these objects, will they just stay there, move, Etc. Need this info to determine how you'd go about doing this. It's supposed to be loot for players(1337 and etc is just testing object,after im done with this the model will be changed),so player has to pick it up some how. Link to comment
Captain Cody Posted February 13, 2016 Share Posted February 13, 2016 So do you want it to create all the objects at the same time or a random object? Link to comment
Protagonist95 Posted February 13, 2016 Author Share Posted February 13, 2016 So do you want it to create all the objects at the same time or a random object? The perfect option would be random,because otherwise players will completeley know,where loot is. Link to comment
Captain Cody Posted February 13, 2016 Share Posted February 13, 2016 Object = {} X = {} Y = {} Z = {} Object[1] = 1 X[1] = 1356.69360 Y[1] = 1546.84326,10.82031 Z[1] = 10.82031 -------- Object[2] = 3 X[2] = 1356.69360 Y[2] = 1546.84326,10.82031 Z[2] = 10.82031 ------- Object[3] = 1 X[3] = 1356.69360 Y[3] = 1546.84326,10.82031 Z[3] = 10.82031 ----- ItemCount = 3 function spawnLoot() randomItem = math.random (1, ItemCount) local object ,x, y, z = object[randomItem],X[randomItem],Y[randomItem],Z[randomItem] createObject ( object,x,y,z,0,0,0) end addEventHandler ( "onResourceStart", getRootElement(), spawnLoot) -- Put in random cords and object id, fill in your own -- As you add more objects you increase item account, and increase the number in [ ] Link to comment
Moderators IIYAMA Posted February 13, 2016 Moderators Share Posted February 13, 2016 function deepcopy(orig) local orig_type = type(orig) local copy if orig_type == 'table' then copy = {} for orig_key, orig_value in next, orig, nil do copy[deepcopy(orig_key)] = deepcopy(orig_value) end setmetatable(copy, deepcopy(getmetatable(orig))) else -- number, string, boolean, etc copy = orig end return copy end http://lua-users.org/wiki/CopyTable items = { {1337,1356.69360,1546.84326,10.82031}, {1337,1356.59802,1536.86560,10.8203}, {1337,1368.62585,1547.30847,10.82031} } function spawnLoot() local items = deepcopy(items) -- copy the table, so we can edit it without editing the original. for i=1, math.max(math.random(#items), math.ceil(#items*0.3)) do -- minimal 30% of the items count = 0.3. local randomItemIndex = math.random ( #items ) table.remove(items, randomItemIndex) -- remove it, so you don't duplicate item/spawnpoint. local objectID, x, y, z = unpack ( items[randomItemIndex]) createObject ( objectID,x,y,z,0,0,0) end end addEventHandler ( "onResourceStart", resourceRoot, spawnLoot) -- don't use the ROOTELEMENT! But resourceRoot! Link to comment
Protagonist95 Posted February 13, 2016 Author Share Posted February 13, 2016 function deepcopy(orig) local orig_type = type(orig) local copy if orig_type == 'table' then copy = {} for orig_key, orig_value in next, orig, nil do copy[deepcopy(orig_key)] = deepcopy(orig_value) end setmetatable(copy, deepcopy(getmetatable(orig))) else -- number, string, boolean, etc copy = orig end return copy end http://lua-users.org/wiki/CopyTable items = { {1337,1356.69360,1546.84326,10.82031}, {1337,1356.59802,1536.86560,10.8203}, {1337,1368.62585,1547.30847,10.82031} } function spawnLoot() local items = deepcopy(items) -- copy the table, so we can edit it without editing the original. for i=1, math.max(math.random(#items), math.ceil(#items*0.3)) do -- minimal 30% of the items count = 0.3. local randomItemIndex = math.random ( #items ) table.remove(items, randomItemIndex) -- remove it, so you don't duplicate item/spawnpoint. local objectID, x, y, z = unpack ( items[randomItemIndex]) createObject ( objectID,x,y,z,0,0,0) end end addEventHandler ( "onResourceStart", resourceRoot, spawnLoot) -- don't use the ROOTELEMENT! But resourceRoot! local objectID, x, y, z = unpack(items[randomItemIndex])---------table expected got nil Link to comment
Captain Cody Posted February 13, 2016 Share Posted February 13, 2016 Why would you do all of this when you could just do it a lot simpler? Link to comment
Protagonist95 Posted February 13, 2016 Author Share Posted February 13, 2016 Why would you do all of this when you could just do it a lot simpler? Your code doesn't work properly,it spawns object in one place. Link to comment
Captain Cody Posted February 13, 2016 Share Posted February 13, 2016 Oh you wanted the object to spawn in different locations? I though you wanted certain object to spawn in set locations. Link to comment
Protagonist95 Posted February 13, 2016 Author Share Posted February 13, 2016 Oh you wanted the object to spawn in different locations? I though you wanted certain object to spawn in set locations. in multiple places Link to comment
Captain Cody Posted February 13, 2016 Share Posted February 13, 2016 Oh that's easy, you just remove the math.random from the object, and replace that with the object id. Then it uses random cords but the same object. Link to comment
Moderators IIYAMA Posted February 13, 2016 Moderators Share Posted February 13, 2016 This should do it. items = { {1337,1356.69360,1546.84326,10.82031}, {1337,1356.59802,1536.86560,10.8203}, {1337,1368.62585,1547.30847,10.82031} } function spawnLoot() local items = deepcopy(items) -- copy the table, so we can edit it without editing the original. for i=1, math.max(math.random(#items), math.ceil(#items*0.3)) do -- minimal 30% of the items count = 0.3. local randomItemIndex = math.random ( #items ) local objectID, x, y, z = unpack ( items[randomItemIndex]) table.remove(items, randomItemIndex) -- remove it, so you don't duplicate item/spawnpoint. createObject ( objectID,x,y,z,0,0,0) end end addEventHandler ( "onResourceStart", resourceRoot, spawnLoot) -- don't use the ROOTELEMENT! But resourceRoot! Swapped the table.remove line, with the table unpack line. Link to comment
Protagonist95 Posted February 13, 2016 Author Share Posted February 13, 2016 Oh that's easy, you just remove the math.random from the object, and replace that with the object id. Then it uses random cords but the same object. il try,ty for reply Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now