Page 1 of 1

[solved] cannot assign function to variable ingame console?

Posted: Wed Jun 17, 2015 1:58 pm
by semvoz
Hello,

While doing some testing for my `mod` I tried to execute the following commands:

Code: Select all

/c p = game.player.print
/c p('test')
Here is the stacktrace that I got after the crash:

Code: Select all

/o/g/f/mods ❯❯❯ less ../factorio-current.log
   0.000 2015-06-17 23:45:13; Factorio 0.11.22 (Build 14011, linux64)
   0.000 Operating system: Linux
   0.000 Read data path: /opt/games/factorio/data
   0.000 Write data path: /opt/games/factorio
   0.000 Binaries path: /opt/games/factorio/bin
   0.352 Graphics options: [FullScreen: true] [VSync: true] [UIScale: 100%] [MultiSampling: OFF] [Graphics quality: normal] [Video memory usage: all]
   0.395 Loading mod core 0.0.0 (data.lua)
   0.412 Loading mod base 0.11.22 (data.lua)
   0.607 Loading mod tutorial-mod 0.0.1 (data.lua)
   0.805 Loading mod base 0.11.22 (data-updates.lua)
   1.420 Initial atlas bitmap size is 8192
   1.436 Created atlas bitmap 8192x8181
   1.437 Created atlas bitmap 8192x4933
   9.113 Info Updater.cpp:717: Downloading https://www.factorio.com/updater/get-available-versions?username=semvoz&token=<private>&apiVersion=2
  10.486 0 packages available to download (experimental updates enabled).
  10.510 Factorio initialised
  17.886 Loading map /opt/games/factorio/saves/test.zip
  17.894 Info Scenario.cpp:158: Map version 0.11.22-0
Factorio crashed. Generating symbolized stacktrace, please wait ...
#2  0x5bcfa3 in CrashHandler::writeStackTrace() at /home/build/Factorio/src/Util/CrashHandler.cpp:79
#3  0x5bd009 in CrashHandler::SignalHandler(int)
#4  0x7f8ca10455b0 in /home/build/Factorio/src/Util/CrashHandler.cpp:139
#5  0x6ec5f8 in ??
#6  0x6ef72c in ??:0
#7  0x44fedf in std::list<OutputConsole::Item, std::allocator<OutputConsole::Item> >::empty() const
#8  0x9acfbd in /usr/include/c++/4.8/bits/stl_list.h:869
#9  0x9aad54 in OutputConsole::addInGameState(LocalisedString const&, Color)
#10 0x9ad749 in /home/build/Factorio/src/Info/OutputConsole.cpp:58
#11 0x9adcb8 in LuaPlayer::luaPrint(lua_State*)
#12 0x998d18 in /home/build/Factorio/src/Script/LuaPlayer.cpp:224 (discriminator 1)
#13 0x6ef8ea in LuaBinder<LuaPlayer>::callWrapper(lua_State*)
#14 0x859263 in /home/build/Factorio/src/Script/LuaBinder.hpp:303
#15 0x8583a9 in luaD_precall(lua_State*, lua_TValue*, int)
#16 0x7a76e8 in crtstuff.c:?
#17 0x4d5ef8 in luaV_execute(lua_State*)
#18 0x943c85 in crtstuff.c:?
#19 0x94a186 in luaD_call(lua_State*, lua_TValue*, int, int)
#20 0x6b47b8 in crtstuff.c:?
#21 0xc1f5fa in luaD_pcall(lua_State*, void (*)(lua_State*, void*), void*, long, long)
#22 0x7f8ca29cf354 in crtstuff.c:?
#23 0x7f8ca10f9bfd in lua_pcallk(lua_State*, int, int, int, int, int (*)(lua_State*))
 288.393 Error Util.cpp:43: Unexpected error occurred. You can help us to solve the problem by posting the contents of the log file on the Factorio forums.
I am not sure if it is a known issue, or simply something we cannot do in factorio's console, but function assignment is working fine in LUA in general?

Re: cannot assign function to variable in ingame console?

Posted: Wed Jun 17, 2015 4:22 pm
by Rseding91
The player object is created when you call game.player
The print method address is based off the player object
The player object is destroyed after the call to game.player.print is finished

Effectively you're trying to call an invalid memory location.

You can use this:

Code: Select all

/c p = function(msg) game.player.print(msg) end

Re: cannot assign function to variable in ingame console?

Posted: Wed Jun 17, 2015 10:14 pm
by semvoz
Thank you, that makes sense with these details.