C
C#14mo ago
kokars

❔ List string variables

Hello, I'm really new to C# and would like to get help. How is the correct way to give variable for each case name? So it would display like - "WOOD x1" "STONE x2" etc... My code is below. This is for Rust game plugin.
private void CreateButtons(BasePlayer player, BaseContainer parent, BuildingPrivlidge tc)
{
// Title
TextContainer.Create(parent, Anchor.TopLeft, new Offset(60f, -50f, 210f, -3f))
.WithSize(15)
.WithText("BASE UPGRADE")
.WithOutline(m_OutlineWhite);


List<string> u_ButtonNames = new List<string> { "WOOD " + "(resource)", "STONE " + "(resource)", "METAL " + "(resource)", "HQM " + "(resource)" };

// Button Container
ImageContainer.Create(parent, Anchor.FullStretch, new Offset(5f, 5f, -5f, -25f))
.WithStyle(m_PanelStyle)
.WithLayoutGroup(u_ButtonLayout, u_ButtonNames, 0, (int i, string t, BaseContainer buttons, Anchor anchor, Offset offset) =>
{
ImageContainer.Create(buttons, anchor, offset)
.WithStyle(m_ButtonStyle)
.WithChildren(header =>
{
TextContainer.Create(header, Anchor.FullStretch, Offset.zero)
.WithText(t)
.WithAlignment(TextAnchor.MiddleCenter);

ButtonContainer.Create(header, Anchor.FullStretch, Offset.zero)
.WithColor(Color.Clear)
.WithCallback(m_CallbackHandler, arg =>
{
switch (t)
{
case "WOOD": rust.RunClientCommand(player, "chat.say", "/up 1"); break;
case "STONE": rust.RunClientCommand(player, "chat.say", "/up 2"); break;
case "METAL": rust.RunClientCommand(player, "chat.say", "/up 3"); break;
case "HQM": rust.RunClientCommand(player, "chat.say", "/up 4"); break;
}
}, $"{player.UserIDString}.{t}");
});
});
}
private void CreateButtons(BasePlayer player, BaseContainer parent, BuildingPrivlidge tc)
{
// Title
TextContainer.Create(parent, Anchor.TopLeft, new Offset(60f, -50f, 210f, -3f))
.WithSize(15)
.WithText("BASE UPGRADE")
.WithOutline(m_OutlineWhite);


List<string> u_ButtonNames = new List<string> { "WOOD " + "(resource)", "STONE " + "(resource)", "METAL " + "(resource)", "HQM " + "(resource)" };

// Button Container
ImageContainer.Create(parent, Anchor.FullStretch, new Offset(5f, 5f, -5f, -25f))
.WithStyle(m_PanelStyle)
.WithLayoutGroup(u_ButtonLayout, u_ButtonNames, 0, (int i, string t, BaseContainer buttons, Anchor anchor, Offset offset) =>
{
ImageContainer.Create(buttons, anchor, offset)
.WithStyle(m_ButtonStyle)
.WithChildren(header =>
{
TextContainer.Create(header, Anchor.FullStretch, Offset.zero)
.WithText(t)
.WithAlignment(TextAnchor.MiddleCenter);

ButtonContainer.Create(header, Anchor.FullStretch, Offset.zero)
.WithColor(Color.Clear)
.WithCallback(m_CallbackHandler, arg =>
{
switch (t)
{
case "WOOD": rust.RunClientCommand(player, "chat.say", "/up 1"); break;
case "STONE": rust.RunClientCommand(player, "chat.say", "/up 2"); break;
case "METAL": rust.RunClientCommand(player, "chat.say", "/up 3"); break;
case "HQM": rust.RunClientCommand(player, "chat.say", "/up 4"); break;
}
}, $"{player.UserIDString}.{t}");
});
});
}
186 Replies
kokars
kokars14mo ago
At the moment it just displays resources as text not values. those values are comming from another file.
ero
ero14mo ago
i find it hard to understand what you're asking here
TheRanger
TheRanger14mo ago
same
kokars
kokars14mo ago
This line List<string> u_ButtonNames = new List<string> { "WOOD " + "(resource)", "STONE " + "(resource)", "METAL " + "(resource)", "HQM " + "(resource)" }; WOOD, STONE ... are names for case break How to pass variable in that name?
TheRanger
TheRanger14mo ago
what variable?
kokars
kokars14mo ago
from another file
ero
ero14mo ago
so are you really asking how to read the contents of a file...?
kokars
kokars14mo ago
kokars
kokars14mo ago
What you mean how to read contents?
ero
ero14mo ago
exactly what i said
kokars
kokars14mo ago
I want to know how to pass variable in new List
TheRanger
TheRanger14mo ago
what does variable mean to you?
kokars
kokars14mo ago
changing number
ero
ero14mo ago
if you already have the variable, you'd just do $"WOOD x{amount}"
kokars
kokars14mo ago
thanks
ero
ero14mo ago
which is crazy if that's actually what you wanted to know because you already use interpolated strings here $"{player.UserIDString}.{t}"
kokars
kokars14mo ago
I have only been working with C# like 2/3 days. Sorry I'm just a fronend developer Do you have spare time now? I can show other file what I'm trying to get
kokars
kokars14mo ago
uMod - Building Grades by Dana
Allows players to easily upgrade or downgrade an entire building
kokars
kokars14mo ago
This is game plugin where my button executes this command - rust.RunClientCommand(player, "chat.say", "/up 1"); ... Here is just plugin when user writes in chat like /up 1 /up 2 etc...
TheRanger
TheRanger14mo ago
are u trying to display Wood x1 instead of Wood resource here?
kokars
kokars14mo ago
I'm trying to display
wood x
wood x
,
stone x
stone x
TheRanger
TheRanger14mo ago
so just wood x not Wood x1 ?
kokars
kokars14mo ago
yes
kokars
kokars14mo ago
kokars
kokars14mo ago
From that buildingGrade plugin I get in chat this x value
TheRanger
TheRanger14mo ago
where are the names coming from?
kokars
kokars14mo ago
But I want to get it on button What names?
TheRanger
TheRanger14mo ago
the names that are written in the buttons in ur image
kokars
kokars14mo ago
from this?
TheRanger
TheRanger14mo ago
from this
kokars
kokars14mo ago
names are from here { $"WOOD x{amount}", $"WOOD x{amount}", $"WOOD x{amount}", $"WOOD x{amount}" };
TheRanger
TheRanger14mo ago
thats weird, where is STONE, METAL and HQM?
kokars
kokars14mo ago
yes
TheRanger
TheRanger14mo ago
?
kokars
kokars14mo ago
out sorry
kokars
kokars14mo ago
kokars
kokars14mo ago
this is original
TheRanger
TheRanger14mo ago
where is STONE, METAL and HQM?
kokars
kokars14mo ago
kokars
kokars14mo ago
Sorry I edited with tha x{amount} and didnt change names
TheRanger
TheRanger14mo ago
then why is resource written next to WOOD here?
kokars
kokars14mo ago
kokars
kokars14mo ago
now it's back where it was before
TheRanger
TheRanger14mo ago
ok so apply string interpolation on ur list
kokars
kokars14mo ago
I execute with button this command - case "WOOD": rust.RunClientCommand(player, "chat.say", "/up 1"); break; for stone, metal and hqm too And I want to display in each button text + value From that command
kokars
kokars14mo ago
kokars
kokars14mo ago
TheRanger
TheRanger14mo ago
List<string> u_ButtonNames = new List<string> { $"WOOD x{woodAmount}", $"STONE x{stoneAmount}", $"METAL x{metalAmount}", $"HQM x{hqmAmount}" };
kokars
kokars14mo ago
That "chat.say", "/up 1.2.3.4" executes from this file
kokars
kokars14mo ago
uMod - Building Grades by Dana
Allows players to easily upgrade or downgrade an entire building
TheRanger
TheRanger14mo ago
what file, this?
{
"Settings": {
"Use Teams": false,
{
"Settings": {
"Use Teams": false,
kokars
kokars14mo ago
yes buildingGrade.cs
kokars
kokars14mo ago
If I'm correct than "UpgradeNotEnoughItems" is what I need for each value to get
TheRanger
TheRanger14mo ago
did you finish naming the buttons correctly first?
kokars
kokars14mo ago
What you mean naming correctly? I can rewrite name on buttons
TheRanger
TheRanger14mo ago
like naming them as u intended Wood x1
kokars
kokars14mo ago
On that button text even could be now wood but just w
TheRanger
TheRanger14mo ago
etc...
kokars
kokars14mo ago
kokars
kokars14mo ago
this is how I need them
kokars
kokars14mo ago
kokars
kokars14mo ago
that number is chaning depending on base. it could be 1k or 200k
TheRanger
TheRanger14mo ago
can u elaborate? what does that even mean
kokars
kokars14mo ago
wait
kokars
kokars14mo ago
ero
ero14mo ago
it's indiscernible what your problem is like do you not know how to display it, or do you not know how to get that number or
kokars
kokars14mo ago
I need to display each chaning value for each button from that BuildingGrades.cs Don't know how to get that value from that other file
ero
ero14mo ago
what other file
kokars
kokars14mo ago
and to display it in my file
ero
ero14mo ago
display what in what file? you're displaying it in the game, no? you're not displaying anything in a file
kokars
kokars14mo ago
yes sorry not in file
ero
ero14mo ago
that umod page has a discord server of its own why not ask there?
kokars
kokars14mo ago
Oh there is no help. toxic and asked like week ago another issue none responded like in dev level
TheRanger
TheRanger14mo ago
so where is the value 11500 stored?
TheRanger
TheRanger14mo ago
that's not what im asking
kokars
kokars14mo ago
That value comes from building block if i remove some wall add some wall it will change
TheRanger
TheRanger14mo ago
where is that value originally stored, in a file? in the game itself?
kokars
kokars14mo ago
yes
TheRanger
TheRanger14mo ago
yes... for file.. or yes.. for in the game?
kokars
kokars14mo ago
value comes from game but that value in chat comes from that BuildingGrades.cs file
kokars
kokars14mo ago
TheRanger
TheRanger14mo ago
where in BuildingGrades.cs?
kokars
kokars14mo ago
uMod - Building Grades by Dana
Allows players to easily upgrade or downgrade an entire building
kokars
kokars14mo ago
Just to show those values can change
ero
ero14mo ago
nothing "comes" from a .cs file. it comes from the code that's executed. the code for it just happens to exist in that file
kokars
kokars14mo ago
okey sorry
TheRanger
TheRanger14mo ago
looks like its coming from the _missingItems field
kokars
kokars14mo ago
yes could be
kokars
kokars14mo ago
I could just write in my file this line?
cap5lut
cap5lut14mo ago
nope generally looking at the api surface of that plugin there isnt really a chance to get that information unless u copy the code over and do it urself
kokars
kokars14mo ago
But how can I extuce that chat command then?
cap5lut
cap5lut14mo ago
but for that u would need some modifications, and for that u would need a deeper understanding of c# and the rust plugin ecosystem
kokars
kokars14mo ago
Yeah that's the problem that rust plugin ecosystem has no docs really like normal examples
cap5lut
cap5lut14mo ago
then its mostly try and error and looking at that building grades plugin
kokars
kokars14mo ago
that BuildingGrade plugin works my plugin works You think there is no way to get those values from BuildingGrades in to my gui? because I have seen simmular plugins on other servers, but those are custom written too
cap5lut
cap5lut14mo ago
if u get the instance of the plugin somehow, u could try with reflection, but that will not be easy either
TheRanger
TheRanger14mo ago
with tracking missingItems it looks like originally it constructs the value from constructionGrade.costToBuild where constructionGrade got its reference from buildingBlock.blockDefinition.GetGrade(targetGrade, 0);
kokars
kokars14mo ago
yeah that I see but how to get that value in my plugin to display on button?
cap5lut
cap5lut14mo ago
basically u would have to write similar methods in ur plugin to get the desired info
kokars
kokars14mo ago
That was the question
TheRanger
TheRanger14mo ago
keep tracking where the references are coming from there might be a way
kokars
kokars14mo ago
That BuildingGrades is reference in my plugin
kokars
kokars14mo ago
here is my all code [PluginReference] private readonly Plugin BuildingGrades; it reads references from grade
cap5lut
cap5lut14mo ago
yeah but it provides no public access to the info, so either use reflection to get the info the ugly way or reimplement modified versions of their methods to do so
kokars
kokars14mo ago
what you mean by ugly way?
cap5lut
cap5lut14mo ago
reflection
cap5lut
cap5lut14mo ago
Reflection in .NET - .NET Framework
Review reflection in .NET. Get information about loaded assemblies and the types defined within them, such as classes, interfaces, structures, and enumerations.
kokars
kokars14mo ago
so basically write new plugin?
cap5lut
cap5lut14mo ago
reimplement modified versions of their methods to do so
would be probably faster, cleaner and more important, independent to code changes in their plugin
kokars
kokars14mo ago
ahh will think about it because I don't know C#
cap5lut
cap5lut14mo ago
basically, in ur plugin, write some methods to get the cost info, u can use their implementation as reference on how to get that info well all of that implies that u need to learn c# first
kokars
kokars14mo ago
understand that, but can't figure out how to start that I can't just copy necasery code in my file? like some funcions etc?
cap5lut
cap5lut14mo ago
first of all im not sure if their licensing allowes that, second u would still need to know what to copy and to modify to ur needs -> u need to learn c# anyway
kokars
kokars14mo ago
ahh i just need to get value from each tier
cap5lut
cap5lut14mo ago
oh, another way would probably be to kindly ask the plugin owners if they could provide the means to access that information, up to them if they do or not
kokars
kokars14mo ago
what information do you mean?
cap5lut
cap5lut14mo ago
tho thats probably the slowest way to get ur stuff done the values u want to display
kokars
kokars14mo ago
but if in that plugin already values display in chat What is the problem to get them in my gui?
cap5lut
cap5lut14mo ago
that they dont provide a way to publicly access these values they just use them internally
TheRanger
TheRanger14mo ago
looks like ur lucky, BuildingGrades is a singleton that can be accessed from its static field
kokars
kokars14mo ago
?
TheRanger
TheRanger14mo ago
you can just trace where the reference of the missing items message is coming from
cap5lut
cap5lut14mo ago
they have a reference to their plguin, just not the underlying stuff is accessible
kokars
kokars14mo ago
kokars
kokars14mo ago
I see it from here i guess
TheRanger
TheRanger14mo ago
something like BuildingGrades.Instance.Assistant.AllBuildingItems since those properties dont exist, you have to create them to refer to their field
kokars
kokars14mo ago
in my plugin?
TheRanger
TheRanger14mo ago
tho i cant guarantee it will work the plugin clears the allbuildingItems list at some point
kokars
kokars14mo ago
Basically i need private IEnumerator UpgradeBuildingBlocks(BasePlayer player, BuildingGrade.Enum targetGrade, PermissionSettings permissionSettings, bool isAdmin) to work in my plugin
TheRanger
TheRanger14mo ago
what does it do? can u even modify BuildingGrades.cs ?
cap5lut
cap5lut14mo ago
as i understood its not kokars plugin, kokars wants to write one on top of it, right?
kokars
kokars14mo ago
yes i can well I just need it in GUI My buttons already are working with those chat commands from BuildingGrades
TheRanger
TheRanger14mo ago
i gave you this hint, go try it
kokars
kokars14mo ago
like in my code or in that building grades?
TheRanger
TheRanger14mo ago
what do u think?
kokars
kokars14mo ago
in my
TheRanger
TheRanger14mo ago
you want to get the value and store it in your GUI button, right?
kokars
kokars14mo ago
yeah
TheRanger
TheRanger14mo ago
it sounds like you didnt get my hint
kokars
kokars14mo ago
sorry I'm not backend developer
TheRanger
TheRanger14mo ago
i wont write code for you
kokars
kokars14mo ago
Can't figure it out yet
kokars
kokars14mo ago
something like this?
TheRanger
TheRanger14mo ago
nope
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
TheRanger
TheRanger14mo ago
you are suppose to create a static property called Instance in your BuildingGrades class that gets the reference from the field _instance;
kokars
kokars14mo ago
in BuildingGrade.cs
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
kokars
kokars14mo ago
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
TheRanger
TheRanger14mo ago
u posted ur GUI code somewhere, right?
kokars
kokars14mo ago
yes here again
TheRanger
TheRanger14mo ago
i wonder if you can get the value through the arguments parentor tc in the method
kokars
kokars14mo ago
tc parent is for ui
TheRanger
TheRanger14mo ago
can you show the file BuildingPrivlidge.cs ?
TheRanger
TheRanger14mo ago
wrong file
kokars
kokars14mo ago
?
TheRanger
TheRanger14mo ago
BuildingPrivlidge.cs not BuildingGrades.cs
kokars
kokars14mo ago
sorry no it is buildinggrades
TheRanger
TheRanger14mo ago
where? i cant find it
kokars
kokars14mo ago
here
kokars
kokars14mo ago
TheRanger
TheRanger14mo ago
yes where in the file? which line? i cant find class BuildingPrivlidge
kokars
kokars14mo ago
? ou that is from oxide it self
TheRanger
TheRanger14mo ago
so its not possible to view that file?
kokars
kokars14mo ago
BuildingPrivlidge tc - is needed to access in game toolcupboard
kokars
kokars14mo ago
you mean like this?
TheRanger
TheRanger14mo ago
what are u trying to show me can u ctrl + left click BuildingPrivlidge ?
kokars
kokars14mo ago
next?
TheRanger
TheRanger14mo ago
?
kokars
kokars14mo ago
I can't open it but "BuildingPrivlidge tc" is only needed to shop gui interface when in game cupboard is opened and that's it for that is no other purpose
TheRanger
TheRanger14mo ago
then how would it know which building you are trying to build/upgrade?
kokars
kokars14mo ago
When cupboard is placed it automatically detects building BuildingPrivlidge tc = is when player is authorized to toolcupboard
ero
ero14mo ago
what a hugely embarrassing typo
TheRanger
TheRanger14mo ago
well yes, that class must have some references to those missing blocks any chance tc has a property called allowedConstructionItems ?
kokars
kokars14mo ago
that i don't know but will try to find out
kokars
kokars14mo ago
Did it
TheRanger
TheRanger14mo ago
u figured out how to get the references from tc?
kokars
kokars14mo ago
Will show in a while Not at pc right now
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts