TeamPorcupine / ProjectPorcupine

Project Porcupine: A Base-Building Game...in Space!
GNU General Public License v3.0
484 stars 278 forks source link

Is powerValue available in Lua? #436

Closed WilcoDW closed 8 years ago

WilcoDW commented 8 years ago

i am not too familliar with the codebase yet and was wondering if the powerValue variable is accesable through Lua code in the furniture? I cant really find anything that dictates what Lua code can use.

Thanks!

Sommerbo commented 8 years ago

there is a power-tag in furniture.xml that adds or subtracts power. Look at power generator and gas generator

78 and #257 Are the major code change for power

WilcoDW commented 8 years ago

Yes, but can you change that value during runtime?

Sommerbo commented 8 years ago

Doesn't looks like you can, except for building more machines

relinu commented 8 years ago

it's public so i think you can change it in lua otherwise add a method which changes the powerLevel

TheRiven commented 8 years ago

At the moment the only Lua connection I considered was being able to check if it had power or not. But last time I checked it was a public field... so you should be able to change it, i think?

TheRiven commented 8 years ago

and beaten to it :smile:

TheRiven commented 8 years ago

What were you planning on doing with it, may i ask?

WilcoDW commented 8 years ago

I just tried to do powerValue = 5

or 0, and MoonSharp said this:

ScriptRuntimeException: attempt to index a nil value
MoonSharp.Interpreter.Execution.VM.Processor.ExecIndex (MoonSharp.Interpreter.Execution.VM.Instruction i, Int32 instructionPtr)
MoonSharp.Interpreter.Execution.VM.Processor.Processing_Loop (Int32 instructionPtr)

I am going to work on implementing it, so i can change the power value

relinu commented 8 years ago

@wilcomega You need to do this:

function OnUpdate_SomeFurniture( furniture, deltaTime )
    furniture.powerValue = 5
end

Atleast it worked for me, but it cant power itself because in HasPower it checks if this is a PowerSupply and than returns false...

TheRiven commented 8 years ago

Yeah if its a consumer then it should have a negative value.

I am also not sure what effects changing the powerValue during play might have at the moment. So in that regards it will be an interesting experiment.

relinu commented 8 years ago

I used it on the oxygengenerator update function and set it's powerlevel to 5, itself had no power but it could power the metalsmelter ^^

TheRiven commented 8 years ago

haha, well that's one way to get more generators :laughing:

relinu commented 8 years ago

yeah more power but less oxygen ;D

TheRiven commented 8 years ago

There is always a price :smiling_imp:

TheRiven commented 8 years ago

hmm I suspect it will probably pay to put some checks in there somewhere for when the powerValue has been changed so that it updates things, as I don't think changing it at the moment will do anything useful.

At the moment once its in the list of power consumers then the only time it is removed is when you remove the object itself. Perhaps if the value is changed it should have to re-register the furniture with the power system to clean it up :thought_balloon:

relinu commented 8 years ago

Yeah i saw that too ;/

TheRiven commented 8 years ago

Well it was a basic implementation to get it running, I did not think about the powerValue being changed when I wrote it and I expected most of it to be changed sooner or later anyway :smile:

Grenkin1988 commented 8 years ago

@TheRiven @relinu I do not really get why it is necessary to change power Value on fly?

Sommerbo commented 8 years ago

I was wondering the same as @Grenkin1988

WilcoDW commented 8 years ago

a power generator might not always have the resources, like sunlight, coal or hydrogen needed to generate power, if not it will not generate any power and set its power value to 0, and some machines only need to request power when they have a job to do like smelt iron into steel.

relinu commented 8 years ago

A fusion reactor could first need power to start the fusion reaction and after that it generates power

Grenkin1988 commented 8 years ago

@wilcomega O.K. that makes sense. I am not familiar with lua how you someone going to change power Value? Just set new value directly? Can lua access properties? @TheRiven Would you do this? Or I could try to solve this (possibly I have solution)

TheRiven commented 8 years ago

As @wilcomega and @relinu said there are probably going to be times when the state of a machine will change, such as upgrades or fuel issues, and with it the power consumption/production.

My thinking was that the simplest solution might be to remove and then re-register the furniture with the power grid. I was looking into this but will need to look at again now that the code has been refactored.

But if you have possible solution @Grenkin1988 then I say give it a go :+1:

WilcoDW commented 8 years ago

466

In this pull request i made it so the Power Generator requires coal to operate, coal will also spawn now. in Furniture.cs i added a function that sets the power value, even though lua can already access powerValue directly.

Grenkin1988 commented 8 years ago

506 Made changes in order to enable power system to react on changes to powerValue

kd7uiy commented 8 years ago

I believe this should be closed.

vogonistic commented 8 years ago

Agreed, closing.