How do you "chop up" the range of a parameter?

I have a spawn function that spawns enemies that move from the left side of the screen to the right at random y values, 100-900 for example. How do I separate this range into smaller ranges, say 100-200, 400-500, and 800-900 for example? I don’t want to just use 3 separate spawns, as this creates a different effect than what I want.

Here is my spawn:
{"_com.ClassCommand",0,0,0,{ type = "spawn", params = { chars = {"ClassEnemy1"}, ranges = { interval = {200,300}, x = {-50,-50}, y = {100,900}, speedX = {"time",3.5,4}, }, } }},[/code]Here is the spawn function within my ClassCommand file:o.spawnObject = function() local p = o.params.params; --local char = p.chars[math.random(#p.chars)]; local char = p.chars[math.random(#p.chars)]; local speedX = math.random () * ( p.ranges.speedX[3] - p.ranges.speedX[2]) + p.ranges.speedX[2]; local x,y,seed,collisionDetectObj,collisionDetectObjX; local object = require(_G.charClass.."."..char).new(); for i=1, 1 do x = math.random (p.ranges.x[1], p.ranges.x[2]); y = math.random (p.ranges.y[1], p.ranges.y[2]); object.x = x; object.y = y; object.speed = speedX; object.isBodyActive = o.isBodiesActive; end return true; end[/code]Any help would be greatly appreciated.Much thanks,Steven [import]uid: 79394 topic_id: 33949 reply_id: 333949[/import]

one way would be to set up each range into a table like this

local range = { { 100,200}, {400,500}, {800,900} }  
  

then using your code above

[code]
{"_com.ClassCommand",0,0,0,{
type = “spawn”,
params = {
chars = {“ClassEnemy1”},
ranges = {
interval = {200,300},
x = {-50,-50},
y = range[math.random( #range )]
speedX = {“time”,3.5,4},
},

}
}},
[import]uid: 7911 topic_id: 33949 reply_id: 134971[/import]

and if you wanted to have it choose from a certain range more then others check out js.lua at http://j-strahan.com/main/?page_id=188 it has code to weight the random selection [import]uid: 7911 topic_id: 33949 reply_id: 134973[/import]

Hi jstrahan, thanks for the response. What I’d ideally like to be able to do is write my spawn using the following format and then alter my ClassCommand file to accomodate these new parameters. Is there a simpler way to accomplish this without the use of tables? I’m not much of a coder, so any thoughts would be greatly appreciated.
{"_com.ClassCommand",0,0,0,{ type = "spawn", params = { chars = {"ClassEnemy1"}, ranges = { interval = {200,300}, x = {-50,-50}, y = {100,200}, {400,500}, {800,900}, speedX = {"time",3.5,4}, }, } }},[/code] [import]uid: 79394 topic_id: 33949 reply_id: 134981[/import]

using your new code above
make this change

[code]
o.spawnObject = function()

local p = o.params.params;

–local char = p.chars[math.random(#p.chars)];
local char = p.chars[math.random(#p.chars)];
local speedX = math.random () * ( p.ranges.speedX[3] - p.ranges.speedX[2]) + p.ranges.speedX[2];

local x,y,seed,collisionDetectObj,collisionDetectObjX;

local object = require(_G.charClass…"."…char).new();

for i=1, 1 do
x = math.random (p.ranges.x[1], p.ranges.x[2]);
select = math.random(#p.ranges.y);
y = math.random (p.ranges.y[select][1], p.ranges.y[select][2]);

object.x = x;
object.y = y;
object.speed = speedX;

object.isBodyActive = o.isBodiesActive;

end

return true;

end [import]uid: 7911 topic_id: 33949 reply_id: 134984[/import]

Hi jstrahan, Once again, thanks for the response. I tried making the change, but I’m getting an error at line 16 that says: attempt to index field ‘?’ (a nil value). I triple checked all my syntax, but there’s no typos. What you wrote makes sense to me, so I’m racking my brain trying to think of what might be the problem. [import]uid: 79394 topic_id: 33949 reply_id: 135130[/import]

Make this change

 y = {{100,200},  
 {400,500},  
 {800,900},}  

Notice extra braces [import]uid: 7911 topic_id: 33949 reply_id: 135134[/import]

Hi jstrahan, yeap, worked like a charm. At the risk of sounding superfluous, you’re a god.

Much thanks,
Steven [import]uid: 79394 topic_id: 33949 reply_id: 135209[/import]

ur welcome [import]uid: 7911 topic_id: 33949 reply_id: 135210[/import]

one way would be to set up each range into a table like this

local range = { { 100,200}, {400,500}, {800,900} }  
  

then using your code above

[code]
{"_com.ClassCommand",0,0,0,{
type = “spawn”,
params = {
chars = {“ClassEnemy1”},
ranges = {
interval = {200,300},
x = {-50,-50},
y = range[math.random( #range )]
speedX = {“time”,3.5,4},
},

}
}},
[import]uid: 7911 topic_id: 33949 reply_id: 134971[/import]

and if you wanted to have it choose from a certain range more then others check out js.lua at http://j-strahan.com/main/?page_id=188 it has code to weight the random selection [import]uid: 7911 topic_id: 33949 reply_id: 134973[/import]

Hi jstrahan, thanks for the response. What I’d ideally like to be able to do is write my spawn using the following format and then alter my ClassCommand file to accomodate these new parameters. Is there a simpler way to accomplish this without the use of tables? I’m not much of a coder, so any thoughts would be greatly appreciated.
{"_com.ClassCommand",0,0,0,{ type = "spawn", params = { chars = {"ClassEnemy1"}, ranges = { interval = {200,300}, x = {-50,-50}, y = {100,200}, {400,500}, {800,900}, speedX = {"time",3.5,4}, }, } }},[/code] [import]uid: 79394 topic_id: 33949 reply_id: 134981[/import]

using your new code above
make this change

[code]
o.spawnObject = function()

local p = o.params.params;

–local char = p.chars[math.random(#p.chars)];
local char = p.chars[math.random(#p.chars)];
local speedX = math.random () * ( p.ranges.speedX[3] - p.ranges.speedX[2]) + p.ranges.speedX[2];

local x,y,seed,collisionDetectObj,collisionDetectObjX;

local object = require(_G.charClass…"."…char).new();

for i=1, 1 do
x = math.random (p.ranges.x[1], p.ranges.x[2]);
select = math.random(#p.ranges.y);
y = math.random (p.ranges.y[select][1], p.ranges.y[select][2]);

object.x = x;
object.y = y;
object.speed = speedX;

object.isBodyActive = o.isBodiesActive;

end

return true;

end [import]uid: 7911 topic_id: 33949 reply_id: 134984[/import]

Hi jstrahan, Once again, thanks for the response. I tried making the change, but I’m getting an error at line 16 that says: attempt to index field ‘?’ (a nil value). I triple checked all my syntax, but there’s no typos. What you wrote makes sense to me, so I’m racking my brain trying to think of what might be the problem. [import]uid: 79394 topic_id: 33949 reply_id: 135130[/import]

Make this change

 y = {{100,200},  
 {400,500},  
 {800,900},}  

Notice extra braces [import]uid: 7911 topic_id: 33949 reply_id: 135134[/import]

Hi jstrahan, yeap, worked like a charm. At the risk of sounding superfluous, you’re a god.

Much thanks,
Steven [import]uid: 79394 topic_id: 33949 reply_id: 135209[/import]

ur welcome [import]uid: 7911 topic_id: 33949 reply_id: 135210[/import]

Hi jstrahan,

I recently checked out the js.lua file you recommended for the js.highWeighted function. My goal was to get my spawn function to return a random y variable weighted high. However, being a noob, I wasn’t able to get it working. I was hoping you could help point out where I went wrong. Here’s what I did:

First, I downloaded the js.lua file, and put it into the library folder of my game (scripts.libs).

Then, I went to my main.lua file and typed: require( “scripts.libs.js” );

Then, to my code above, I changed:

y = math.random (p.ranges.y[select][1], p.ranges.y[select][2]);  

to:

y = js.highWeighted( p.ranges.y[select][1], p.ranges.y[select][2], p.ranges.y[select][3] );  

Then, I changed my y parameters to acommodate the extra parameter that controls the strength of the weighting:

 y = {{100,200},  
 {400,500},  
 {800,900}}  

to:

 y = {{100,200,3},  
 {400,500,3},  
 {800,900,3}}  

Finally, I tried it in the simulator, but I’m getting an error pointing to the y = js.highWeighted… line of my code that says "attempt to index global ‘js’ (a nil value).

Do you know what I’m doing wrong? I’m guessing I’m making a pretty obvious noob error here, so any help would be greatly appreciated.

Much thanks and Happy New Year,
Steven [import]uid: 79394 topic_id: 33949 reply_id: 136924[/import]

when you required the js.lua file did you make it local in main and try to use it from another file.
if so you either need to remove ‘local’ or require it in the fie file you are using it in. [import]uid: 7911 topic_id: 33949 reply_id: 136963[/import]

Hi jstrahan,

I recently checked out the js.lua file you recommended for the js.highWeighted function. My goal was to get my spawn function to return a random y variable weighted high. However, being a noob, I wasn’t able to get it working. I was hoping you could help point out where I went wrong. Here’s what I did:

First, I downloaded the js.lua file, and put it into the library folder of my game (scripts.libs).

Then, I went to my main.lua file and typed: require( “scripts.libs.js” );

Then, to my code above, I changed:

y = math.random (p.ranges.y[select][1], p.ranges.y[select][2]);  

to:

y = js.highWeighted( p.ranges.y[select][1], p.ranges.y[select][2], p.ranges.y[select][3] );  

Then, I changed my y parameters to acommodate the extra parameter that controls the strength of the weighting:

 y = {{100,200},  
 {400,500},  
 {800,900}}  

to:

 y = {{100,200,3},  
 {400,500,3},  
 {800,900,3}}  

Finally, I tried it in the simulator, but I’m getting an error pointing to the y = js.highWeighted… line of my code that says "attempt to index global ‘js’ (a nil value).

Do you know what I’m doing wrong? I’m guessing I’m making a pretty obvious noob error here, so any help would be greatly appreciated.

Much thanks and Happy New Year,
Steven [import]uid: 79394 topic_id: 33949 reply_id: 136924[/import]