Accessing public method of another class - class

Basically, I'm trying to call a method in another class, but I'm running into errors.
Here is the code:
public class Ocean
{
private static int gridWidth, gridHeight;
private int safeCount;
private boolean shipSafe;
private static Ship[] ships;
private static int[][] grids;
public Ocean(int a, int b, int c)
{
grids = new int[a][b];
ships = new Ship[c];
for(int i = 0; i < c; i++)
{
ships[i] = Ship();
}
gridWidth = a;
gridHeight = b;
}
I cannot access the public Ship class, and the error appears in ships[i] = Ship();
I've tried redefining it, adding a constructor.

change the line ships[i] = new Ship();

Related

How can I create foldout EditorGUI element with toggle in Unity Inspector

Need your help.
Easily create a foldout element with toggle list. Like that
But I need to create foldout element with toggle in a header. Like that
I think it's possible because scripts header already have this
I tried to find the answer here but didn't find anything like it.
Thank you for help
You can override how your Serializable class is rendered using EditorGUI by creating a custom PropertyAttribute and PropertyDrawer.
Example
I cooked up an attribute that takes a boolean field (specified by you) and instead of rendering it as usual, it renders it as a checkbox at the top. You can have any number of boolean fields inside your class, they should render just fine.
This implementation renders only boolean fields. If you wish to render other kind of stuff besides that, feel free to extend this solution.
Implementation
using UnityEngine;
public class ToggleListAttribute : PropertyAttribute
{
public string StatusPropertyName { get; private set; }
public ToggleListAttribute(string statusPropertyName)
{
StatusPropertyName = statusPropertyName;
}
}
using System;
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(ToggleListAttribute))]
public class ToggleListDrawer : PropertyDrawer
{
private bool show;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var statusProperty = GetStatusPropertyFrom(property);
var foldoutRect = GetLinePositionFrom(position, 1);
show = EditorGUI.Foldout(
foldoutRect,
show,
string.Empty,
false);
statusProperty.boolValue = EditorGUI.ToggleLeft(
foldoutRect,
property.displayName,
statusProperty.boolValue);
if (show)
RenderSubproperties(property, position);
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (show)
return EditorGUIUtility.singleLineHeight * (GetBooleanPropertyCount(property) + 1);
else
return EditorGUIUtility.singleLineHeight;
}
private SerializedProperty GetStatusPropertyFrom(SerializedProperty property)
{
var listAttribute = attribute as ToggleListAttribute;
var statusProperty = property.FindPropertyRelative(
listAttribute.StatusPropertyName);
if (statusProperty == null)
throw new Exception($"No property named \"{listAttribute.StatusPropertyName}\" found!");
return statusProperty;
}
private void RenderSubproperties(SerializedProperty property, Rect position)
{
var innerPosition = new Rect(
position.x + EditorGUIUtility.standardVerticalSpacing * 4,
position.y,
position.width,
position.height);
var statusProperty = GetStatusPropertyFrom(property);
int line = 2;
foreach (var instance in property)
{
var subproperty = instance as SerializedProperty;
if (subproperty.propertyType != SerializedPropertyType.Boolean ||
subproperty.name == statusProperty.name)
{
continue;
}
subproperty.boolValue = EditorGUI.ToggleLeft(
GetLinePositionFrom(innerPosition, line),
subproperty.displayName,
subproperty.boolValue);
line++;
}
}
private int GetBooleanPropertyCount(SerializedProperty property)
{
int count = 0;
foreach (var instance in property)
{
var subproperty = instance as SerializedProperty;
if (subproperty.propertyType != SerializedPropertyType.Boolean)
continue;
count++;
}
return count - 1;
}
private Rect GetLinePositionFrom(Rect rect, int line)
{
float heightModifier = EditorGUIUtility.singleLineHeight * (line - 1);
return new Rect(
rect.x,
rect.y + heightModifier,
rect.width,
EditorGUIUtility.singleLineHeight);
}
}
Usage
using System;
using UnityEngine;
public class Example : MonoBehaviour
{
[ToggleList("enabled")]
public RenderList list1;
[ToggleList("enabled")]
public RenderList2 list2;
}
[Serializable]
public class RenderList
{
public bool enabled;
public bool floor;
public bool car;
public bool train;
}
[Serializable]
public class RenderList2
{
public bool enabled;
public bool one;
public bool two;
public bool three;
public bool four;
public bool five;
public bool six;
public bool seven;
}
Use EditorGUILayout.InspectorTitlebar(foldout: foldout, editor: targetEditor);

Unity3D Access class from another script

hey how can i access this list of int and strings from another
script?
// Slot One Data
[Serializable]
public class SlotOneStats
{
public string nameOne;
public int roleOne;
public int strengthOne;
public int meleeOne;
public int shootingOne;
public int huntingOne;
public int cookingOne;
public int craftingOne;
public int buildingOne;
public int engineeringOne;
}
i tried changing 'public' to 'static' or 'public static' but whenever i changed to static
it will say
Static member `Main.SlotOneStats.ALLTHESTATICVARIABLEGOESHERE' cannot be accessed with an
instance reference, qualify it with a type name instead
BinaryFormatter bfWriter = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/dataStats" + onSlot + ".fgsv");
if(onSlot == 1)
{
SlotOneStats slotoneStats = new SlotOneStats();
slotoneStats.nameOne = name;
slotoneStats.roleOne = role;
slotoneStats.strengthOne = strength;
slotoneStats.meleeOne = melee;
slotoneStats.shootingOne = shooting;
slotoneStats.huntingOne = hunting;
slotoneStats.cookingOne = cooking;
slotoneStats.craftingOne = crafting;
slotoneStats.buildingOne = building;
slotoneStats.engineeringOne = engineering;
bfWriter.Serialize(file, slotoneStats);
}
I would suggest adding a public instance of the class to the first script:
public class SlotOneStats
{
public string nameOne;
public int roleOne;
public int strengthOne;
public int meleeOne;
public int shootingOne;
public int huntingOne;
public int cookingOne;
public int craftingOne;
public int buildingOne;
public int engineeringOne;
}
public SlotOneStats SOS;
Now just access the public varaible SOS and it should work:
GameObject example = GameObject.Find("Object_with_script").GetComponent<Script_with_class>.SOS;
// do stuff with example

Is there something wrong with my eclipse calculator?

public class TaxReturn {
private double rate1= 0.10;
private double rate2=0.25;
private double single_limit = 32000;
private double married_limit = 64000;
private double income;
private int status;
public static int married=2;
public static int single=1;
public TaxReturn(double inc, int stat){
double income = inc;
int status=stat;
}
public double getTaxi(){
double tax1=0;
double tax2=0;
if(status==single){
if(income<=single_limit)
tax1=rate1*income;
else{
tax1=rate1*single_limit;
tax2=rate2*(income-single_limit);
}
}
else{
if(income<=married_limit)
tax1=rate1*income;
else
tax1=rate1*married_limit;
tax2=rate2*(income-married_limit);
}
return tax1+tax2;
}
}
import java.util.Scanner;
public class TaxCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("eneter income; avoid commas");
double income = sc.nextDouble();
System.out.println("are you married; type Y or N");
String status=sc.next();
int statuss;
if (status.equalsIgnoreCase("y"))
statuss=TaxReturn.married;
else
statuss=TaxReturn.single;
TaxReturn tr = new TaxReturn(income, statuss);
System.out.println("your tax is: " + tr.getTaxi());
}
}
I keep getting-16000 for the answer.I dont know if my code is wrong or something wrong with the software. This code was copied from the book. Iva had this problem with other codes too. Any help would be appreciated. Thanks
Your constructor is not correct.
Rewrite it as follow
public TaxReturn(double inc, int stat){
income = inc;
status = stat;
}
By declaring type on income and status variables, you made them local to the constructor.

Drools log file

I have tried and unable to find a document that describes the attributes in Drools log file. For example, in below HelloWorld example log what is type?
<org.drools.audit.event.ActivationLogEvent>
<type>4</type>
<activationId>Hello World [1]</activationId>
<rule>Hello World</rule>
<declarations>m=com.sample.DroolsTest$Message#19a01f9(1)</declarations>
</org.drools.audit.event.ActivationLogEvent>
From ActivationLogEvent.java:
#param type The type of event. This can only be ACTIVATION_CREATED, ACTIVATION_CANCELLED, BEFORE_ACTIVATION_FIRE or AFTER_ACTIVATION_FIRE.
From LogEvent.java:
public static final int ACTIVATION_CREATED = 4;
public static final int ACTIVATION_CANCELLED = 5;
public static final int BEFORE_ACTIVATION_FIRE = 6;
public static final int AFTER_ACTIVATION_FIRE = 7;
So I guess your event is an ACTIVATION_CREATED event.

Instance variables in Java

Please explain the following behaviour.
class Base
{
public int num = 3;
public int getNum()
{
return num;
}
public void setNum(int num)
{
this.num = num;
}
}
class child
extends Base
{
public int num = 4;
public child()
{
}
public child(int i)
{
this.num = i;
}
public int getNum()
{
return num;
}
public void setNum(int num)
{
this.num = num;
}
}
public class Main
{
public static void main(String[] args)
{
Base obj2 = new child();
System.out.println(obj2.num);
System.out.println(obj2.getNum());
Base obj3 = new child(10);
System.out.println(obj3.num);
System.out.println(obj3.getNum());
}
}
Output :
3
4
3
10
Here how obj2.num and obj3.num still point to the value 3 which is assigned in the Base class instance variable. Wont it get overidded like the obj2.getNum() and obj3.getNum().
Thanks in advance.
Because the objects are declared with super class type, you get the super member variable value.
If the object was declared with sub class type, the value would be overridden value.
If the Base obj3 = new child(10); is modified to child obj3 = new child(10); the output would be 3 4 10 10
This is well explained here