Jump to content

new to mods, have trouble with functions, help appreciated


Recommended Posts

Hello Iam new to modding, and I keep running into problem with functions not getting its parameters...

 

I have the following scenario:

 

component code:

 

inst.MyNewFn = nil

 

function inst:MyNewFnCaller()

local testingvalue = math.random(2)

self:MyNewFn(testingvalue)

end

 

function inst:SetUpFn(fn)
self.MyNewFn = fn
end

 

prefab code:

 

inst.components.MyComponent:SetUpFn(

function(value)

if value > 1 then

"domething"

end

end

)

 

Basically I have a prefab with a component - i have a function MyNewFn in the component that gets loaded by the prefab using the SetUpFn function. Then the component has a caller function that generates some kind of number and then calls the function that was given by the prefab and passes the generated number to that function.

 

The problem is that when I run this, I get an error saying that I cant compare number with a table. The error happens on the line with - if value > 1 then

 

Dont know why this is happening. I thought the problem is somewhere with the function being given by the prefab so I tried changing it...

 

My second code looked like this:

 

component code:

 

function inst:MyNewFn(value)

if value > 1 then

"dosomething"

end

end

 

function inst:MyNewFnCaller()

local testingvalue = math.random(2)

self:MyNewFn(testingvalue)

end

 

In this code, the value testing statement is already inside the MyNewFn from the start (its not given by the prefab) - this doesnt seem to produce the number to table compare error and works as intended. It does the "dosomething" statement roughly 50 percent of the time.

 

But I really really need my function to get loaded from the prefab, because in the "dosomething" statement, I want to do something with the prefab itself, not just with the component. I believe you call this type of thing "callback" - that component can basically see who his prefab is by getting a function from him.

 

Can anyone pls help with this problem?

Link to comment
Share on other sites

I'm not..... entirely sure about what you're trying to achieve, but I can tell you that what you have is not a component. At least, that is, if that is the entirety of the code comprising the component. Components are a kind of factory class (that you create using Class()) that you can attach the products of to prefabs via AddComponent(). You don't want to just shove generic table references into the components table of a prefab. Though maybe you are creating it correctly and just didn't post all the code :p  That said, it's a little easier to understand what's going on when formatted slightly differently:
 

inst.MyNewFn = nil  --[[ tables can't actually hold nil as a value, so you only ever need to                     explicitly set something to nil when you want to delete it, or to make sure it                     gets garbage collected.]]function inst:MyNewFnCaller()      -- this defines the function:    MyNewFnCaller(self)    local testingvalue = math.random(2)      self:MyNewFn(testingvalue)     -- this calls MyNewFn with two arguments:                                    --    self(as "self"), and testingvalueend--[[ side note -- you may want to do:       if self.MyNewFn then self:MyNewFn(testingvalue) end     ... to avoid potential 'attempt to call a nil value' errors ]] function inst:SetUpFn(fn)          -- this defines the function:    SetUpFn(self, fn)    self.MyNewFn = fnend---------------------------------prefab code:local function MyFn(value)         -- this defines the function:    MyFunction(value)    if value > 1 then              -- this was what you had already, I just moved it outside and        --"do something"           -- assigned it to a variable. ]]    endendinst.components.MyComponent:SetUpFn(MyFn)  --[[  This is calling SetUpFn with two arguments:                                                      MyComponent(as "self"), and MyFunction                                                 this is also identical (with the exception of the                                                 one local variable to hold the function) ]]

This effectively ends up with Lua trying to evaluate MyComponent > 1

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...