How to use a variable from another file of another folder.

Hi.

First , yes i know there is 999999 thread about it , but actually i’m completly lost.
When i try to reproduce something it never works.

Actually that’s what i’ve :

https://image.noelshack.com/fichiers/2017/44/3/1509569455-dossiers.png

So actually i want call a variable that is in the “cl_init.lua” file of the “Bucket” folder called BWaterAmount
in to the “init.lua” file of the “Water Container” folder.

cl_init.lua of Bucket :


include("shared.lua")

BWaterAmount = 2

surface.CreateFont( "TheDefaultSettings", {
	font = "Arial", -- Use the font-name which is shown to you by your operating system Font Viewer, not the file name
	extended = true,
	size = 40,
	weight = 500,
	blursize = 0,
	scanlines = 0,
	antialias = true,
	underline = false,
	italic = false,
	strikeout = false,
	symbol = false,
	rotary = false,
	shadow = true,
	additive = false,
	outline = false,
} )

function ENT:Draw()

	self.Entity:DrawModel()

	cam.Start3D2D(self:GetPos() + Vector(0,-9,10), Angle(0, 0, 90), 0.1)

		draw.DrawText("Bucket","TheDefaultSettings",0,-15,Color(120,120,255),1,1)
		draw.DrawText("Water"..BWaterAmount,"TheDefaultSettings",0,45,Color(240,195,0),1,1)

	cam.End3D2D()

end


init.lua of Water Container :



AddCSLuaFile('cl_init.lua')
AddCSLuaFile('shared.lua')

include('shared.lua')

function ENT:Initialize( )
 
	self:SetModel( "models/props_borealis/bluebarrel001.mdl" )
	self:PhysicsInit( SOLID_VPHYSICS ) 
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid(  SOLID_BBOX ) 
	self:SetUseType( SIMPLE_USE )
	self:DrawShadow( false )
end
  
function ENT:Touch( ent )
	if ent:IsPlayer() then
		local effectdata = EffectData()
		effectdata:SetOrigin( self:GetPos() )
		effectdata:SetScale(1)
	end
end

function ENT:StartTouch(ent)
	if ent:GetClass() == "bucket" then
	print ("Water:"..BWaterAmount)
	end
end


So actually what should i add on these 2 files ?

Variables can’t be shared between realms like that: cl_init is clientside and init is serverside. You’ll have to network the variable on the entity using a https://wiki.garrysmod.com/page/Entity/NetworkVar

Still the same problem , the variable is not found :

https://image.noelshack.com/fichiers/2017/44/4/1509645335-error-console.png

THat’s what i did :

In the Shared.lua of Bucket



ENT.Base = "base_ai" 
ENT.Type = "ai"
ENT.PrintName		= "Sceau" --<-- Nom de votre NPC
ENT.Category		= "Fleuriste" --<-- Catégorie dans laquelle il sera affiché
ENT.Spawnable		= true --<-- Si true alors les joueurs peuvent le faire spawn / si false uniquement les admins le peuvent
ENT.AdminSpawnable		= true --<-- Les admin uniquement ("ULX") peuvent le faire spawn



function ENT:SetupDataTables()

	self:NetworkVar("Int",1,"BWaterAmount")
	
end

return BWaterAmount


In the init.lua of Water Container



AddCSLuaFile('cl_init.lua')
AddCSLuaFile('shared.lua')

include('shared.lua')

local BWaterAmount = require 'shared'

function ENT:Initialize( )
 
	self:SetModel( "models/props_borealis/bluebarrel001.mdl" )
	self:PhysicsInit( SOLID_VPHYSICS ) 
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid(  SOLID_BBOX ) 
	self:SetUseType( SIMPLE_USE )
	self:DrawShadow( false )
end
  
function ENT:Touch( ent )
	if ent:IsPlayer() then
		local effectdata = EffectData()
		effectdata:SetOrigin( self:GetPos() )
		effectdata:SetScale(1)
	end
end

function ENT:StartTouch(ent)
	if ent:GetClass() == "sceau" then
	print ("Water:"..BWaterAmount)
	end
end




So i guess i probably did not do well the Water Container script.

If you actually read the wiki page on it, you would see once you create the networked variable, you then just use the Get and Set of that variable you created like so


self:GetBWaterAmount()

self:SetBWaterAmount(int)

I’ve tried this



include("shared.lua")


function ENT:SetupDataTables()

	self:NetworkVar("Int",1,"BWaterAmount")

	self:SetBWaterAmount(3)

end



surface.CreateFont( "TheDefaultSettings", {
	font = "Arial", -- Use the font-name which is shown to you by your operating system Font Viewer, not the file name
	extended = true,
	size = 40,
	weight = 500,
	blursize = 0,
	scanlines = 0,
	antialias = true,
	underline = false,
	italic = false,
	strikeout = false,
	symbol = false,
	rotary = false,
	shadow = true,
	additive = false,
	outline = false,
} )

function ENT:Draw()

	self.Entity:DrawModel()

	cam.Start3D2D(self:GetPos() + Vector(0,-9,10), Angle(0, 0, 90), 0.1)

		draw.DrawText("Sceau","TheDefaultSettings",0,-15,Color(120,120,255),1,1)
		draw.DrawText("Eau:"..self:GetBWaterAmount() ,"TheDefaultSettings",0,45,Color(240,195,0),1,1)

	cam.End3D2D()
end





AddCSLuaFile('cl_init.lua')
AddCSLuaFile('shared.lua')

include('shared.lua')


function ENT:Initialize( )
 
	self:SetModel( "models/props_borealis/bluebarrel001.mdl" )
	self:PhysicsInit( SOLID_VPHYSICS ) 
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid(  SOLID_BBOX ) 
	self:SetUseType( SIMPLE_USE )
	self:DrawShadow( false )
end
  
function ENT:Touch( ent )

	if ent:IsPlayer() then
		local effectdata = EffectData()
		effectdata:SetOrigin( self:GetPos() )
		effectdata:SetScale(1)
	end
end

function ENT:StartTouch(ent)
	if ent:GetClass() == "sceau" then
	print ("Water:"..self:GetBWaterAmount())
	end
end



But it’s not working , i tried some things but i don’t find what’s the error

You have to do ENT:GetBWater not self:GetBWater

When i’m writing


print ("Water:"..ent:GetBWaterAmount())

I’ve this console error :

https://image.noelshack.com/fichiers/2017/44/5/1509716235-azefzef.png

And with


print ("Water:"..ENT:GetBWaterAmount())

It’s saying : =

https://image.noelshack.com/fichiers/2017/44/5/1509716265-kiikik.png

Maybe try moving



self:SetBWaterAmount(3)


To ENT:Initialize, and leave the print as



print ("Water:"..self:GetBWaterAmount())


[editline]3rd November 2017[/editline]

Try removing the class check from the if statement so it’s



function ENT:StartTouch( ent )
	print( "Water:" .. self:GetBWaterAmount() )
end


When i’m writing this

https://image.noelshack.com/fichiers/2017/44/5/1509719149-bug.png

model bug
**
edit ** Deleting the class check don’t change

Read your own code, i meant ent (phone changed it to ENT for some reason)
You didn’t define the network var inside the second class where you run the print, that’s what the error means

I’m not really sure if you should initialize the function inside SetupDataTable, although doing in Initialize is a bad practise since it will break pocket system

I don’t understand . Should i do this ? :



include("shared.lua")

function ENT:SetupDataTables()

	self:NetworkVar("Int",1,"BWaterAmount")
	self:SetBWaterAmount(5)
end

surface.CreateFont( "TheDefaultSettings", {
	font = "Arial", -- Use the font-name which is shown to you by your operating system Font Viewer, not the file name
	extended = true,
	size = 40,
	weight = 500,
	blursize = 0,
	scanlines = 0,
	antialias = true,
	underline = false,
	italic = false,
	strikeout = false,
	symbol = false,
	rotary = false,
	shadow = true,
	additive = false,
	outline = false,
} )

function ENT:Draw()

	self.Entity:DrawModel()

	cam.Start3D2D(self:GetPos() + Vector(0,-9,10), Angle(0, 0, 90), 0.1)

		draw.DrawText("Sceau","TheDefaultSettings",0,-15,Color(120,120,255),1,1)
		draw.DrawText("Eau:"..self:GetBWaterAmount(),"TheDefaultSettings",0,45,Color(240,195,0),1,1)

	cam.End3D2D()
end



AddCSLuaFile('cl_init.lua')
AddCSLuaFile('shared.lua')

include('shared.lua')

function ENT:SetupDataTables()
	self:NetworkVar("Int",1,"BWaterAmount")
	self:SetBWaterAmount(5)
end

function ENT:Initialize()
	self:SetModel( "models/props_borealis/bluebarrel001.mdl" )
	self:PhysicsInit( SOLID_VPHYSICS ) 
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid(  SOLID_BBOX ) 
	self:SetUseType( SIMPLE_USE )
	self:DrawShadow( false )
end
  
function ENT:Touch( ent )

	if ent:IsPlayer() then
		local effectdata = EffectData()
		effectdata:SetOrigin( self:GetPos() )
		effectdata:SetScale(1)
	end
end

function ENT:StartTouch(ent)
	print ("Water:"..ent:GetBWaterAmount())
	end

There is the same Console error

I think i’ve a brain problem

Show us the error and the line it shows the error
Also you are not checking if touching entity has GetBWaterAmount method, like it can be the world and anything, also add inside initialize self:SetTrigger(true)

https://image.noelshack.com/fichiers/2017/44/5/1509744402-aggggg.png

that’s what i get when i touch the entity

And line 31 is…

Also read what I’ve wrote you (since I’m trying to help you)
You are not checking the entity at all, if you touch the ground, it will error, if you touch a prop, it will error

If you want to program, YOU HAVE TO UNDERSTAND ERRORS, otherwise you won’t ever be able to fix those
The error is saying that the networked car you are creating in one entity is not valid when the other entity touches something

Ok , sorry
So the line 31 is

print (“Water:”…ent:GetBWaterAmount())

I don’t really understand , like …
Normally it should work.

I’ve the networked variable that i’m calling well

so you mean that there is an error in something that has nothing to do with the variable ?
I don’t understand what you mean by " if you touch the ground, it will error, if you touch a prop, it will error "

Actually what i understand from this error is that it don’t recognize the variable because it’s a “nil value”.

So for recap i’ve this :

https://image.noelshack.com/fichiers/2017/44/6/1509754186-liililililil.png

https://image.noelshack.com/fichiers/2017/44/6/1509754226-oiooiio.png

Look, StartTouch gets called everytime an entity touchs your entity, do you follow me with that?

You have a bucket, you’re expecting water entity, and water has it capability called “Amount”
Everytime something touches the bucket, you’re asking to the touching entity the amount

-Hey floor, what’s your amount, said the bucket
-What? Replied the floor trhowing some lua errors

You’re not verifying never that the entity you’re touching ACTUALLY is an entity with amount capability
You have to understand that, i just can give you the “working” code, but you won’t still understand the implementation at your own if i do that

First it’s not the bucket but the water container that ask what the amount
Also , there isn’t anything about " water has it capability called “Amount” " but just that the variable is called BWaterAmount

And the script is not asking what is the value/amount of the entity that is touching but for the value of the variable “BWaterAmount”.

But if i retake a previous line that i had , and that someone told me to take off :

if ent:GetClass() == “sceau” then

( sceau is bucket in french )

i guess it should normally work :

If the bucket touch the Water Container , then it will print BWaterAmount.

But actually still the same error when the Bucket touch the Water Container :

https://image.noelshack.com/fichiers/2017/44/6/1509802601-ppopopopopo.png

So don’t give me the script at the moment but can you tell me if i’m right.

I gave you a theorical example about what’s happening and why the error happens
As programer is your work to understand the situation and what’s happening behind scenes

You had to check the class because not all entities in game that can touch your entity has BWaterAmount function since it’s a networked function inside sceau entity that gets created in SetupDataTable

I don’t understand what are you doing, but by looking at the picture, you’re touching your “conteneur” with a prop and it will error since it’s asking for BWaterAmount and props doesn’t have that, while the sceau that’s on the floor has the function, as i’ve said before, you have to understand the error

It clearly says BWaterAmount doesn’t exists there, where? Check the error line, inside StartTouch? Well, that means you’re trying to call BWaterAmount on the entity you’re touching
If you can’t get that well, you should consider trying to rewrite the code and making it step by step about what you’re looking to make

Ok . Thanks for the help anyways .

I’m gonna try to dot it : p

Hi , after 1 week i still don’t find the solution of my problem , i did some changes but it’s not enough.

Cl_init.lua of the Water Container :



include("shared.lua")


function ENT:SetupDataTables()


	self:NetworkVar( "Int", 0, "Water" )


end

surface.CreateFont( "TheDefaultSettings", {
	font = "Arial", -- Use the font-name which is shown to you by your operating system Font Viewer, not the file name
	extended = true,
	size = 40,
	weight = 500,
	blursize = 0,
	scanlines = 0,
	antialias = true,
	underline = false,
	italic = false,
	strikeout = false,
	symbol = false,
	rotary = false,
	shadow = true,
	additive = false,
	outline = false,
} )

function ENT:Draw()

local Water = self:GetWater()
	
	self.Entity:DrawModel()


	cam.Start3D2D(self:GetPos() + Vector(0,-15,25) , Angle(0,0,75) , 0.1)
		draw.DrawText("Conteneur D'eau","TheDefaultSettings",-10,0,Color(120,120,255),1,1)
		draw.DrawText("Eau:"..Water,"TheDefaultSettings",-120,80,Color(255,127,80))
	cam.End3D2D()

end




shared.lua of the Water Container :




ENT.Base = "base_ai" 
ENT.Type = "ai"
ENT.PrintName		= "Conteneur D'eau" --<-- Nom de votre NPC
ENT.Category		= "Fleuriste" --<-- Catégorie dans laquelle il sera affiché
ENT.Spawnable		= true --<-- Si true alors les joueurs peuvent le faire spawn / si false uniquement les admins le peuvent
ENT.AdminSpawnable		= true --<-- Les admin uniquement ("ULX") peuvent le faire spawn

function ENT:SetupDataTables()

	self:NetworkVar( "Int", 0, "Water" )

	self:SetWater( 4 )

end


cl_init.lua of the Bucket :



include("shared.lua")

function ENT:SetupDataTables()


	self:NetworkVar( "Int", 0, "Water" )


end


surface.CreateFont( "TheDefaultSettings", {
	font = "Arial", -- Use the font-name which is shown to you by your operating system Font Viewer, not the file name
	extended = true,
	size = 40,
	weight = 500,
	blursize = 0,
	scanlines = 0,
	antialias = true,
	underline = false,
	italic = false,
	strikeout = false,
	symbol = false,
	rotary = false,
	shadow = true,
	additive = false,
	outline = false,
} )

function ENT:Draw()

local Water = self:GetWater()

	self.Entity:DrawModel()

	cam.Start3D2D(self:GetPos() + Vector(0,-9,10), Angle(0, 0, 90), 0.1)
		draw.DrawText("Eau:"..Water,"TheDefaultSettings",-120,80,Color(255,127,80))
		draw.DrawText("Sceau","TheDefaultSettings",0,-15,Color(120,120,255),1,1)

	cam.End3D2D()
end


shared.lua of the Bucket :



ENT.Base = "base_ai" 
ENT.Type = "ai"
ENT.PrintName		= "Sceau" --<-- Nom de votre NPC
ENT.Category		= "Fleuriste" --<-- Catégorie dans laquelle il sera affiché
ENT.Spawnable		= true --<-- Si true alors les joueurs peuvent le faire spawn / si false uniquement les admins le peuvent
ENT.AdminSpawnable		= true --<-- Les admin uniquement ("ULX") peuvent le faire spawn

function ENT:SetupDataTables()

	self:NetworkVar( "Int", 0, "Water" )

end


So normally if the variable was correctly networked , it would do a 3D2D text with “Eau : 4” on the Bucket

but it’s not :

https://image.noelshack.com/fichiers/2017/45/7/1510444772-bucket.png

https://image.noelshack.com/fichiers/2017/45/7/1510444892-water-container.png

So it’s not anymore an “error” , but that i don’t understand how to network the variable , or how to receive it .