A Table of Tables that can be randomly selected by a function

Uhh… That may have sounded confusing.

So, I have an attack that looks like the attack you’ll see in 0:11 to 0:14 of this video:

https://www.youtube.com/watch?v=8tBjWfofCqM

And here is the code for that attack:

local lifetime = 50000 local attack = {} function attack.horizontalAttack(blockadeNumber, spaceApart, speed, change) local function moveBlockades(self) if( autoIgnore( "enterFrame", self ) ) then return end if self.x + 15 \> 0 then self.x = self.x - speed end end local function removeBlockades(self) ignore("enterFrame", self) display.remove(self) end for i = 1, blockadeNumber do local blockadeBottom = ssk.display.newRect( nil, right + (i \* spaceApart), bottom, { w = 15, h = 200 + (i \* change), enterFrame = moveBlockades, anchorY = 1, fill = { 0.56, 0.13, 0.94 } }, { isSensor = true } ) local blockadeTop = ssk.display.newRect( nil, blockadeBottom.x, top, { w = 15, h = 200 - (i \* change), enterFrame = moveBlockades, anchorY = 0, fill = { 0.56, 0.13, 0.94 } }, { isSensor = true } ) blockadeTop.timer = removeBlockades timer.performWithDelay(lifetime, blockadeTop) blockadeBottom.timer = removeBlockades timer.performWithDelay(lifetime, blockadeBottom) end end return attack

So, lets say I have a table of tables in a module and I have “required” it at the top of my file. I would want it to do something like this:

for i, blockadeNumber do count = count + 1 --and then in this line: local blockadeBottom = ssk.display.newRect( nil, right + (i \* spaceApart), bottom, --\> { w = 15, h = 200 + table[math.random(1, 10)][count], enterFrame = moveBlockades, anchorY = 1, fill = { 0.56, 0.13, 0.94 } }, { isSensor = true } ) end

Would this work? I haven’t tried this yet, and I am wondering if there is a better way of doing this. If not, please inform me if this would work. Thank you.

I’m not 100% sure what you’re going after, but you can try this code standalone (i.e. NOT in your project)

https://www.youtube.com/watch?v=yQ9xan-IfN0&feature=youtu.be

main.lua

require "ssk2.loadSSK" \_G.ssk.init( { launchArgs = ..., enableAutoListeners = true, exportCore = true, exportColors = true, exportSystem = true, measure = false, --useExternal = true, gameFont = native.systemFont, debugLevel = 0 } ) local physics = require "physics" physics.start() local undertale = require "undertale" local offsets = { 0, 10, 20, 30, 40, 30, 20, 10, 0, -10, -20, -30, -40, -30, -20, 0} local container = undertale.run( nil, offsets, 10, 300 ) -- Hack to delete after 11 seconds transition.to( container, { alpha = 0, delay = 10000, onComplete = display.remove } )

undertale.lua

local undertale = {} function undertale.run( parent, offsets, iterations, speed, randomize ) parent = parent or display.currentStage iterations = iterations or 1 speed = speed or 100 local container = display.newContainer( parent, 300, 300 ) container.x = centerX container.y = centerY local border = ssk.display.newRect( container, 0, 0, { size = 300, strokeWidth = 5, stroke = \_W\_, fill = \_T\_ }) local index = randomize and math.random(1,#offsets) or 1 local count = 0 local maxCount = #offsets \* iterations local width = 20 local tween = 40 local parts = {} while( count \< maxCount ) do local topBlockade = ssk.display.newRect( container, -count \* tween, offsets[index] - 20, { w = width, h = 300, anchorY = 1, fill = \_R\_ }, { isSensor = true, gravityScale = 0 } ) local botBlockade = ssk.display.newRect( container, -count \* tween, offsets[index] + 20, { w = width, h = 300, anchorY = 0, fill = \_G\_ }, { isSensor = true, gravityScale = 0 } ) parts[#parts+1] = topBlockade parts[#parts+1] = botBlockade index = index + 1 if( index \> #offsets ) then index = 1 end count = count + 1 end border:toFront() timer.performWithDelay( 1000, function() for i = 1, #parts do parts[i]:setLinearVelocity( speed, 0 ) end end ) return container end return undertale

Thanks, with this code, I am going to set out to replicate the entire Sans fight!

https://www.youtube.com/watch?v=Vr4IYjeplJA

I am having a problem:

  1. I placed the code for creatingContainers inside a function on my attack.lua file, but everytime I try to call this error appears: 

    ERROR: Runtime error ssk2/extensions/display.lua:21: bad argument #1 to ‘display_newContainer’ (number expected, got nil) stack traceback: [C]: in function ‘display_newContainer’ ssk2/extensions/display.lua:21: in function ‘newContainer’ attack.lua:30: in function ‘createContainer’ attack.lua:107: in function ‘smack’ main.lua:46: in main chunk

This is the code for my createContainer function: 

local function createContainer(width, height) local container = display.newContainer( parent, width, height) container.x = centerX container.y = centerY local border = ssk.display.newRect( container, 0, 0, { w = width, h = height, strokeWidth = 5, stroke = \_W\_, fill = \_T\_ }) end

And this is how I call the function: 

local hitBox = createContainer(500, 500)

I am currently clueless as to how to fix this.

Again… you changed my code.  Go back and look at what I did in my code and pay attention to the ‘parent’ argument.

That is you have this:

local function createContainer(width, height)

I had this:

function undertale.run( parent, offsets, iterations, speed, randomize )

HINT: parent doesn’t exist.

Also, everything you need to start working this out is in the debug message:

ERROR: Runtime error ssk2/extensions/display.lua:21: bad argument #1 to 'display\_newContainer' (number expected, got nil) stack traceback: [C]: in function 'display\_newContainer' ssk2/extensions/display.lua:21: in function 'newContainer' attack.lua:30: in function 'createContainer' attack.lua:107: in function 'smack' main.lua:46: in main chunk

It refers to this line of code, and argument #1 to the function newContainer()… so what is wrong with argument #1?

 local container = display.newContainer( parent, width, height)
  1. Did you read that error message and investigate each piece of code it referred to and then try to figure out what was wrong with argument #1?

If the answer is, “No,” then that should be step 1.  You have all of the code and can add print statements statements to examine arguments and try to figure out what is going on.

  1. Did you get lost when reading the error message?

If, “Yes,” then this is what the message is saying:

An error occurred when  display_newContainer() called newContainer() and 

  • that error occurred on line 21 of ssk2/extensions/display.lua 
  • the problem was with argument #1

Note: While the error was detected there, it may have come from further up the call stack.

Working backward we see:

  • newContainer() was called in createContainer() on line 30 of attack.lua

$10 says the problem is with this call.  In fact I know it is.

Thanks to your help, I was able to figure this out. So… I guess I owe you $10. :slight_smile:

There is another problem. Heh. no surprise here, it is with vectors, but it does not involve bullets or turrets. 

What I am trying to do is something like 6:27 to 6:33 in my second post. For now, I just want that to happen once and here is what I tried. 

function attack.smack(player, target) local hitBox = createContainer(parent, 500, 500) local player local angle = 0 local vec = ssk.math2d.angle2Vector( angle, true ) vec = ssk.math2d.add( target, vec ) player = vec end --this is how it is called in main.lua: local character = ssk.display.newCircle( group, centerX, centerY, { fill = \_R\_, radius = 10 }) physics.addBody(character, "dynamic", {isSensor = true}) local target = display.newRect(1, 1, 0, display.actualContentHeight) attack.smack(character, target)

I know for sure that the problem is with the vector coding I attempted to create in the attack.smack function.

More like this I I should think

--local angle = 0 local vec = ssk.math2d.diff( player, target ) vec = ssk.math2d.normalize(vec)

Thanks, but it still does not do anything. I added a print function to make sure it was being called and it is. This is the code now:

function attack.smack(player, target) local hitBox = createContainer(parent, 500, 500) local vec = ssk.math2d.diff( player, target ) vec = ssk.math2d.normalize(vec) end --how it is called local target = display.newRect(5, 5, 10, 10) attack.smack(character, target)

I tried placing setLinearVelocity for the player, but that did not work neither. And again, I am feeling kind of bad that you are doing all my work again, so if you could just point me in the right direction, I could try to take it from there.

I’m stuck.  I don’t get what you’re trying to do. Sorry.

local impulseStrength = 10 function attack.smack(player, target) local hitBox = createContainer(parent, 500, 500) local vec = ssk.math2d.diff( player, target ) vec = ssk.math2d.normalize(vec) vec = ssk.math2d.scale( impulseStrength \* player.mass ) player:applyLinearImpulse( vec.x, vec.y, player.x, player.y ) end

I’m still really confused by your code.  You’ve got a lot of things mixed together that don’t go together.

I have to ask.  Why are you trying to replicate undertale?  i.e. What is your goal?  Fun or learning?  You can have both, but… if it is learning I’d start with something way simpler.  

If you’re trying to learn about game development you would be better served starting with games that demonstrate basic mechanics:

  • Action-ish:
    • one-player pong
    • two-player pong
    • breakout
    • whack-a-mole
    • asteroids
  • Board-ish:
    • boggle
    • hangman

Its kind of both, but I got a bit too caught up. I will probably start on pong, that would be a good way to get started on 2D Math.

I’m not 100% sure what you’re going after, but you can try this code standalone (i.e. NOT in your project)

https://www.youtube.com/watch?v=yQ9xan-IfN0&feature=youtu.be

main.lua

require "ssk2.loadSSK" \_G.ssk.init( { launchArgs = ..., enableAutoListeners = true, exportCore = true, exportColors = true, exportSystem = true, measure = false, --useExternal = true, gameFont = native.systemFont, debugLevel = 0 } ) local physics = require "physics" physics.start() local undertale = require "undertale" local offsets = { 0, 10, 20, 30, 40, 30, 20, 10, 0, -10, -20, -30, -40, -30, -20, 0} local container = undertale.run( nil, offsets, 10, 300 ) -- Hack to delete after 11 seconds transition.to( container, { alpha = 0, delay = 10000, onComplete = display.remove } )

undertale.lua

local undertale = {} function undertale.run( parent, offsets, iterations, speed, randomize ) parent = parent or display.currentStage iterations = iterations or 1 speed = speed or 100 local container = display.newContainer( parent, 300, 300 ) container.x = centerX container.y = centerY local border = ssk.display.newRect( container, 0, 0, { size = 300, strokeWidth = 5, stroke = \_W\_, fill = \_T\_ }) local index = randomize and math.random(1,#offsets) or 1 local count = 0 local maxCount = #offsets \* iterations local width = 20 local tween = 40 local parts = {} while( count \< maxCount ) do local topBlockade = ssk.display.newRect( container, -count \* tween, offsets[index] - 20, { w = width, h = 300, anchorY = 1, fill = \_R\_ }, { isSensor = true, gravityScale = 0 } ) local botBlockade = ssk.display.newRect( container, -count \* tween, offsets[index] + 20, { w = width, h = 300, anchorY = 0, fill = \_G\_ }, { isSensor = true, gravityScale = 0 } ) parts[#parts+1] = topBlockade parts[#parts+1] = botBlockade index = index + 1 if( index \> #offsets ) then index = 1 end count = count + 1 end border:toFront() timer.performWithDelay( 1000, function() for i = 1, #parts do parts[i]:setLinearVelocity( speed, 0 ) end end ) return container end return undertale

Thanks, with this code, I am going to set out to replicate the entire Sans fight!

https://www.youtube.com/watch?v=Vr4IYjeplJA

I am having a problem:

  1. I placed the code for creatingContainers inside a function on my attack.lua file, but everytime I try to call this error appears: 

    ERROR: Runtime error ssk2/extensions/display.lua:21: bad argument #1 to ‘display_newContainer’ (number expected, got nil) stack traceback: [C]: in function ‘display_newContainer’ ssk2/extensions/display.lua:21: in function ‘newContainer’ attack.lua:30: in function ‘createContainer’ attack.lua:107: in function ‘smack’ main.lua:46: in main chunk

This is the code for my createContainer function: 

local function createContainer(width, height) local container = display.newContainer( parent, width, height) container.x = centerX container.y = centerY local border = ssk.display.newRect( container, 0, 0, { w = width, h = height, strokeWidth = 5, stroke = \_W\_, fill = \_T\_ }) end

And this is how I call the function: 

local hitBox = createContainer(500, 500)

I am currently clueless as to how to fix this.

Again… you changed my code.  Go back and look at what I did in my code and pay attention to the ‘parent’ argument.

That is you have this:

local function createContainer(width, height)

I had this:

function undertale.run( parent, offsets, iterations, speed, randomize )

HINT: parent doesn’t exist.

Also, everything you need to start working this out is in the debug message:

ERROR: Runtime error ssk2/extensions/display.lua:21: bad argument #1 to 'display\_newContainer' (number expected, got nil) stack traceback: [C]: in function 'display\_newContainer' ssk2/extensions/display.lua:21: in function 'newContainer' attack.lua:30: in function 'createContainer' attack.lua:107: in function 'smack' main.lua:46: in main chunk

It refers to this line of code, and argument #1 to the function newContainer()… so what is wrong with argument #1?

 local container = display.newContainer( parent, width, height)
  1. Did you read that error message and investigate each piece of code it referred to and then try to figure out what was wrong with argument #1?

If the answer is, “No,” then that should be step 1.  You have all of the code and can add print statements statements to examine arguments and try to figure out what is going on.

  1. Did you get lost when reading the error message?

If, “Yes,” then this is what the message is saying:

An error occurred when  display_newContainer() called newContainer() and 

  • that error occurred on line 21 of ssk2/extensions/display.lua 
  • the problem was with argument #1

Note: While the error was detected there, it may have come from further up the call stack.

Working backward we see:

  • newContainer() was called in createContainer() on line 30 of attack.lua

$10 says the problem is with this call.  In fact I know it is.