Jump to content

How To Create a Custom Console Command (help me!)


Recommended Posts

I tried that already! D:

and just to test I tried it again!

 

G.ConsoleCommand1 = function()
G.GetPlayer().components.talker:Say("This is a console command...")
end
 
I did this and entered ConsoleCommand1 in the console and it says "attempt to call a nil value"
 
(and if you are wondering I got a line saying "local G = GLOBAL")
Link to comment
Share on other sites

It's not possible for something to be added to the GLOBAL table and for it to disappear once in game.  modmain.lua has a environment that's not global, but the GLOBAL table is the global environment.  Make sure you don't have any typos.  Make sure something else isn't breaking your script.

 

You can verify this by retrieving the global environment from a function that uses it and comparing it to the GLOBAL table(or whatever your reference variable is).

local global_env = getfenv(tostring) -- tostring used as example.  Any core Lua function will do.print(GLOBAL == G, G == global_env) --> true  true
Link to comment
Share on other sites

 

I tried that already! D:

and just to test I tried it again!

 

G.ConsoleCommand1 = function()
G.GetPlayer().components.talker:Say("This is a console command...")
end
 
I did this and entered ConsoleCommand1 in the console and it says "attempt to call a nil value"
 
(and if you are wondering I got a line saying "local G = GLOBAL")

 

 

Are you setting brackets in the command? :p

Link to comment
Share on other sites

 

It's not possible for something to be added to the GLOBAL table and for it to disappear once in game.  modmain.lua has a environment that's not global, but the GLOBAL table is the global environment.  Make sure you don't have any typos.  Make sure something else isn't breaking your script.

 

You can verify this by retrieving the global environment from a function that uses it and comparing it to the GLOBAL table(or whatever your reference variable is).

local global_env = getfenv(tostring) -- tostring used as example.  Any core Lua function will do.print(GLOBAL == G, G == global_env) --> true  true

 

I will have to fiddle with it later today but yes I do not have any typos at all. Are you able to make a GLOBAL command and make it work in the console?

Link to comment
Share on other sites

You mean ConsoleCommand1() right?

 

lol I said the same :razz:

 

@Fidooop, I went through some of my mods and spontanously called global functions, got no errors other than missing parameters. Can you call other functions without problem? How about global functions from other mods?

Link to comment
Share on other sites

Are you setting brackets in the command? :razz:

I feel kinda bad that it looks like I ignored you so I thought I'd clear things up!

when you said brackets the first thing that came to mind was [] so I got really confused and skipped over your comment... sorry :C

but now I know you meant parentheses because I was missing those!!!! D:

kinda mad at myself for forgetting the most painfully obvious things in the world!!! :L

Link to comment
Share on other sites

@Fidooop,

 

The parentheses were the first thing I though of, but then I thought "well then he wouldn't be getting the 'attempt to call a nill value' error...."

 

Completely forgot that that's the error you get from the console because it attempts to convert the console string into a function (I believe via loadstring), which fails, and then it tries to execute the function, which is nil.

 

 

 

Link to comment
Share on other sites

I feel kinda bad that it looks like I ignored you so I thought I'd clear things up!

when you said brackets the first thing that came to mind was [] so I got really confused and skipped over your comment... sorry :C

but now I know you meant parentheses because I was missing those!!!! D:

kinda mad at myself for forgetting the most painfully obvious things in the world!!! :L

 

Yeah I'm not the standard modder :p Next time I make no sense just question me about it!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...