value is not nil, then is nil

In main.lua:

local windSpeed = 7;  
print("WS: " .. windSpeed); --prints 7, as expected  
kite.update(windSpeed);  

in kite.lua:

function new()  
 local obj = display.newGroup();  
  
 function obj:update(windSpeed)  
 print("windSpeed here: " .. windSpeed) ; --attempt to concatenate local 'windSpeed' (a nil value)  
 end  
  
 return obj;  
end  

Why is windSpeed nil in the update method? [import]uid: 52127 topic_id: 10744 reply_id: 310744[/import]

windSpeed is a local variable not a global. [import]uid: 31262 topic_id: 10744 reply_id: 39048[/import]

Its being passed as a parameter to the function, so when update is processed, “windSpeed” should be local to that function and is a copy of the data passed to it from the stack.

Least that’s the way most languages deal with passed parameters. However Lua might have issues with stack parameter names being the same as local variables in the calling chunk. It shouldn’t though.

[import]uid: 19626 topic_id: 10744 reply_id: 39050[/import]

@aaaron - I don’t think that’s the issue. If I change kite.lua to this:

function new()  
 local obj = display.newGroup();  
  
 function obj:update(mySpeed)  
 print("windSpeed here: " .. mySpeed) ;  
 end  
  
 return obj;  
end  

I still get the same error. Surely there must be a way to pass a parameter into a function in lua… [import]uid: 52127 topic_id: 10744 reply_id: 39069[/import]

@jn19,
just an Observation

You have declared the function as obj:update(param) but are calling it as obj.update(param)

obj:update(param) expands to obj.update(obj,param)
so in your case the object gets passed windspeed and windspeed is assigned nil

hope you get that and it resolves your problem

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 10744 reply_id: 39087[/import]