Log In  

I'm having an issue where it seems like I absolutely cannot pass an argument to a function, no matter what I do.

For starters, I am using doc_robs' famous hitbox function:

  function player_hit(x1,y1,w1,h1,
                      x2,y2,w2,h2)
   local hit=false
   local xs=w1/2+w2/2
   local ys=h1/2+h2/2
   local xd=abs((x1+(w1/2))-(x2+(w2/2)))
   local yd=abs((y1+(h1/2))-(y2+(h2/2)))
   if xd<xs and yd<ys then
    hit=true
   end
   return hit
  end

So far, so good, right? Then I pass the following arguments along to it from another function:

  if player_hit(p1.x,p1.y,pw,ph,star1.x,star1.y,sw,sh) then
  --stuff happens in here
  end

All of the values in the preceding if statement are defined at _init(). The values pw, ph, sw, and sh are all 2, defined as global variables. The p1 and star1 values come from tables.

But every time I run my program, this error occurs:

RUNTIME ERROR
   local xs=w1/2+w2/2
Attempt to perform arithmetic on local 'w1' (a nil value)

Now, I can't find the reason why w1 registers as nil. The value pw (2) should be subbed in for it, since it's being provided in the function call.

Strangely, the same error occurs even if I manually type "2" in place of pw in the player_hit() call.

P#74058 2020-03-19 23:20

:: Spaz48

This isn't really enough information to really solve it on its own, from what I can tell, but here's a couple things to try.

If you type "resume" after you get the error, does the cart play fine afterwards without throwing the same error again? If so, you've either got something in the wrong order, so the code isn't setting the variable before it calls that function, or you didn't define the variable in _init() when you should have.

if that's not it, try putting
if debug!=nil then print(debug,0,0,7)

at the end of _draw(), with whatever coordinates and color would be best, and put
debug=pw..","..sw

or whatever variables you think might be problematic directly before the function call that throws an error. And if it throws the error on the very first frame, you can use "?debug" in the terminal to see the result that way.

Hopefully this helps.

P#74067 2020-03-20 02:00

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2020-05-31 07:10 | 0.008s | 4194k | Q:11