Page 1 of 1

Searching through multiple alcoves?

Posted: Sat Feb 02, 2013 5:18 am
by Ryeath_Greystalk
I come humbly requesting a little more guidance with a script if someone would be so kind.

This is a small section of a script I am attempting that searches the contents of 4 alcoves for various things. I'm trying to pass values between functions (for the first time) so I have more than one function on the script.

Code: Select all

function qtyCheck(punished) 
local punished 
local test = false                 -- false means test failed
	for n = 1,4 do
	                if ScaleAlcove[n]:getItemCount() ~= 3 then
			judgement(test)            -- send false to judgement function indicate fail
			return punished            -- should return true allThiingsEqual function
		end                                              -- and stop qtyCheck function
	
		test = true                    -- indicate test passed
		judgement(test)
		return punished
	end
end
I have 4 alcoves with id ScaleAlcove1, ScaleAlcove2, ScaleAlcove3 and ScaleAlcove4. I thought I could use a for statement to cycle through them, but it says I'm calling a global variable.
How do I make it recognize it as an entity id and not a variable?

Many thanks in advance.

edit: actual verbage attempt to index global 'ScaleAlcove' (a nil value)

Re: Searching through multiple alcoves?

Posted: Sat Feb 02, 2013 7:42 am
by Komag
because you can't do it that way. You're treating it like it's a table:

Code: Select all

scaleAlcove = {"red","blue","green","yellow"}
print(scaleAlcove[3]) -- this would print "green"
You need to find a way to treat the portion of the entity name as a string that you can concatenate, and a way to do that is with an entity search. Here is an example from my project

Code: Select all

function tictacLitOff()
  for i=1,9 do
    local j = findEntity("tictacLightBlue"..i)
    local k = findEntity("tictacLightYellow"..i)
    if j then j:deactivate() end
    if k then k:deactivate() end
  end
end
when you call findEntity you put in a string, and so you have the opportunity to cycle through a for loop with numbers to concatenate the string with each number, thus finding tictacLightBlue1, tictacLightBlue2, etc
(the reason I have the if j and if k parts is to check that they exist before telling it to deactivate them, so that just in case they don't exist it won't crash)

Code: Select all

function qtyCheck(punished)
local punished
local test = false  -- false means test failed
   for n = 1,4 do
      local SA = findEntity("ScaleAlcove"..n)
      if SA:getItemCount() ~= 3 then
         judgement(test)  -- send false to judgement function indicate fail
         return punished  -- should return true allThiingsEqual function
      end  -- and stop qtyCheck function
      test = true  -- indicate test passed
      judgement(test)
      return punished
   end
end
I have not much experience passing values though, so I'm not sure if there is any issue there. Good luck with it :)

Re: Searching through multiple alcoves?

Posted: Sat Feb 02, 2013 6:46 pm
by Ryeath_Greystalk
Thanks Komag, that resolved the issue of calling for the global.

Now I can't seem to get the variables to pass correctly. I'm going to play with it for a while and

if cannot get variables to pass correctly then
give up and go back to speghetti code.
end

just when I think I'm gaining a grip on this stuff I find out I still don't understand it.

Thanks again for all your help and patience.

Re: Searching through multiple alcoves?

Posted: Sat Feb 02, 2013 7:55 pm
by Ryeath_Greystalk
I figured I would add this here instaed of making a separte post since it's the same script. I have read the thread on local and global variables, and can't seem to figure out why some variables are passing and some aren't.

I commented out all the meat of the functions to concentrate on the passing variable. I will add comments on what I know for a fact is working in game (the editor at least)

Code: Select all

function allThingsEqual(HasBeenPunished)
qtyCheck()
-->I know the script is calling the qtycheck function

function qtyCheck(HBP_one)
local test = false
judgement(test)
-->I know the script is calling the judgement function and passing the boolean value of test to it.

function judgement(z)
local punished = false

if z == true then               
hudPrint("test is true")
else hudPrint("test is false") --test has failed
end
-->this is how I have been proving that test is being passed and assigned to the value z
-->if I change Test between true and false in qtycheck it reflects in this part

if z == false then
punished = true
hudPrint("All are not equal. You have been punished!")
else
hudPrint("All is equal, you have not been punished")
end

-->this section shows judgment is processing z and either changing punished to true or leaving punished false

if punished == true then
hudPrint("punished is true")
else hudPrint("punished is false")
end
-->this section shows punished is either true or false to prove it was evaulated

return punished
--> should return the boolean state of punished back to the qtycheck function
end 

function qtyCheck(HBP_one)
local test = false
judgement(test)

if HBP_one == true then
hudPrint("HBP_one is true in qtycheck")
else hudPrint("HBP_one is false in qtycheck")
end
end 
-->back in the qtycheck function. judgement should pass the state of punished to qtycheck in the variable HBP_one
-->however no matter what state punished is in judgement, true or false, HBP_one always prints false in qtycheck function
any ideas on what I'm doing wrong?

Again, thanks greatly in advance.

Re: Searching through multiple alcoves?

Posted: Sat Feb 02, 2013 8:26 pm
by Komag
That one makes my head spin! I'm sure I could wrap my brain around it if I worked on it, but I gotta work on my dungeon with the time I have.

Here's a little tiny example if it helps any:

Code: Select all

function lookForRock()
  if _floorSearch("rock",1,3,4) then print("found a rock!")
end

function _floorSearch(entity,floor,x,y)
  for i in entitiesAt(floor,x,y) do -- checks all the entities on floor 1 at x,y coordinates 3,4
    if i.name = entity then
       return true
    end
  end
end