Jump to content

Need help on creating horse


Recommended Posts

Hello everyone, while im working on my script i have got a little problem about checking if the horse id exists on the farm or not, im lost through a lot of tables 

this part shows the creation of the table

farmID[id] = {}
		local f = {marker = marker, id=id, location={x, y, z}, owner = nil, creator=player, CreationTime = getTickCount(), price=price, horses={}}
		farm[id] = {}
		table.insert(farm[id], f)
horseID = {}
function createhorse(player, ID, farmid)
	if not ID and type(ID) ~= "number" then
		outputChatBox("Incorrect ID, correct command /createhorse [horse ID] [farm ID]", player, 255, 0,0)
		return false
	end

	if not farmid and type(farmid) ~="number" then
		outputChatBox("Incorrect farmID, correct command /createhorse [horse ID] [farm ID]", player, 255, 0,0)
		return false
	end

	if horseID[ID] then
		outputChatBox("horse ID already exists", player, 255 ,0 ,0)
		return false
	end
	if farm[farmid] then
		for i, v in ipairs(farm[farmid]) do
			if v.horses == ID then
				outputChatBox("this horse already exist in the farm", player, 255, 0,0)
			end
		end
	end

	if not farm[farmid] then
		outputChatBox("Farm ID doesnt exist", player, 255, 0,0)
		return false
	end
	
	if farm[farmid] and not horseID[ID] then
		for i, v in ipairs ( farm[farmid] ) do 
			if v.horses ~= horseID[ID] then
				local x, y, z = getElementPosition( player )
				local _, _, rz = getElementRotation( player )
				local horse = createPed( 0, x, y, z, rz)
				table.insert(v.horses,ID)
			else
				outputChatBox("ERROR #1", player, 255, 0,0)
			end
		end
	end

end

this works well but it doesnt work on the part of checking if the horse exists in the farm or not exactly this part 

for i, v in ipairs ( farm[farmid] ) do 
            if v.horses ~= horseID[ID] then
    		end
 end

thank you everyone for taking the time to read this

 

Link to comment
  • Moderators
21 hours ago, Snow-Man said:

im lost through a lot of tables 

It helps if you write every table modification inside of a new function. This way you keep your code more readable.

The following examples are re-written code. You could use this as a start, but make sure to test it, because I didn't test it for you.

 

local farm = createFarm(123, 1, 1, 1)

 

local horse = createHorse(player, horseId)

local farm = getFarmFromID (123)
-- If the farm does exist
if farm then
    addHorseToFarm(horse, farm)
end

 

 

local horseCollection = {} -- all horses
local farmCollection = {} -- all farms

---@param player userdata
---@param horseId integer
---@return table
function createHorse(player, horseId)

    local horse = { id = horseId }
    horseCollection[horseId] = horse

    return horse
end

---@param farmId integer
---@param x number
---@param y number
---@param z number
---@return table
function createFarm (farmId, x, y, z)

    local marker = createMarker(x, y, z)

    local farm =  {
        marker = marker, 
        id = farmId, 
        location = { x, y, z }, 
        owner = nil, 
        creator = nil, 
        creationTime = getTickCount(), 
        price = 0, 
        horses = {}
    }

    farmCollection[farmId] = farm
    return farm
end

---@param farmId integer
---@return table|false
function getFarmFromID (farmId)
    return farmCollection[farmId] or false
end

---@param farm table
---@param player userdata
---@return table
function setFarmCreator (farm, player)
    farm.creator = player
    return farm
end

---@param horse table
---@param farm table
function addHorseToFarm (horse, farm)
    table.insert(farm, horse)
end

---@param horse table
---@param farm table
---@return boolean
function isHorseInFarm(horse, farm)
    for i=1, #farm do
        if farm[i] == horse then return true end
    end
    return false
end

---@param farmId integer
---@return boolean
function doesFarmExist(farmId)
    return farmCollection[farmId] and true or false
end

 

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

It helps if you write every table modification inside of a new function. This way you keep your code more readable.

The following examples are re-written code. You could use this as a start, but make sure to test it, because I didn't test it for you.

 

local farm = createFarm(123, 1, 1, 1)

 

local horse = createHorse(player, horseId)

local farm = getFarmFromID (123)
-- If the farm does exist
if farm then
    addHorseToFarmHorse(horse, farm)
end

 

 

local horseCollection = {} -- all horses
local farmCollection = {} -- all farms

---@param player userdata
---@param horseId integer
---@return table
function createHorse(player, horseId)

    local horse = { id = horseId }
    horseCollection[horseId] = horse

    return horse
end

---@param farmId integer
---@param x number
---@param y number
---@param z number
---@return table
function createFarm (farmId, x, y, z)

    local marker = createMarker(x, y, z)

    local farm =  {
        marker = marker, 
        id = farmId, 
        location = { x, y, z }, 
        owner = nil, 
        creator = nil, 
        creationTime = getTickCount(), 
        price = 0, 
        horses = {}
    }

    farmCollection[farmId] = farm
    return farm
end

---@param farmId integer
---@return table|false
function getFarmFromID (farmId)
    return farmCollection[farmId] or false
end

---@param farm table
---@param player userdata
---@return table
function setFarmCreator (farm, player)
    farm.creator = player
    return farm
end

---@param horse table
---@param farm table
function addHorseToFarm (horse, farm)
    table.insert(farm, horse)
end

---@param horse table
---@param farm table
---@return boolean
function isHorseInFarm(horse, farm)
    for i=1, #farm do
        if farm[i] == horse then return true end
    end
    return false
end

---@param farmId integer
---@return boolean
function doesFarmExist(farmId)
    return farmCollection[farmId] and true or false
end

 

This reply is actually beautiful and could quite easily be it's own little thread, you have explained it much better than I ever could have there and showed the right way to achieve things like this. 

One of the biggest headaches I faced as a new coder many years ago was super duper nesting myself and creating messes.

Edited by TMTMTL
  • Like 2
Link to comment
  • Moderators
6 hours ago, Snow-Man said:

what confuses me that when i get table into another tables inside and when i try to get a return from what?

 

Sometimes it really helps when you use the iprint function to print the current table. It shows you the current depth and helps you to decide how to index the next table.

 

When I for example I am working with a secret table.

iprint(result)

>

{
	["test"] = {
		{
			{
				[133]="found me!"
			}
		}
	}
}

 

The first step is to peel it down.

 

Layer 1

iprint(result["test"])

>

{
	{
		{
			[133]="found me!"
		}
	}
}

 

Layer 2

iprint(result["test"][1])

>

{
	{
		[133]="found me!"
	}
}

 

Layer 3

iprint(result["test"][1][1])

>

{
	[133]="found me!"
}

 

Layer 4

iprint(result["test"][1][1][133])

>

"found me!"

 

 

 

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