Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
yannickkutil

Where is the flaw in the Code

Question

Hello, when I run the program I'm not getting the correct variable.

The variables remain at the default value, I do not know where the error.

Thank you for your help in advance.

 

local hardware = require("component")
local term = require("term")
local os = require("os")
local gpu = hardware.gpu

local zahl = 0
local AdresseGp = {["EVerbunden"]=0, ["EVorhanden"]=0, ["EDerzeit"]=0, ["ESpannung"]=0, ["ETick"]=0}

function coreUp()
local AdresseGp = {["EVerbunden"]=0, ["EVorhanden"]=0, ["EDerzeit"]=0, ["ESpannung"]=0, ["ETick"]=0}
for adresse, typName in hardware.list() do
	if typName == "mfsu" then
	AdresseGp["EVerbunden"] = AdresseGp["EVerbunden"] + 1
	AdresseGp["EVorhanden"] = AdresseGp["EVorhanden"] + hardware.proxy(adresse).getEUCapacity()
	AdresseGp["EDerzeit"] = AdresseGp["EDerzeit"] + hardware.proxy(adresse).getEUStored()
	AdresseGp["ESpannung"] = AdresseGp["ESpannung"] + hardware.proxy(adresse).getDemandedEnergy()
	AdresseGp["ETick"] = AdresseGp["ETick"] + hardware.proxy(adresse).getEUOutputPerTick()
end
end
end

term.clear()
while true do
term.clear()
coreUp()
term.setCursor(1,1)
if AdresseGp["EVerbunden"] <= 0 then
if zahl == 0 then
gpu.setBackground(0xFFFFFF)
gpu.setForeground(0xFF0000)
print("? Fehler: Es sind keine Devices Verbunden ---")
zahl = 1
else
gpu.setBackground(0x000000)
gpu.setForeground(0xFFFFFF)
print("? Fehler: Es sind keine Devices Verbunden ---")
zahl = 0
end
else
gpu.setBackground(0x000000)
gpu.setForeground(0xFFFFFF)
print("!Fehler: Quell Code in Beta!")
end
gpu.setBackground(0x000000)
gpu.setForeground(0xFFFFFF)
print("###########################################")
print("Energie Speicher Verbunden : "..AdresseGp["EVerbunden"])
print("Energie Speicher Insgesammt: "..AdresseGp["EVorhanden"])
print("Energie Speicher Derzeit   : "..AdresseGp["EDerzeit"])
print("Energie Akku Ladestand     : ".."n/a")
print("Energie Spannung           : "..AdresseGp["ESpannung"])
print("Energie Richtung  Derzeit  : "..AdresseGp["ETick"])
os.sleep(0.5)
end


Edited by Lizzy Trickster
fixed code tags
Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Hi,

 

Just to advise, you should indent your code, it's much easier to detect bugs.

 

This is what I found : you have 2 variables called 'AdresseGp'

When the 'coreUp()' function is executed, the local 'AdresseGp' of the function is used.

But when you access it out of the function, this is the local 'AdresseGp' of the program scope.

 

So i propose you to rewrite your function like this:

function coreUp(_AdresseGp)
    for adresse, typName in hardware.list() do
        if typName == "mfsu" then
            _AdresseGp["EVerbunden"] = _AdresseGp["EVerbunden"] + 1
            _AdresseGp["EVorhanden"] = _AdresseGp["EVorhanden"] + hardware.proxy(adresse).getEUCapacity()
            _AdresseGp["EDerzeit"] = _AdresseGp["EDerzeit"] + hardware.proxy(adresse).getEUStored()
            _AdresseGp["ESpannung"] = _AdresseGp["ESpannung"] + hardware.proxy(adresse).getDemandedEnergy()
            _AdresseGp["ETick"] = _AdresseGp["ETick"] + hardware.proxy(adresse).getEUOutputPerTick()
        end
    end
end

And use it like so:

coreUp(AdresseGp)

Full suggestion with changes

 

local hardware = require("component")
local term = require("term")
local os = require("os")
local gpu = hardware.gpu

local zahl = 0
local AdresseGp = {["EVerbunden"]=0, ["EVorhanden"]=0, ["EDerzeit"]=0, ["ESpannung"]=0, ["ETick"]=0}

function coreUp(_AdresseGp)
	for adresse, typName in hardware.list() do
		if typName == "mfsu" then
			_AdresseGp["EVerbunden"] = _AdresseGp["EVerbunden"] + 1
			_AdresseGp["EVorhanden"] = _AdresseGp["EVorhanden"] + hardware.proxy(adresse).getEUCapacity()
			_AdresseGp["EDerzeit"] = _AdresseGp["EDerzeit"] + hardware.proxy(adresse).getEUStored()
			_AdresseGp["ESpannung"] = _AdresseGp["ESpannung"] + hardware.proxy(adresse).getDemandedEnergy()
			_AdresseGp["ETick"] = _AdresseGp["ETick"] + hardware.proxy(adresse).getEUOutputPerTick()
		end
	end
end

term.clear()
while true do
	term.clear()
	coreUp(AdresseGp)
	term.setCursor(1,1)
	if AdresseGp["EVerbunden"] <= 0 then
		if zahl == 0 then
			gpu.setBackground(0xFFFFFF)
			gpu.setForeground(0xFF0000)
			print("? Fehler: Es sind keine Devices Verbunden ---")
			zahl = 1
		else
			gpu.setBackground(0x000000)
			gpu.setForeground(0xFFFFFF)
			print("? Fehler: Es sind keine Devices Verbunden ---")
			zahl = 0
		end
	else
		gpu.setBackground(0x000000)
		gpu.setForeground(0xFFFFFF)
		print("!Fehler: Quell Code in Beta!")
	end
	gpu.setBackground(0x000000)
	gpu.setForeground(0xFFFFFF)
	print("###########################################")
	print("Energie Speicher Verbunden : "..AdresseGp["EVerbunden"])
	print("Energie Speicher Insgesammt: "..AdresseGp["EVorhanden"])
	print("Energie Speicher Derzeit   : "..AdresseGp["EDerzeit"])
	print("Energie Akku Ladestand     : ".."n/a")
	print("Energie Spannung           : "..AdresseGp["ESpannung"])
	print("Energie Richtung  Derzeit  : "..AdresseGp["ETick"])
	os.sleep(0.5)
end

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.