can anyone help me to get the error cleared? - lua

1.i,ve got error while implementing the game mario in games50 the error shows in the statemachine and its like
function StateMachine:change(stateName, enterParams)
assert(self.states[stateName]) -- state must exist!
self.current:exit()
self.current = self.states[stateName]()
self.current:enter(enterParams)
end
Error
src/StateMachine.lua:18: attempt to call method 'enter' (a nil value)
Traceback
src/StateMachine.lua:18: in function 'change'
main.lua:21: in function 'load'
[C]: in function 'xpcall'
[C]: in function 'xpcall'

OK. So you invoke a function called "enter" which is contained in a table called "current", which is contained in another table passed in the hidden parameter "self" of the function "change".
You need first to check how the "change" function is called.
Check if it's something like:
xxxx:change(...
and not:
xxx.change(...
Because in the second version, the hidden "self" parameter (which will contain a reference to "xxx") is not provided.
Then, check is "self.current" is valid, right after self.current = self.states[stateName]() by printing it:
print(self.current)
If you see a table reference in your console, it's good.
At this stage, it will show you that the problem is the "enter" function which does not exists in the table returned by self.current = self.states[stateName]().
All your code sounds like you're using an external library from which you do not master the use. I advice you to start coding game with your own code, simple games, not using external code.

Related

Why do I use 'end)' instead of just end |?

draw.RoundedBox(0,0,0,100,100,Color(120,255,120))
end)
I'm watching tutorials for learning Lua, more specifically Lua for the Garry's Mod engine. During the tutorial, I noticed using just 'end' didn't work. I had to use 'end)' with a ')'.
Why do I have to place a ')' at the end of end in this code?
Some functions take other functions as parameters.
When you see a function called like this:
hello(param, function(a) print(a) end)
(Sorry, poor example. This function doesn't really do anything) The function on the inside is called internally from the source of the hello function.
The end is the end of the inside function, and the ) is the end of the parameter list for the first function.
So here we have a function hook.Add() being called:
hook.Add("HUDPaint", "DrawMyHud", function()
draw.RoundedBox(0,0,0,100,100,Color(120,255,120))
end)
and from the GMOD wiki:
hook.Add( string eventName, any identifier, function func )
--Add a hook to be called upon the given event occurring.
Although we're calling a function, what the function is doing is creating a special connection in the code called a hook. On the event "HUDPaint", the GMOD client will call the function provided.
Another way to write this function that might make the meaning of end) more clear is by making each parameter it's own line:
hook.Add(
"HUDPaint",
"DrawMyHud",
function() draw.RoundedBox(0,0,0,100,100,Color(120,255,120)) end --end of function declaration
) --end of hook.Add function call

How to determine if sysdig field exists or handle error if it doesn't

I'm using Sysdig to capture some events and have a small chisel (LUA script) to capture and format the events as necessary. On the on_init() I'm requesting fields like so :
f_field = chisel.request_field("<field>")
My question is how can I check if a field exists before requesting it? I'm going to use a new field only just released on 0.24.1 but ideally I'd like my chisel to continue to work on older versions of sysdig without this field. I've tried wrapping the call to chisel.request_field in a pcall() like so :
ok, f_field = pcall(chisel.request_field("<field>"))
and even implementing my own "get_field" function :
function get_field(field)
ok, f = pcall(chisel.request_field(field))
if ok then return f else return nil end
end
f_field = get_field("<field>")
if f_field ~= nil then
-- do something
end
but the error ("chisel requesting nonexistent field <field>") persists.
I can't see a way to check if a field exists but I can't seem to handle the error either. I really don't want multiple versions of my scripts if possible.
Thanks
Steve H
You're almost there. Your issue is in how you're using pcall. Pcall takes a function value and any arguments you wish to call that function with. In your example you're passing the result of the request_field function call to pcall. Try this instead..
ok, f = pcall(chisel.request_field, "field")
pcall will call the chisel method with your args in a protected mode and catch any subsequent errors.

attempt to index local 'itemWidget' (a nil value)

Somebody can explain me this error ?? I'm new on Lua, and I wanna learn what I'm doing wrong!!
This is my Inventory.Lua
https://pastebin.com/KiUEajMm
I get this error:
ERROR: Unable to load module 'game_inventory': LUA ERROR:
/game_inventory/inventory.lua:117: attempt to index local 'itemWidget' (a nil value)
stack traceback:
[C]: in function '__index'
/game_inventory/inventory.lua:117: in function 'onInventoryChange'
/game_inventory/inventory.lua:77: in function 'refresh'
/game_inventory/inventory.lua:47: in function 'init'
/game_inventory/inventory.otmod:8:[#onLoad]:1: in main chunk
[C]: in function 'reloadModules'
/client_modulemanager/modulemanager.lua:149: in function 'reloadAllModules'
/client_modulemanager/modulemanager.otui:75: [#onClick]:2: in function </client_modulemanager/modulemanager.otui:75: [#onClick]:1>
The error means that you try to access a methode of itemWidget while it is nil. So if the possibility exits that itemWidget can be nil you have to check this:
if itemWidget == nil then
itemWidget:setStyle(InventorySlotStyles[slot])
...
end
Form the code you posted I can only guess why itemWidget is nil:
it is valid that inventoryPanel:getChildById('slot' .. slot) returns nil
a typo somewhere (e.g.: 'slot' .. slot instead of 'slot ' .. slot --> missing space)
wrong order: inventoryPanel:getChildById('slot' .. slot) only works after the init-function has ended or something similar

love2d just wont accept some codes

Every time I use this:
function love.draw()
love.graphics.setfont(love.graphics.newfon(50))
I get this error:
Error
main.lua:2: attempt to call field newfon (a nil value)
Traceback
main.lua:2: in function draw [C]: in function xpcall
How can I fix it ?
Lua is case-sensitive. The correct call is
love.graphics.setFont(love.graphics.newFont(50))

gmod GameMode Lua. IsPlayer retuning nill value

I'm trying to make a gmod gamemode. In my init.lua I wanted it so that way team members can't hurt each other. So I used this code
function GM:EntityTakeDamage( target, dmginfo )
if ( target:IsPlayer() and dmginfo:IsPlayer() ) then
if (dmginfo:Team() == target:Team()) then
dmginfo:ScaleDamage( 0.0 ) // Sets damage to 0
end
end
end
However it's giving me the error telling me that IsPlayer() is a nil value even though it should be returning a boolean. It points to no other lines other then the line with IsPlayer() and it's saying it is IsPlayer()
you have a typo in line 3. dminfo
You should narrow down which of your multiple IsPlayer() calls actually is nil
dmgInfo is a CTakeDamageInfo which has no function IsPlayer()
single line Lua comments are opened with --, not //
https://wiki.garrysmod.com/page/Category:CTakeDamageInfo
If you call a function and it says its nil, then check if it even exists. Or even better, check this befor you use the function in the first place.
And to prevent you from coming back in a minute, CTtakeDamageInfo also does not have a function Team() as well.
Check out CTDamageInfo:GetAttacker()

Resources