Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog

ShiVa3D

Return to Game programming

attempt to compare nil with number  [SOLVED]

All about the StoneScript

attempt to compare nil with number

Postby Guinner » 18 May 2012, 23:48

So whenever i'm running my code i'm getting this:
Code: Select all
[+ Warning ] {Scripting         }---------------------------------------------------------------
[+ Warning ] {Scripting         }AI Runtime error : [Handler] EnemyTestAI.onEnterFrame (line 30): attempt to compare nil with number
[+ Warning ] {Scripting         }---------------------------------------------------------------
[+ Warning ] {Scripting         }AI Stack Traceback :
[+ Warning ] {Scripting         }    [Line  30] : Handler 'onEnterFrame'
[+ Warning ] {Scripting         }---------------------------------------------------------------


I did some research and the main problem behind this usually is not declaring the variable. But I have this under enterframe:
Code: Select all
if (hVariable < 0) then
hud.setLabelText ( hComponent, "DEAD!")
end


And this under onInit:
Code: Select all
application.setCurrentUserEnvironmentVariable ( "health", "100" )

So I know i've declared it.
What could be going on?
Guinner
Junior Boarder
Junior Boarder
 
Posts: 36

Re: attempt to compare nil with number

Postby dtr1au » 19 May 2012, 00:13

Hi Guinner,

Did you try remove the quotes around the value for health when setting it?

So instead of:
application.setCurrentUserEnvironmentVariable ( "health", "100" )

Use:
application.setCurrentUserEnvironmentVariable ( "health", 100 )


With the quotes the variable type becomes a string, you probably want it to be a number, remove the quotes and it will be that. It may mess up other areas of you game though if you've built other things that depend on health being a string. You will need to convert the data type to either a string or number wherever you need it. Look at all the string functions in the documentation to determine what you want to do.

Hope that helps.
dtr1au
Platinum Boarder
Platinum Boarder
 
Posts: 375
Location: ACT, Australia

Re: attempt to compare nil with number

Postby Guinner » 19 May 2012, 01:04

Hey dtr1au,
Thanks for the quick reply, however that hasn't seemed to fix the problem. It is still the same error.
Guinner
Junior Boarder
Junior Boarder
 
Posts: 36

Re: attempt to compare nil with number

Postby dtr1au » 19 May 2012, 01:09

Can you post how hVariable is being declared?
dtr1au
Platinum Boarder
Platinum Boarder
 
Posts: 375
Location: ACT, Australia

Re: attempt to compare nil with number

Postby Guinner » 19 May 2012, 01:11

Code: Select all
local hVariable = application.getCurrentUserAIVariable ("EnemyTestAI", "health" )
Guinner
Junior Boarder
Junior Boarder
 
Posts: 36

Re: attempt to compare nil with number

Postby neilb » 19 May 2012, 01:33

You appear to be setting an environment variable called Health, then retrieving the value from an AI variable also called Health which wouldn't have been initialised.

You'd want to set the variable using application.setCurrentUserAIVariable
neilb
Platinum Boarder
Platinum Boarder
 
Posts: 463
Location: Adelaide, South Australia

Re: attempt to compare nil with number

Postby Guinner » 19 May 2012, 04:17

Code: Select all
local hVariable = application.setCurrentUserEnvironmentVariable ( "health", 100 )


if (hVariable < 0) then
hud.setLabelText ( hComponent, "DEAD!")
end


So i've set it up like so, with still no success. Same error :/
Guinner
Junior Boarder
Junior Boarder
 
Posts: 36

Re: attempt to compare nil with number

Postby neilb » 19 May 2012, 04:35

Use getCurrentUserEnvironmentVariable to read the value, you've got setCurrentUserEnvironmentVariable in the code you pasted.
neilb
Platinum Boarder
Platinum Boarder
 
Posts: 463
Location: Adelaide, South Australia

Re: attempt to compare nil with number

Postby Guinner » 19 May 2012, 05:28

OnInit:
Code: Select all
application.setCurrentUserAIVariable ( "EnemyTestAI", "health", 100)


OnEnterFrame:
Code: Select all
local vValue = application.getCurrentUserAIVariable ( "EnemyTestAI", "health" )

if (vValue < 0) then
hud.setLabelText ( hComponent, "DEAD!")
end
----------


And i've created it in the explorer as a number. Still no bingo.
Guinner
Junior Boarder
Junior Boarder
 
Posts: 36

Re: attempt to compare nil with number

Postby neilb » 19 May 2012, 05:57

Is EnemyTestAI added as a main AI to the project - ie. not as a resource, but an AI in the main AI's list?

Have you tried stepping through the code to confirm that the onInit code is actually being called before the onEnterFrame?
neilb
Platinum Boarder
Platinum Boarder
 
Posts: 463
Location: Adelaide, South Australia

Re: attempt to compare nil with number

Postby broozar » 19 May 2012, 17:34

wrong parameter count.

OnInit:
Code: Select all
application.setCurrentUserEnvironmentVariable ( "health", 100)

not ( "EnemyTestAI", "health", 100). the function is designed to take 2 parameters (name and value), not 3.

OnEnterFrame:
Code: Select all
local vValue = application.getCurrentUserEnvironmentVariable ( "health" )
--and not ( "EnemyTestAI", "health" ). this function takes only 1 parameter (the var name), nothing more.

if (vValue < 0) then
hud.setLabelText ( hComponent, "DEAD!")
end
----------


it's all in the manual.
http://www.stonetrip.com/developer/doc/api/application-setCurrentUserEnvironmentVariable
http://www.stonetrip.com/developer/doc/api/application-getCurrentUserEnvironmentVariable
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3580
Location: Berlin - Germany

Re: attempt to compare nil with number

Postby Guinner » 21 May 2012, 00:25

Thank you - I was getting confused between AIVariable and EnvironmentVariable.
I'm not at my computer right now, but i'll try this when I get home and let you know how it goes.
Thanks.
Guinner
Junior Boarder
Junior Boarder
 
Posts: 36

Re: attempt to compare nil with number

Postby Guinner » 21 May 2012, 06:25

broozar wrote:wrong parameter count.

OnInit:
Code: Select all
application.setCurrentUserAIVariable ( "health", 100)

not ( "EnemyTestAI", "health", 100). the function is designed to take 2 parameters (name and value), not 3.

OnEnterFrame:
Code: Select all
local vValue = application.getCurrentUserAIVariable ( "health" )
--and not ( "EnemyTestAI", "health" ). this function takes only 1 parameter (the var name), nothing more.

if (vValue < 0) then
hud.setLabelText ( hComponent, "DEAD!")
end
----------


it's all in the manual.
http://www.stonetrip.com/developer/doc/api/application-setCurrentUserEnvironmentVariable
http://www.stonetrip.com/developer/doc/api/application-getCurrentUserEnvironmentVariable/quote


That's the argumentcount for EnvironmentVariable sorry? Im using AI variable?
Now i'm getting wrong argument count errors?
Guinner
Junior Boarder
Junior Boarder
 
Posts: 36

Re: attempt to compare nil with number

Postby broozar » 21 May 2012, 08:44

oh crud. sorry. of course i meant "...EnvironmentVariable". corrected in the post above.

---------------------

your "...CurrentUserAIVariable" code does not work because EnemyAI is not part of the current user AIs, but an object AI. to make it work, use the object API. http://www.stonetrip.com/developer/doc/api/object-setAIVariable
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3580
Location: Berlin - Germany

Re: attempt to compare nil with number  [SOLVED]

Postby Guinner » 21 May 2012, 09:08

Thank you,
this is now solved: For reference of anyone searching for the same problem my end code was
Code: Select all
--------------------------------------------------------------------------------
--  Handler.......... : onEnterFrame
--  Author........... :
--  Description...... :
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
function EnemyTestAI.onEnterFrame (  )
--------------------------------------------------------------------------------
local hUser = application.getCurrentUser ( )
local hComponent = hud.getComponent ( hUser, "myHUD.myComponent" )   
local hObject = (this.getObject ( ))
local hCamera = scene.getTaggedObject (application.getCurrentUserScene(), "ProperCamera")
--The scene 3D coordinates of the object
local sceneX, sceneY, sceneZ = object.getTranslation ( hObject, object.kGlobalSpace )

--Screen coordinates (-1,1)
local hudX, hudY = camera.projectPoint ( hCamera, sceneX, sceneY + 1, sceneZ )

--Convert to HUD coordinates (0,100)
hudX = hudX * 50 + 50
hudY = hudY * 50 + 50

--Place the component
hud.setComponentPosition (hComponent, hudX, hudY )

local vValue = object.getAIVariable (hObject, "EnemyTestAI", "health" )

if (vValue < 0) then
hud.setLabelText ( hComponent, "DEAD!")
end
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------

Code: Select all
--------------------------------------------------------------------------------
--  Handler.......... : onInit
--  Author........... :
--  Description...... :
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
function EnemyTestAI.onInit (  )
--------------------------------------------------------------------------------
    local hUser = application.getCurrentUser ( )
hud.newTemplateInstance (application.getCurrentUser ( ), "HUD_GAME", "EnemyTest" )
hud.newTemplateInstance ( hUser, "myHUD", "myHUD"  )
--application.setCurrentUserAIVariable ( "health", 100)
local hObject = (this.getObject ( ))
object.setAIVariable ( hObject, "EnemyTestAI", "health", 100 )
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------
Guinner
Junior Boarder
Junior Boarder
 
Posts: 36


Return to Game programming