Page 1 of 1

[0.10.9] print = game.player.print (crashes on call)

Posted: Tue Sep 02, 2014 10:50 am
by rk84
I should be playing the game, but I'm fooling around with Lua scripts again :)

I get crash if I try to call "game.player.print"-method with variable that refers it directly. Not sure if it is only with print.
This crashes.

Code: Select all

print = game.player.print
print("test")
This works.

Code: Select all

gt = game.gettile
game.player.print( gt(0,0).name )
As a side note I was trying it in closure test and it did print "ModA tick: 0" and crashed after 300 ticks. (Closure test was overall successful after removing the print reference.)

Code: Select all

require "defines"

function initOnTick()
  local tick = 0
  return function(event)
    if event.tick >= tick then
      game.player.print("ModA tick: ".. tick)
      tick = event.tick + 300
    end
  end
end

game.onevent(defines.events.ontick, initOnTick() )

Re: [0.10.9] print = game.player.print (crashes on call)

Posted: Thu Sep 04, 2014 6:55 am
by kovarex
Hello, we are aware of this problem and I'm afraid we will not fix it soon.

The difference is, that with the game.<some method>, it works, because the game object is persistent, it exists all the time during the game, but the game.player<something> doesn't work, as the player object is removed after the game.player is executed.

Generally saving pointers to methods is problematic, and needs to be done with caution (these pointers are not persistent when you save/load the game, so you need to refresh them etc).

The solution to this particular problem is:

Code: Select all

print = function(text) game.player.print(text) end
This way, the player object is evluated every time you call the print.

Moving this to known issues.

Re: [0.10.9] print = game.player.print (crashes on call)

Posted: Mon Sep 08, 2014 12:07 pm
by rk84
I forgot this has been discussed. Thank you for reply.

Re: [0.10.9] print = game.player.print (crashes on call)

Posted: Tue Mar 01, 2016 10:43 am
by Albrat
I will also start that you defined a local variable that over-writes a global varible. while the global variable is used in the definition of the new local variable.

print = game.player.print

each "." represents a seperator for command names. so game , player and print are global variables. You defined print as equalling game.player.print (where print now = game.player.print.game.player.print.game.player.print .... Untill you run out of memory. It created a loop that will never break. )

try changing the variable Print to outprint or some other name. It should work.