Page 1 of 1

[Lua] IsVisible Help

Posted: Thu Feb 25, 2010 7:54 pm
by Ace Rimmer
Per the function list, IsVisible =
IsVisible : unitId, byTeamId : bool

True iff given unit is visible to given team. In full information mode, visibility information about other teams is available. In limited information mode, only visible units are accessible


Lua Guide says:
IsVisible (unitID, teamID)
unitID:IsVisible (teamID)


Every which way I've used this, the game crashes. If understand this correctly, it's this...

IsVisible(unitID[not your own unit], [your own]TeamID), that is, can your own bot see the enemy unit you're referencing.

Even in chat, IsVisible causes a crash. Is it my code or the argument?!

Code: Select all

Defcon 4
        enemyBattleships = {}
        other code logs enemy units every second

...
Defcon 3

   local us = GetOwnTeamID()
   local eb = # enemyBattleships
   if # enemyBattleships > 0 then
      --if enemyBattleships[eb]:IsVisible (us) == true then
         SendChat(enemyBattleships[eb]:IsVisible (us))
      --end
   end

Posted: Thu Feb 25, 2010 8:17 pm
by roflamingo
Can you display boolean values (true/false)? I thought it wouldnt output them.

Posted: Thu Feb 25, 2010 8:21 pm
by Ace Rimmer
I don't know, but even if you can't I've had strings in the SendChat with the if statement un-commented and it still crashed.

Posted: Thu Feb 25, 2010 8:45 pm
by Montyphy
Doesn't cause Defcon to crash but SendChat wont accept boolean values. Either use tostring() on the returned value or use print() to output the value.

Are you certain that you're populating enemyBattleships with unitIDs?

Posted: Thu Feb 25, 2010 9:30 pm
by Ace Rimmer
It must be IsVisible causing it to crash because everytime I attempt to use it, Defcon crashes. As soon as I stop, everything works fine.

Code: Select all

   ---[[
   local us = GetOwnTeamID()
   local eb = # enemyBattleships

   if # enemyBattleships > 0 then
      --if enemyBattleships[eb]:IsVisible (us) == true then
         SendChat("yep, more than one BB")
         print(enemyBattleships[eb]:IsVisible(us))
      --end
   end
   --]]


Without the print(), I got my SendChat message. With the print(), crash.

Posted: Thu Feb 25, 2010 9:47 pm
by roflamingo
Exactly. You can evaluate if a condition == true, but you cannot print out 'true'. You would need an

if blah == true then
SendChat("stuff is true!)
else
SendChat("stuff is false!")
end

Posted: Thu Feb 25, 2010 9:50 pm
by Montyphy
roflamingo wrote:Exactly. You can evaluate if a condition == true, but you cannot print out 'true'. You would need an

if blah == true then
SendChat("stuff is true!)
else
SendChat("stuff is false!")
end


There's no need to do that when you can do either of the following:

Code: Select all

print(true)


Code: Select all

SendChat(tostring(true))


I recommend both of you create a test bot which is blank template so you can test simple ideas or portions of code. Something like this:

Code: Select all

-- Make require look in this folder for .lua files
package.path = debug.getinfo(1, "S").source:match[[^@?(.*[\/])[^\/]-$]] .."?.lua;".. package.path

-- Load up required files
require "utility"

function OnInit()
   SendChat "/name [Bot]TestBot"
end

function OnTick()
   OnFirstTick()
   OnTick = OnTickReal
   return OnTick()
end

function OnFirstTick()
end

function OnTickReal()
   WorkOnLongTasks()
   
   --only want to do this once
   if GetGameTick() == 60 then
      print(true)
      SendChat(tostring(true))
   end
end

function OnEvent(eventType, sourceID, targetID, unitType, longitude, latitude)
end

function OnShutdown()
end

Posted: Thu Feb 25, 2010 9:54 pm
by roflamingo
Right. I think we both got it :o

Posted: Thu Feb 25, 2010 10:03 pm
by Ace Rimmer

Code: Select all

   if # enemyBattleships > 0 then
      for i, unit in ipairs(enemyBattleships) do
         if unit:IsVisible (us) == true then
            SendChat("yep, more than one BB")
            print(true)
         end
      end
   end

Still crashes

Posted: Thu Feb 25, 2010 10:05 pm
by roflamingo
Why do you need to print(true)? You already know the condition is true... you could just print("true")...couldn't you?

Posted: Thu Feb 25, 2010 10:06 pm
by Ace Rimmer
roflamingo wrote:Why do you need to print(true)? You already know the condition is true... you could just print("true")...couldn't you?

I suppose that bit is redundant now. :P

Posted: Thu Feb 25, 2010 10:08 pm
by roflamingo
Montyphy wrote:I recommend both of you create a test bot which is blank template so you can test simple ideas or portions of code.


I think that's a great idea... but I have ~500 lines of code that work really well. It's when I elaborate into more complex things that things start to bog down...

That and I'm learning LUA as I go along.

I really appreciate your teaching and patience... you have no obligation to be as helpful as you are.

Posted: Thu Feb 25, 2010 10:17 pm
by Montyphy
Ace, what is the table populated with?

roflamingo wrote:Why do you need to print(true)? You already know the condition is true... you could just print("true")...couldn't you?


Was just a simple example to show that it handled boolean values without causing a problem :P

Posted: Thu Feb 25, 2010 10:23 pm
by Ace Rimmer
The enemyBattleships table is populated with unitID's of BB's other than mine. Perhaps I need FleetIDs in there instead of unitID's

This code happens every tick from Defcon 4 down:

Code: Select all

function CheckEnemyUnits()
   StartLongTask(function()
   local enemyUnits = GetAllUnits()   
   local us = GetOwnTeamID()
   for i, unit in ipairs(enemyUnits) do
      if unit:GetTeamID() ~= us then
         if  unit:GetUnitType () == "Silo" then
            local items = SetES (enemySilos)
            if items[unit] == nil then
               table.insert(enemySilos, unit)
               table.insert(targetCities, 1, unit)
               table.insert(silotargetCities, 1, unit)
            end
         elseif  unit:GetUnitType () == "AirBase" then
            local items = SetEA (enemyAirbases)
            if items[unit] == nil then
               table.insert(enemyAirbases, unit)
               table.insert(targetCities, 10, unit)
            end
         elseif  unit:GetUnitType () == "RadarStation" then
            local items = SetER (enemyRadars)
            if items[unit] == nil then
               table.insert(targetCities, 1, unit)
               table.insert(enemyRadars, unit)
               table.insert(silotargetCities, 1, unit)
            end
         elseif  unit:GetUnitType () == "BattleShip" then
            local items = SetEB (enemyBattleships)
            if items[unit] == nil then
               table.insert(enemyBattleships, unit)
            end
         elseif  unit:GetUnitType () == "Carrier" then
            local items = SetEC (enemyCarriers)
            if items[unit] == nil then
               table.insert(enemyCarriers, unit)
            end
         elseif  unit:GetUnitType () == "Sub" then
            local items = SetESb (enemySubs)
            if items[unit] == nil then
               table.insert(enemySubs, unit)
            end
         end
      end
   end
   YieldLongTask()
   --SendChat("Silos:" .. # enemySilos .. " Airbases:" .. # enemyAirbases .. " Radars:" .. # enemyRadars .. " BBs:" .. # enemyBattleships .. " CVs:" .. # enemyCarriers)
   end)
end         

Posted: Thu Feb 25, 2010 11:43 pm
by NeoThermic
roflamingo wrote:I really appreciate your teaching and patience... you have no obligation to be as helpful as you are.


Lies, we've got him locked in the basement with just the forums as an access to the outside world. If he answers, he gets food. If he does not, we don't feed him for one day... ;)

NeoThermic