How to use my function in the game console?

Place to get help with not working mods / modding interface.
ManselD
Inserter
Inserter
Posts: 26
Joined: Mon Apr 01, 2013 10:43 pm
Contact:

How to use my function in the game console?

Post by ManselD »

Here's the code in control.lua:

Code: Select all

    
    require "util"
    require "defines"


    function gimmeCar()
        game.player.character.insert{name="car", count=1}
    end
Mind you i'm only testing, here's the image of the error: http://prntscr.com/z9c48 But why doesn't it work?

User avatar
rk84
Filter Inserter
Filter Inserter
Posts: 556
Joined: Wed Feb 13, 2013 9:15 am
Contact:

Re: How to use my function in the game console?

Post by rk84 »

This control.lua is in mods, right? I think console works in same level as core control.lua

So your options are:
- modify control.lua in core folder.
- or type the function console. (mayby store it in global table?)
- or use Script interfaces.

I'm not sure about last option though. My tries to remote.call freeplay's funtions in console has failed so far and tries to discover what is inside remote.interfaces has lead to crashes without error.
Test mode
Searching Flashlight
[WIP]Fluid handling expansion
[WIP]PvP gamescript
[WIP]Rocket Express
Autofill: The torch has been pass to Nexela

slpwnd
Factorio Staff
Factorio Staff
Posts: 1835
Joined: Sun Feb 03, 2013 2:51 pm
Contact:

Re: How to use my function in the game console?

Post by slpwnd »

rk84 wrote: I'm not sure about last option though. My tries to remote.call freeplay's funtions in console has failed so far and tries to discover what is inside remote.interfaces has lead to crashes without error.
That is not a desired behavior :) Could you give me a specific example ?

User avatar
rk84
Filter Inserter
Filter Inserter
Posts: 556
Joined: Wed Feb 13, 2013 9:15 am
Contact:

Re: How to use my function in the game console?

Post by rk84 »

slpwnd wrote:
rk84 wrote: I'm not sure about last option though. My tries to remote.call freeplay's funtions in console has failed so far and tries to discover what is inside remote.interfaces has lead to crashes without error.
That is not a desired behavior :) Could you give me a specific example ?
I tried function call in console

Code: Select all

game.player.print(tostring(remote.call("freeplay", "getattackdata")))
-> function:xxxxxxxxxxxxxxxx
I expected table.

I try using it

Code: Select all

f = remote.call("freeplay", "getattackdata")
game.player.print(tostring(f()))
-> Bad argument #0 to f (Wrong number of arguments)
So I try to explore in remote.interfaces

Code: Select all

 if remote.interfaces.freeplay and remote.interfaces.freeplay.getattackdata then game.player.print("Safe to use.") end
->I hear the message beep and windows tells factorio has stop working. I guess it was not safe after all.

Code: Select all

game.player.print(tostring(remote.interfaces.freeplay))
-> Error invalid key to next
I run into another problem. Before those tests above I turned all my mods off and restarted, but atleast one of them were still answering script events (init,onload,onsave). Mod recipes and tech was not loaded. Sorry too lazy to make another thread :P
Test mode
Searching Flashlight
[WIP]Fluid handling expansion
[WIP]PvP gamescript
[WIP]Rocket Express
Autofill: The torch has been pass to Nexela

ManselD
Inserter
Inserter
Posts: 26
Joined: Mon Apr 01, 2013 10:43 pm
Contact:

Re: How to use my function in the game console?

Post by ManselD »

Can I have an example of how I can achieve this without putting my code in the main control.lua (only the one in my mod folder)
I'm confused as to why this doesn't work.

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: How to use my function in the game console?

Post by FreeER »

Is there a reason you don't want to place it in your mods control.lua? I suppose you could always use require in the data.lua file to refrence an interface.lua file or something.

edit:
rk84 wrote: I tried function call in console

Code: Select all

game.player.print(tostring(remote.call("freeplay", "getattackdata")))
-> function:xxxxxxxxxxxxxxxx
I expected table.
I'm fairly sure you can not print a function (unless maybe if you created your own function that used return to put the data into a string, ie

Code: Select all

function whatever() remote.call("freeplay", "getattackdata") return tostring(getattackdata) end
or a table (without iterating over the table)
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net

drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

Re: How to use my function in the game console?

Post by drs9999 »

rk84 wrote:This control.lua is in mods, right? I think console works in same level as core control.lua
IMHO this is the "real" problem here

If you call the the function that ManseID posted, somewhere in the mods-control file it is working fine, but you can not call it from the console (AFAIK).
That is maybe not that big issue in that case, because you can just type "game.player.character.insert{name="car", count=1}" instead of gimmeCar()", but
in reference to the 0.2.10 CCMod, where you were able to enter creative-mode via a function call from the console it is a problem in my opinion.

But like I said maybe there is a way to call a function from mod-lua-file I am not sure , but if yes I really want to know how :D

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: How to use my function in the game console?

Post by FreeER »

drs9999 wrote:
rk84 wrote:This control.lua is in mods, right? I think console works in same level as core control.lua
IMHO this is the "real" problem here
If you call the the function that ManseID posted, somewhere in the mods-control file it is working fine, but you can not call it from the console (AFAIK).
That is maybe not that big issue in that case, because you can just type "game.player.character.insert{name="car", count=1}" instead of gimmeCar()", but
in reference to the 0.2.10 CCMod, where you were able to enter creative-mode via a function call from the console it is a problem in my opinion.
But like I said maybe there is a way to call a function from mod-lua-file I am not sure , but if yes I really want to know how :D
This is an issue but you can not simply tell the game to load the console up where the mod is (many people may have more than one). SO there are the remote interfaces, I just tried this (since it's one of the things I want to be able to cover in the 3.x modding guide):

Code: Select all

remote.addinterface("RemoteInterfacesTest",
{
    testprint = function(msg)
        game.player.character.print(tostring(msg))
    end
})
Then I go into the console and type remote.interfaces.RemoteInterfacesTest.testprint(test) and windows tells me that Factorio.exe has stopped working :) THAT I think is the real issue, that the interfaces mods are suppose to use to communicate do not seem to be working properly
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net

drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

Re: How to use my function in the game console?

Post by drs9999 »

FreeER wrote:This is an issue but you can not simply tell the game to load the console up where the mod is (many people may have more than one)
Good point, totally forgot about that :D

Anyway, the interface-stuff wont work like you thought. I didnt tested it a lot, but according to the wiki an interface can only "send/receive" values and or tables.

Also to access an interface the correct command is "remote.call("InterfaceNameHere","FunctionName"[,parameter,...])
So reffering to your code, this will work:

This goes to the mods control.lua:

Code: Select all

remote.addinterface("RemoteInterfacesTest",
{
    testprint = function()
        return "Applejuice"
    end
})
And type this into the console:

Code: Select all

game.player.print(remote.call("RemoteInterfacesTest",testprint)
So I wasnt able to create a function that return a variable for example some thing like this:

Code: Select all

remote.addinterface("RemoteInterfacesTest",
{
    testprint = function(msg)
        return testprint
    end
})
But I just played a bit around after I read you post, I guess it is doable, i just want to share my knowledge so far.

slpwnd
Factorio Staff
Factorio Staff
Posts: 1835
Joined: Sun Feb 03, 2013 2:51 pm
Contact:

Re: How to use my function in the game console?

Post by slpwnd »

Just to confirm what was already mentioned before: The code in the console is run from the context of the scenario script. When you are playing the regular Factorio game that would be the core/lualib/freeplay.lua. You can change the global variables, run the functions, etc.

As for communicating with other mods that should be done either via raising custom script events or by using script interfaces. The interfaces seem buggy. I found already one specific bug when referencing remote.interfaces from the script that defines an interfaces itself will cause a crash. This is bound to some complexities in dealing with multiple lua states in the background. Anyway in short this means that you do check remote.interfaces in the freeplay.lua (or in the console) that will break Factorio. The same holds for calling the interface from the same script that defined it. I will look more into this tomorrow.

ManselD
Inserter
Inserter
Posts: 26
Joined: Mon Apr 01, 2013 10:43 pm
Contact:

Re: How to use my function in the game console?

Post by ManselD »

FreeER wrote:Is there a reason you don't want to place it in your mods control.lua? I suppose you could always use require in the data.lua file to refrence an interface.lua file or something.

edit:
rk84 wrote: I tried function call in console

Code: Select all

game.player.print(tostring(remote.call("freeplay", "getattackdata")))
-> function:xxxxxxxxxxxxxxxx
I expected table.
I'm fairly sure you can not print a function (unless maybe if you created your own function that used return to put the data into a string, ie

Code: Select all

function whatever() remote.call("freeplay", "getattackdata") return tostring(getattackdata) end
or a table (without iterating over the table)
I have placed it in my Mods folder's control.lua, that's how I want it to be. I just don't want to place it in the base/core's control.lua file. Even in my file it simply doesn't work. I'm trying to make a mod for starting off and this is essential for it. Unless there's a way to append into the main file (not sure what that is) so that my function(s) are able to be called from the console.

drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

Re: How to use my function in the game console?

Post by drs9999 »

So what exactly you are trying to achieve? Maybe there is another solution to solve it.

ManselD
Inserter
Inserter
Posts: 26
Joined: Mon Apr 01, 2013 10:43 pm
Contact:

Re: How to use my function in the game console?

Post by ManselD »

To be able to call and use my function that's in my mods' folder's control.lua through the in-game console.

drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

Re: How to use my function in the game console?

Post by drs9999 »

Yes obviously. But what you want to do in this functions? Reffering to your initial post it is not that big problem to type "game.player.character.insert{name="car", count=1}" instead of "remote.call(X,Y,Z)" or something like this (with an interface) in my opinion.

But I guess that it was just a simple example? More infos about these function would raise the chance that anyone can help you.

ManselD
Inserter
Inserter
Posts: 26
Joined: Mon Apr 01, 2013 10:43 pm
Contact:

Re: How to use my function in the game console?

Post by ManselD »

It doesn't really matter what's in the function, all there is, is some inserts. And it's much easier to do "gimmeCar()" than:

Code: Select all

game.player.character.insert{name="car", count=1}
But it doesn't work -.-

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: How to use my function in the game console?

Post by FreeER »

Well if you just want a shorter way to get items you can set variables from console, thus you could do

Code: Select all

gimme=function(name,count) game.player.character.insert{name=name, count=count} end
after that you should be able to do

Code: Select all

gimme("car", 1)
Assuming you are giving yourself more than one item, just don't forget the quotes when you type the gimme command.
You could extend this to give you a large set of items within the gimme function but since you'd still have to type it yourself... If that is what you wanted then it'd be easier to simply type it into notepad then copy/paste it into the console lol.
Not sure how feasible this is but you might be able to use pcall(require, cheat.lua) in your data.lua (I believe pcall suppresses errors if a file does not exist) and simply place a cheat.lua file inside of your mod folder. Or if Factorio allows lua to check for files then you could simply have your mod check for a cheat.txt file within the mod directory, if it exists then it gives you a set of items on game start.

edit: Started playing with that last idea of mine:

Code: Select all

game.oninit(function()
    local cheatFile="cheat.txt"
    if cheatFile then
        game.player.character.insert{name="car",count=1}
	end
end)
thus if you create a cheat.txt file within your mods folder then you will get a car when the game starts
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net

ManselD
Inserter
Inserter
Posts: 26
Joined: Mon Apr 01, 2013 10:43 pm
Contact:

Re: How to use my function in the game console?

Post by ManselD »

No, I don't want to create the function in-game. WHY U NO WORK IN-GAME? fml.

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: How to use my function in the game console?

Post by FreeER »

wow, I just went back and tried something... it worked, WHY did this not occur to me earlier lol

Code: Select all

remote.addinterface("RemoteInterfacesTest",
{
	cheat = function()
        local function cheater()
            game.player.character.insert{name="car", count=10} 
            game.player.character.insert{name="iron-ore", count=10}
        end
        return cheater()
	end
}
)
note if you test this: cars only stack to one so you'll get one car in your quick bar and 9 in your inventory
could probably even shorten this to cheat=function() return game.player.character.insert{...} end (though not sure if you could insert more than one item this way)
Now I just need to figure out how I can pass variables within a remote call lol
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net

MF-
Smart Inserter
Smart Inserter
Posts: 1235
Joined: Sun Feb 24, 2013 12:07 am
Contact:

Re: How to use my function in the game console?

Post by MF- »

I agree that a functionality that would allow mods to export some functions (=>commands) into the console context would be really useful.
(So that a mod could request registration of "gimmeCar" in the correct context from it's init function or something like that)

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: How to use my function in the game console?

Post by FreeER »

Additional functionally would probably be especially useful for people (not pointing fingers lol) who have mods that they are splitting up into several, or intend to create several mods that work fine on their own but are intended to complement each other :D
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net

Post Reply

Return to “Modding help”