Jump to content

index skipping in loop


βurak

Recommended Posts

hi, can we move to the next index if there is an invalid condition in the loop?

example:

for i,v in pairs(table_name) do
   if(not condition) then --if the condition is not met
      --don't go any further. and go to next index
   end
end

 

Link to comment

 i think u have to use

table_name = {"A",1,2,3,"A"} 

for i=1,#table_name do -- it loops through all the indexes
	if table_name[i] == "A" then  -- example
		outputDebugString("This index has useful value")
	end
end 

I am not very advanced in tables but u should go for ipairs if u want to find a certain value in a table. Also check this topic it might be useful.

->  https://forum.multitheftauto.com/topic/36318-ipairs-and-pairs/

 

  • Like 1
Link to comment

Thanks for your answer, it's okay to find the values in the table, the problem is that I want to make zombie artificial intelligence, zombies will be stored in the table, I will update the artificial intelligence with the timer, but here there will be nested if structures and a complex one will appear.

something like this will happen

function updateAI()
   if(not isInWater and not dead and not moving) then
      --do something
   end
end

--or
function updateAI()
   if(not isInWater) then
      if(not dead) then
         if(not moving) then
            --do something
         end
      end
   end
end

in this case I can't use commands like "return" "break" because all zombies have to be updated so I have to go to the next index by the shortest path

maybe I don't know my way of thinking is wrong but right now I can't see any way out

Edited by Burak5312
Link to comment
  • Moderators
53 minutes ago, Burak5312 said:

maybe I don't know my way of thinking is wrong but right now I can't see any way out

AI thinking is not linear unfortunately. But some tasks are, and yet those probably can be interrupted in some way. Therefore it is important to run some of the checks every cycle.

 

You could give something like this a try:

local dataStorage_AI = {}

-- Give the ped a brain if it hasn't one yet.
function init_AI (ped)
	if dataStorage_AI[ped] then return end
	dataStorage_AI[ped] = {
		controls = {}, -- pressed controls maybe? (since serverside is not aware)
		element = ped
  	}
end

-- Run the main update cycle
function update_AI(ped) 
  	if not dataStorage_AI[ped] then return end -- no storage, no brain...
	if isPedDead(ped) then return end
	if isElementInWater(ped) then 
		return runPedInWaterInstructions (ped)
  	end
end

-- If the ped is in the water, do stuff here
function runPedInWaterInstructions (ped)
	local pedData =  dataStorage_AI[ped]
  	-- do something here
end

 

 

  • Like 1
Link to comment
26 minutes ago, IIYAMA said:

AI thinking is not linear unfortunately. But some tasks are, and yet those probably can be interrupted in some way. Therefore it is important to run some of the checks every cycle.

 

You could give something like this a try:

local dataStorage_AI = {}

-- Give the ped a brain if it hasn't one yet.
function init_AI (ped)
	if dataStorage_AI[ped] then return end
	dataStorage_AI[ped] = {
		controls = {}, -- pressed controls maybe? (since serverside is not aware)
		element = ped
  	}
end

-- Run the main update cycle
function update_AI(ped) 
  	if not dataStorage_AI[ped] then return end -- no storage, no brain...
	if isPedDead(ped) then return end
	if isElementInWater(ped) then 
		return runPedInWaterInstructions (ped)
  	end
end

-- If the ped is in the water, do stuff here
function runPedInWaterInstructions (ped)
	local pedData =  dataStorage_AI[ped]
  	-- do something here
end

 

 

yes i was really thinking wrong separating it into functions is a good idea than writing all the artificial intelligence in one place thanks IIYAMA 👍

I have another question, which one do you think I should use to store the peds, I'm undecided between these two but do you have a better idea?
local allpeds = {}
local testped = createPed(0, 0,0,3)
allpeds[testped] = true

--or
local allpeds = {}
local testped = createPed(0, 0,0,3)
table.insert(allpeds, testped)

 

  • Like 1
Link to comment
  • Moderators
2 hours ago, Burak5312 said:

I'm undecided between these two but do you have a better idea?

 

2 hours ago, Burak5312 said:
local allpeds = {}
local testped = createPed(0, 0,0,3)
allpeds[testped] = true

For easy and fast management.

 

2 hours ago, Burak5312 said:
local allpeds = {}
local testped = createPed(0, 0,0,3)
table.insert(allpeds, testped)

For looping speed. (complexer management)

 

Note: you can also go for both.

 


 

Or you could go for this one:

https://wiki.multitheftauto.com/wiki/CreateElement (as parent)

https://wiki.multitheftauto.com/wiki/SetElementParent

 

Benefits:

  • No need to clean up after a ped is deleted.
  • Able to get specific streamedin peds at clientside.
  • Attach eventHandlers to all your peds and only those.
  • (Whipe all peds with just one destroyElement call > propagation.)

 

  • Like 1
Link to comment
2 hours ago, IIYAMA said:

 

For easy and fast management.

 

For looping speed. (complexer management)

 

Note: you can also go for both.

 


 

Or you could go for this one:

https://wiki.multitheftauto.com/wiki/CreateElement (as parent)

https://wiki.multitheftauto.com/wiki/SetElementParent

 

Benefits:

  • No need to clean up after a ped is deleted.
  • Able to get specific streamedin peds at clientside.
  • Attach eventHandlers to all your peds and only those.
  • (Whipe all peds with just one destroyElement call > propagation.)

 

very descriptive thank you 👍

Edited by Burak5312
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...