ShiVa3D
attempt to compare nil with number [SOLVED]
All about the StoneScriptattempt to compare nil with number
by Guinner » 18 May 2012, 23:48
So whenever i'm running my code i'm getting this:
I did some research and the main problem behind this usually is not declaring the variable. But I have this under enterframe:
And this under onInit:
So I know i've declared it.
What could be going on?
- 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

- Posts: 36
Re: attempt to compare nil with number
by 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.
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.
Re: attempt to compare nil with number
by 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.
Thanks for the quick reply, however that hasn't seemed to fix the problem. It is still the same error.
- Guinner
- Junior Boarder

- Posts: 36
Re: attempt to compare nil with number
by dtr1au » 19 May 2012, 01:09
Can you post how hVariable is being declared?
Re: attempt to compare nil with number
by Guinner » 19 May 2012, 01:11
- Code: Select all
local hVariable = application.getCurrentUserAIVariable ("EnemyTestAI", "health" )
- Guinner
- Junior Boarder

- Posts: 36
Re: attempt to compare nil with number
by 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
You'd want to set the variable using application.setCurrentUserAIVariable
Re: attempt to compare nil with number
by 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

- Posts: 36
Re: attempt to compare nil with number
by neilb » 19 May 2012, 04:35
Use getCurrentUserEnvironmentVariable to read the value, you've got setCurrentUserEnvironmentVariable in the code you pasted.
Re: attempt to compare nil with number
by Guinner » 19 May 2012, 05:28
OnInit:
OnEnterFrame:
And i've created it in the explorer as a number. Still no bingo.
- 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

- Posts: 36
Re: attempt to compare nil with number
by 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?
Have you tried stepping through the code to confirm that the onInit code is actually being called before the onEnterFrame?
Re: attempt to compare nil with number
by broozar » 19 May 2012, 17:34
wrong parameter count.
OnInit:
not ( "EnemyTestAI", "health", 100). the function is designed to take 2 parameters (name and value), not 3.
OnEnterFrame:
it's all in the manual.
http://www.stonetrip.com/developer/doc/api/application-setCurrentUserEnvironmentVariable
http://www.stonetrip.com/developer/doc/api/application-getCurrentUserEnvironmentVariable
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
Re: attempt to compare nil with number
by 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.
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

- Posts: 36
Re: attempt to compare nil with number
by 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

- Posts: 36
Re: attempt to compare nil with number
by 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
---------------------
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
Re: attempt to compare nil with number [SOLVED]
by 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
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

- Posts: 36
15 posts
• Page 1 of 1
