Jump to content

Irritating code behavior? [LUA]


Phodex Games
 Share

Go to solution Solved by Josh,

Recommended Posts

EDIT:

Just found out that the error was somewhere else ^^. The below code just works fine. Anyway thank you for the help :).

Hi there,

I faced some weird behavior, testing and debugging my project. The following shows the structure of my code, it would be very kind of you, if you could tell me if you can find any error, because I dont. Maybe I messed around with global/local/script variables, which I know, often cause such errors.

Explanation:

Of course this script makes no sense, but it should only representate the structure, because it would be way to complicated to explain the issue within the actual script. So what is the problem? When I apply the "Main Script" to one entity, everything works fine and I get printed the data. but if I apply the "Main Script" to more than one, for example two, then the problem occurs. In the output I get the "Send my data!"-message, but now comes the weird thing. It prints the data and then, even if the "Test"-function is just called once it prints "Variable data is nil!" twice. And this happens for both entitys. So this is my output with two entities with the "Main Script":

  • Send my data!
  • Test
  • Variable data is nil!
  • Variable data is nil!
  • Send my data!
  • Test
  • Variable data is nil!
  • Variable data is nil!

Main Script:

Script.dataToSend = "Test"
Script.data = nil

import "exampleScript.lua"

function Script:Start()
	self.myLuaClass = testClass(self)
end

function Script:Test(data)
	if data ~= nil then System:Print(data) else System:Print("Variable data is nil!") end
end

The "exampleScript.lua" Script:

function testClass(script)
	local container = {}
  
	function container:CallMeFromAnywhereToSendSomething()
  	  self:SendSomething(script.dataToSend)
  	end
  
	function container:SendSomething(sendData)
	   if sendData ~= nil then
	      script.data = sendData
	   end
	   if script.data ~= nil then System:Print("Sent my data!") script:Test(script.data) end
	end
  
  	return container
end
  	

Addition:

I was able to work around this issue by simply checking if the variable is nil, or not (I actually need/do stuff with the "data" variable in my actual script), but still I am confused. Maybe you can help me out. If you find other mistakes, or have improvement suggestions for my "Class" system please tell me. Thanks for reading :) 

Link to comment
Share on other sites

  • Solution

Looks like script is a global variable.  Add it as a member of the container table.

function testClass(script)
	local container = {}
  	container.script = script
  
	function container:CallMeFromAnywhereToSendSomething()
  	  self:SendSomething(self.script.dataToSend)
  	end
  
	function container:SendSomething(sendData)
	   if sendData ~= nil then
	      self.script.data = sendData
	   end
	   if self.script.data ~= nil then System:Print("Sent my data!") self.script:Test(self.script.data) end
	end
  
  	return container
end

 

  • Upvote 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...