3

In my game that uses Gideros Studio, I have a function that has multiple parameters. I want to call my function on one parameter, and then later on another. Is this possible?

Here is my function:

local function wiggleroom(a,b,c)
    for i = 1,50 do
        if a > b then
            a = a - 1
        elseif a < b then
            a = a + 1
        elseif a == b then
            c = "correct"
        end
    return c
    end
end

I want a to be compared with b, but call the function on b and c later on. For example:

variable = (wiggleroom(variable, b, c) --if variable was defined earlier
variable2 = (wiggleroom(a, variable2, c)
variable3 = (wiggleroom(a, b, variable3)

I also want to be able to use this function for multiple objects (call each parameter twice).

agrm
  • 3,735
  • 4
  • 26
  • 36
Henry V
  • 195
  • 1
  • 7
  • Please clarify, what is the result you are expecting after each function call. Do you want value of a and b to be changed outside of the function, too? – lisu Oct 18 '14 at 23:57
  • I don't want a and b to be changed outside of the function, I just want them to be compared. However, I want c 's value to be returned based on this comparison. – Henry V Oct 19 '14 at 00:53
  • Then why you cannot call it exactly as you stated? – lisu Oct 19 '14 at 01:01
  • I don't know if you could call it twice for each parameter (for x and y coordinates) – Henry V Oct 19 '14 at 01:13
  • Your example would beore useful if it were syntactically correct AND used actual numbers for a,b,c with the actual return value you would like. As it stands it is unclear what you are trying to achieve. – Oliver Oct 19 '14 at 15:19

1 Answers1

2

If I am understanding you correctly you may consider using the lua version of classes. If you don't know them you may want to look at this.

example:

tab = {}

function tab:func(a, b, c)  -- c doesn't get used?
    if a then self.a = a end
    if a then self.b = b end
    if a then self.c = c end

    for i = 1,50 do
        if self.a > self.b then
            self.a = self.a - 1
        elseif self.a < self.b then
            self.a = self.a + 1
        elseif self.a == self.b then
            self.c = "correct"
        end
    end
    return c                -- not really necessary anymore but i leave it in
end

function tab:new (a,b,c)    --returns a table
    o = {}
    o.a = a
    o.b = b
    o.c = c
    setmetatable(o, self)
    self.__index = self
    return o
end

                            --how to use:
whatever1 = tab:new(1, 60)  --set a and b
whatever2 = tab:new()       --you also can set c here if needed later in the function

whatever1:func()            --calling your function
whatever2:func(0,64)

print(whatever1.a)          -->51
print(whatever2.a)          -->50
print(whatever1.c)          -->nil
whatever1:func()            --calling your function again
whatever2:func()
print(whatever1.a)          -->60
print(whatever2.a)          -->64
print(whatever1.c)          -->correct
print(whatever2.c)          -->correct
CHlM3RA
  • 118
  • 6