Jump to content

fileRead client


Recommended Posts

Hello, I am asking about how to read a file in the server and get the data from it into client? I tried a lot of times but nothing happens at all.

Server : 

function getUpdates()
	local updates
	local file = "UpdatesLog.txt"
	local size = fileGetSize(file)  
	local openFile = fileOpen(file) 	
	if openFile then
		while not fileIsEOF(openFile) do
			updates = fileRead(openFile, size)
		end
		fileClose(openFile)
	end
	triggerClientEvent(client, "getUpdates", client, updates)
end

Client : 

	local key = "F1"
    updatesPanel = guiCreateWindow(195, 64, 986, 579, "Updates Changelog", false)
    guiWindowSetSizable(updatesPanel, false)

    updatesMemo = guiCreateMemo(10, 26, 966, 539, "", false, updatesPanel)    

	guiSetVisible(updatesPanel, false)
	
	guiSetInputMode("no_binds_when_editing")
		
function showAndCloseWindow(updates)
	if not guiGetVisible(updatesPanel) then
		guiSetVisible(updatesPanel, true)
		showCursor(true)
		guiSetText(updatesMemo, updates)
	else
		guiSetVisible(updatesPanel, false)
		showCursor(false)
		end
end
bindKey(key, "down", showAndCloseWindow)
addEvent("getUpdates", true)
addEventHandler("getUpdates", root, showAndCloseWindow)

 

Edited by JAVAAA
Link to comment
function getUpdates()
	local file = "UpdatesLog.txt"
	local openFile = fileOpen(file)
	if not openFile then outputChatBox("Error loading file") return false end
	local size = fileGetSize(openFile)
	local updates = fileRead(openFile, size)
	fileClose(openFile)
	triggerClientEvent(client, "getUpdates", client, updates)
end
2 minutes ago, Avagard said:

fileRead is a Shared function... why don't you use it on client?

Perhaps the file he wants to read is stored on the server?

Edited by MrTasty
Swapped lines 4 and 5
Link to comment
Just now, MrTasty said:

function getUpdates()
	local file = "UpdatesLog.txt"
	local openFile = fileOpen(file)
	local size = fileGetSize(openFile)
	if not openFile then outputChatBox("Error loading file") return false end
	local updates = fileRead(openFile, size)
	fileClose(openFile)
	triggerClientEvent(client, "getUpdates", client, updates)
end

Perhaps the file he wants to read is stored on the server?

 

Yeah exactly.

Link to comment
function getUpdates()
	local file = "UpdatesLog.txt"
	local openFile = fileOpen(file)
	local size = fileGetSize(openFile)
	if not openFile then outputChatBox("Error loading file") return false end
	local updates = fileRead(openFile, size+500)
	fileClose(openFile)
	triggerClientEvent(client, "getUpdates", client, updates)
end

It works fine for me when I create logs file for exemple.

Edited by Gordon_G
Link to comment

Didn't work yet, here's my code now:

Server:

function sayUpdates()
	local localTime = getRealTime(_,true)
	local day = localTime.monthday
	local month = localTime.month + 1
	local year = localTime.year + 1900 
	outputChatBox("#F2D200["..year..":"..month..":"..day.."]NEW UPDATES! CHECK F1 TO SEE LATEST UPDATES!", getRootElement(), 255, 255, 255, true)
end
addEventHandler("onResourceStart", getResourceRootElement ( getThisResource() ), sayUpdates)

function getUpdates()
	local file = "UpdatesLog.txt"
	local openFile = fileOpen(file)
	local size = fileGetSize(openFile)
	if not openFile then outputChatBox("Error loading file") return false end
	local updates = fileRead(openFile, size+500)
	fileClose(openFile)
	triggerClientEvent(client, "getUpdates", client, updates)
end

Client : 

--[[-------------------------------------------------
Notes:

> This code is using a relative image filepath. This will only work as long as the location it is from always exists, and the resource it is part of is running.
    To ensure it does not break, it is highly encouraged to move images into your local resource and reference them there.
--]]-------------------------------------------------


GUIEditor = {
    label = {}
}
	--local key = "F1"
    updatesPanel = guiCreateWindow(195, 64, 986, 579, "Updates Changelog", false)
    guiWindowSetSizable(updatesPanel, false)

    updatesMemo = guiCreateMemo(10, 26, 966, 539, "", false, updatesPanel)    

	guiSetVisible(updatesPanel, false)
	
	guiSetInputMode("no_binds_when_editing")
		
function showAndCloseWindow(command, state)
	if not guiGetVisible(updatesPanel) then
		guiSetVisible(updatesPanel, true)
		showCursor(true)
		getUpdates()
	else
		guiSetVisible(updatesPanel, false)
		showCursor(false)
		end
end
bindKey("F1", "down", showAndCloseWindow)



function getUpdates(updates)
	guiSetText(updatesMemo, updates)
end
addEvent("getUpdates", true)
addEventHandler("getUpdates", root, getUpdates)


Gives me an error saying "Bad argument guiSetText, argument 2 expected string got nil"

Link to comment
10 minutes ago, Gordon_G said:

You never call getUpdates()

function showAndCloseWindow(command, state)
	if not guiGetVisible(updatesPanel) then
		guiSetVisible(updatesPanel, true)
		showCursor(true)
		getUpdates()
	else
		guiSetVisible(updatesPanel, false)
		showCursor(false)
		end
end
bindKey("F1", "down", showAndCloseWindow)


function getUpdates(updates)
	guiSetText(updatesMemo, updates)
end
addEvent("getUpdates", true)
addEventHandler("getUpdates", root, getUpdates)

....

Link to comment
--client

-- [...]
function showAndCloseWindow(command, state)
	if not guiGetVisible(updatesPanel) then
		guiSetVisible(updatesPanel, true)
		showCursor(true)
		requestUpdates()
	else
		guiSetVisible(updatesPanel, false)
		showCursor(false)
		end
end
bindKey("F1", "down", showAndCloseWindow)

function requestUpdates() -- inquire for the updates text from the server
  triggerServerEvent("requestUpdates", localPlayer)
end

function getUpdates(updates)
	guiSetText(updatesMemo, updates)
end
addEvent("getUpdates", true)
addEventHandler("getUpdates", root, getUpdates)
-- [...]
--server

-- [...]
function getUpdates()
	local file = "UpdatesLog.txt"
	local openFile = fileOpen(file)
	if not openFile then outputChatBox("Error loading file") return false end
	local size = fileGetSize(openFile)
	local updates = fileRead(openFile, size+500)
	fileClose(openFile)
	triggerClientEvent(client, "getUpdates", client, updates)
end
addEvent("requestUpdates", true)
addEventHandler("requestUpdates", root, getUpdates)
-- [...]

 

Edited by MrTasty
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...